Skip to content

Commit

Permalink
RavenDB-23346 Removed option to fallback to old code. We're not gonna…
Browse files Browse the repository at this point in the history
… use it anyway since.
  • Loading branch information
arekpalinski committed Dec 17, 2024
1 parent c4b98b6 commit 325adc7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 101 deletions.
54 changes: 8 additions & 46 deletions src/Sparrow/Json/LazyStringParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ public static bool TryParseTimeSpan(char* buffer, int len, out TimeSpan ts)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Result TryParseDateTime(char* buffer, int len, out DateTime dt, out DateTimeOffset dto, bool properlyParseThreeDigitsMilliseconds, bool properlyParseTrailingZeros = true)
public static Result TryParseDateTime(char* buffer, int len, out DateTime dt, out DateTimeOffset dto, bool properlyParseThreeDigitsMilliseconds)
{
// PERF: We want this part of the code to be embedded into the caller code instead.
if (len < 19 || len > 33)
Expand All @@ -555,7 +555,7 @@ public static Result TryParseDateTime(char* buffer, int len, out DateTime dt, ou
if (buffer[4] != '-' || buffer[7] != '-' || buffer[10] != 'T' ||
buffer[13] != ':' || buffer[16] != ':' || buffer[16] != ':')
{
if (properlyParseTrailingZeros == false || len != 29 || buffer[3] != ',' || buffer[19] != ':' || buffer[22] != ':')
if (len != 29 || buffer[3] != ',' || buffer[19] != ':' || buffer[22] != ':')
goto Failed;

// ddd, dd MMM yyyy HH':'mm':'ss 'GMT' - "r" format specifier
Expand All @@ -572,15 +572,15 @@ public static Result TryParseDateTime(char* buffer, int len, out DateTime dt, ou
}
}

return TryParseDateTimeInternal(buffer, len, out dt, out dto, properlyParseThreeDigitsMilliseconds, properlyParseTrailingZeros);
return TryParseDateTimeInternal(buffer, len, out dt, out dto, properlyParseThreeDigitsMilliseconds);

Failed:
dt = default(DateTime);
dto = default(DateTimeOffset);
return Result.Failed;
}

public static Result TryParseDateTimeInternal(char* buffer, int len, out DateTime dt, out DateTimeOffset dto, bool properlyParseThreeDigitsMilliseconds, bool properlyParseTrailingZeros)
public static Result TryParseDateTimeInternal(char* buffer, int len, out DateTime dt, out DateTimeOffset dto, bool properlyParseThreeDigitsMilliseconds)
{
if (TryParseNumber4(buffer, 0, out int year) == false)
goto Failed;
Expand Down Expand Up @@ -621,9 +621,6 @@ public static Result TryParseDateTimeInternal(char* buffer, int len, out DateTim
goto case 23;
}

if (properlyParseTrailingZeros == false)
goto Failed;

if (buffer[19] != '.')
goto Failed;
if (TryParseNumber4(buffer, 20, out fractions) == false)
Expand All @@ -634,9 +631,6 @@ public static Result TryParseDateTimeInternal(char* buffer, int len, out DateTim
case 23: //"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff" OR "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffZ",
if (buffer[22] == 'Z')
{
if (properlyParseTrailingZeros == false)
goto Failed;

kind = DateTimeKind.Utc;
goto case 22;
}
Expand All @@ -650,9 +644,6 @@ public static Result TryParseDateTimeInternal(char* buffer, int len, out DateTim
case 25: //"yyyy'-'MM'-'dd'T'HH':'mm':'ss'+'dd':'dd'" OR "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffff" OR "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffZ"
if (buffer[22] != ':' || (buffer[19] != '+' && buffer[19] != '-'))
{
if (properlyParseTrailingZeros == false)
goto Failed;

if (buffer[24] == 'Z')
{
kind = DateTimeKind.Utc;
Expand Down Expand Up @@ -688,9 +679,6 @@ public static Result TryParseDateTimeInternal(char* buffer, int len, out DateTim
case 27: //"yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffff" OR "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffZ"
if (buffer[26] == 'Z')
{
if (properlyParseTrailingZeros == false)
goto Failed;

kind = DateTimeKind.Utc;
goto case 26;
}
Expand Down Expand Up @@ -720,9 +708,6 @@ public static Result TryParseDateTimeInternal(char* buffer, int len, out DateTim
result = Result.DateTimeOffset;
goto Finished;
case 22: //"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ff" OR "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fZ",
if (properlyParseTrailingZeros == false)
break;

if (buffer[21] == 'Z')
{
kind = DateTimeKind.Utc;
Expand All @@ -735,17 +720,13 @@ public static Result TryParseDateTimeInternal(char* buffer, int len, out DateTim
fractions *= 100000;
goto Finished_DT;
case 21: //"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'f",
if (properlyParseTrailingZeros == false)
break;
if (buffer[19] != '.')
goto Failed;
if (TryParseNumber(buffer + 20, 1, out fractions) == false)
goto Failed;
fractions *= 1000000;
goto Finished_DT;
case 26: //"yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffff" OR "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffZ"
if (properlyParseTrailingZeros == false)
break;
if (buffer[25] == 'Z')
{
kind = DateTimeKind.Utc;
Expand Down Expand Up @@ -775,7 +756,7 @@ public static Result TryParseDateTimeInternal(char* buffer, int len, out DateTim
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Result TryParseDateTime(byte* buffer, int len, out DateTime dt, out DateTimeOffset dto, bool properlyParseThreeDigitsMilliseconds, bool properlyParseTrailingZeros = true)
public static Result TryParseDateTime(byte* buffer, int len, out DateTime dt, out DateTimeOffset dto, bool properlyParseThreeDigitsMilliseconds)

{
// PERF: We want this part of the code to be embedded into the caller code instead.
Expand All @@ -785,7 +766,7 @@ public static Result TryParseDateTime(byte* buffer, int len, out DateTime dt, ou
if (buffer[4] != '-' || buffer[7] != '-' || buffer[10] != 'T' ||
buffer[13] != ':' || buffer[16] != ':' || buffer[16] != ':')
{
if (properlyParseTrailingZeros == false || len != 29 || buffer[3] != ',' || buffer[19] != ':' || buffer[22] != ':')
if (len != 29 || buffer[3] != ',' || buffer[19] != ':' || buffer[22] != ':')
goto Failed;

// ddd, dd MMM yyyy HH':'mm':'ss 'GMT' - "r" format specifier
Expand All @@ -796,15 +777,15 @@ public static Result TryParseDateTime(byte* buffer, int len, out DateTime dt, ou
}
}

return TryParseDateTimeInternal(buffer, len, out dt, out dto, properlyParseThreeDigitsMilliseconds, properlyParseTrailingZeros);
return TryParseDateTimeInternal(buffer, len, out dt, out dto, properlyParseThreeDigitsMilliseconds);

Failed:
dt = default(DateTime);
dto = default(DateTimeOffset);
return Result.Failed;
}

private static Result TryParseDateTimeInternal(byte* buffer, int len, out DateTime dt, out DateTimeOffset dto, bool properlyParseThreeDigitsMilliseconds, bool properlyParseTrailingZeros)
private static Result TryParseDateTimeInternal(byte* buffer, int len, out DateTime dt, out DateTimeOffset dto, bool properlyParseThreeDigitsMilliseconds)
{
if (TryParseNumber4(buffer, 0, out int year) == false)
goto Failed;
Expand Down Expand Up @@ -845,9 +826,6 @@ private static Result TryParseDateTimeInternal(byte* buffer, int len, out DateTi
goto case 23;
}

if (properlyParseTrailingZeros == false)
goto Failed;

if (buffer[19] != '.')
goto Failed;
if (TryParseNumber4(buffer, 20, out fractions) == false)
Expand All @@ -858,9 +836,6 @@ private static Result TryParseDateTimeInternal(byte* buffer, int len, out DateTi
case 23: //"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff" OR "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffZ",
if (buffer[22] == 'Z')
{
if (properlyParseTrailingZeros == false)
goto Failed;

kind = DateTimeKind.Utc;
goto case 22;
}
Expand All @@ -874,9 +849,6 @@ private static Result TryParseDateTimeInternal(byte* buffer, int len, out DateTi
case 25: //"yyyy'-'MM'-'dd'T'HH':'mm':'ss'+'dd':'dd'" OR "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffff" OR "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffZ"
if (buffer[22] != ':' || (buffer[19] != '+' && buffer[19] != '-'))
{
if (properlyParseTrailingZeros == false)
goto Failed;

if (buffer[24] == 'Z')
{
kind = DateTimeKind.Utc;
Expand Down Expand Up @@ -912,9 +884,6 @@ private static Result TryParseDateTimeInternal(byte* buffer, int len, out DateTi
case 27: //"yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffff" OR "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffZ"
if (buffer[26] == 'Z')
{
if (properlyParseTrailingZeros == false)
goto Failed;

kind = DateTimeKind.Utc;
goto case 26;
}
Expand Down Expand Up @@ -944,9 +913,6 @@ private static Result TryParseDateTimeInternal(byte* buffer, int len, out DateTi
result = Result.DateTimeOffset;
goto Finished;
case 22: //"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ff" OR "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fZ",
if (properlyParseTrailingZeros == false)
break;

if (buffer[21] == 'Z')
{
kind = DateTimeKind.Utc;
Expand All @@ -959,17 +925,13 @@ private static Result TryParseDateTimeInternal(byte* buffer, int len, out DateTi
fractions *= 100000;
goto Finished_DT;
case 21: //"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'f",
if (properlyParseTrailingZeros == false)
break;
if (buffer[19] != '.')
goto Failed;
if (TryParseNumber(buffer + 20, 1, out fractions) == false)
goto Failed;
fractions *= 1000000;
goto Finished_DT;
case 26: //"yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffff" OR "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffZ"
if (properlyParseTrailingZeros == false)
break;
if (buffer[25] == 'Z')
{
kind = DateTimeKind.Utc;
Expand Down
55 changes: 0 additions & 55 deletions test/FastTests/Utils/TimeParsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,60 +317,5 @@ private static DateTime GetRandomDate(Random random, int minYear = 1900, int max
randomDate = randomDate.AddMilliseconds(random.Next(0, 9999999));
return randomDate;
}

[RavenTheory(RavenTestCategory.Core)]
[InlineData("2024-12-13T02:38:42.786481Z")]
[InlineData("2024-12-13T02:38:42.7864Z")]
[InlineData("2024-12-13T02:38:42.78644")]
[InlineData("2024-12-13T02:38:42.7868")]
[InlineData("2024-12-13T02:38:42.78Z")]
public void FailsToParseValidDatesFormattedWithFSpecifier_BackwardCompatibility(string dt)
{
const bool doNotParseTrailingZeros = false;

var bytes = Encoding.UTF8.GetBytes(dt);
fixed (byte* buffer = bytes)
{
Assert.Equal(LazyStringParser.Result.Failed,
LazyStringParser.TryParseDateTime(buffer, bytes.Length, out DateTime time, out _, properlyParseThreeDigitsMilliseconds: true, properlyParseTrailingZeros: doNotParseTrailingZeros));
}

fixed (char* buffer = dt)
{
Assert.Equal(LazyStringParser.Result.Failed,
LazyStringParser.TryParseDateTime(buffer, bytes.Length, out DateTime time, out _, properlyParseThreeDigitsMilliseconds: true, properlyParseTrailingZeros: doNotParseTrailingZeros));
}
}

[RavenTheory(RavenTestCategory.Core)]
[InlineData("2024-12-13T02:38:42.786488")]
[InlineData("2024-12-13T02:38:42.78648Z")]
[InlineData("2024-12-13T02:38:42.7Z")]
[InlineData("2024-12-13T02:38:42.77")]
[InlineData("2024-12-13T02:38:42.7")]
public void ManagesToParseValidDatesFormattedWithFSpecifier_ButParsedValuesArentCorrect_BackwardCompatibility(string dt)
{
const bool doNotParseTrailingZeros = false;

var expected = DateTime.ParseExact(dt, DefaultFormat.DateTimeFormatsToRead, CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind);

var bytes = Encoding.UTF8.GetBytes(dt);
fixed (byte* buffer = bytes)
{
Assert.Equal(LazyStringParser.Result.DateTime,
LazyStringParser.TryParseDateTime(buffer, bytes.Length, out DateTime time, out _, properlyParseThreeDigitsMilliseconds: true, properlyParseTrailingZeros: doNotParseTrailingZeros));

Assert.NotEqual(expected, time);
}

fixed (char* buffer = dt)
{
Assert.Equal(LazyStringParser.Result.DateTime,
LazyStringParser.TryParseDateTime(buffer, bytes.Length, out DateTime time, out _, properlyParseThreeDigitsMilliseconds: true, properlyParseTrailingZeros: doNotParseTrailingZeros));

Assert.NotEqual(expected, time);
}
}
}
}

0 comments on commit 325adc7

Please sign in to comment.