Skip to content

Commit 20b557e

Browse files
Add CToT3 function to convert JSON time strings to DateTime with default date part
(cherry picked from commit c409704) # Conflicts: # dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs
1 parent a70c6c5 commit 20b557e

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,6 +2872,32 @@ public static GxSimpleCollection<DateTime> CToT2(GxSimpleCollection<string> stCo
28722872
}
28732873
return dtCollection;
28742874
}
2875+
public static DateTime CToT3(string value)
2876+
{
2877+
if (isNullJsonDate(value))
2878+
return nullDate;
2879+
else
2880+
{
2881+
DateTime timeOnlyDateTime = nullDate;
2882+
TimeSpan timeSpan;
2883+
if (TimeSpan.TryParse(value, out timeSpan))
2884+
{
2885+
timeOnlyDateTime = DateTime.MinValue.Add(timeSpan);
2886+
}
2887+
else if (DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out timeOnlyDateTime))
2888+
{
2889+
timeOnlyDateTime = ResetDate(timeOnlyDateTime);
2890+
}
2891+
else if (value.StartsWith(GxDateString.NullValue))
2892+
{
2893+
value = value.Substring(GxDateString.NullValue.Length);
2894+
DateTime.TryParse(GxDateString.GregorianDate + value, CultureInfo.InvariantCulture, DateTimeStyles.None, out timeOnlyDateTime);
2895+
2896+
}
2897+
2898+
return timeOnlyDateTime;
2899+
}
2900+
}
28752901
public static DateTime CToT2(string value, IGxContext context)
28762902
{
28772903
if (isNullJsonDate(value))

dotnet/test/DotNetUnitTest/Domain/TimeZoneTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,27 @@ public void Year2050Conversion()
287287
#endregion
288288

289289
}
290+
[Fact]
291+
public void TimeZoneInJsonTime()
292+
{
293+
DateTime value = DateTimeUtil.CToT3("19:00:00");
294+
DateTime expected = DateTime.MinValue.AddHours(19);
295+
Assert.Equal(expected, value);
296+
297+
value = DateTimeUtil.CToT3("19:00:00.000");
298+
Assert.Equal(expected, value);
299+
300+
value = DateTimeUtil.CToT3("1899-12-31T19:00:00.000");
301+
Assert.Equal(expected, value);
290302

303+
value = DateTimeUtil.CToT3("0000-00-00T19:00:00.000");
304+
Assert.Equal(expected, value);
305+
306+
value = DateTimeUtil.CToT3("0001-01-01T19:00:00.000");
307+
Assert.Equal(expected, value);
308+
309+
value = DateTimeUtil.CToT3("2025-04-14T19:00:00.000");
310+
Assert.Equal(expected, value);
311+
}
291312
}
292313
}

0 commit comments

Comments
 (0)