Skip to content

Commit 88ce097

Browse files
Add CToH2 function to convert JSON time strings to DateTime (#1126)
* Add CToT3 function to convert JSON time strings to DateTime with default date part * Rename CToT3 to CToH2 * Add HToC2 (inverse of CToH2)
1 parent f5db673 commit 88ce097

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,6 +2850,32 @@ internal static DateTime CToDT2(string jsonDate, IGxContext context) {
28502850
return CToD2(jsonDate);
28512851
}
28522852
}
2853+
public static DateTime CToH2(string value)
2854+
{
2855+
if (isNullJsonDate(value))
2856+
return nullDate;
2857+
else
2858+
{
2859+
DateTime timeOnlyDateTime = nullDate;
2860+
TimeSpan timeSpan;
2861+
if (TimeSpan.TryParse(value, out timeSpan))
2862+
{
2863+
timeOnlyDateTime = DateTime.MinValue.Add(timeSpan);
2864+
}
2865+
else if (DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out timeOnlyDateTime))
2866+
{
2867+
timeOnlyDateTime = ResetDate(timeOnlyDateTime);
2868+
}
2869+
else if (value.StartsWith(GxDateString.NullValue))
2870+
{
2871+
value = value.Substring(GxDateString.NullValue.Length);
2872+
DateTime.TryParse(GxDateString.GregorianDate + value, CultureInfo.InvariantCulture, DateTimeStyles.None, out timeOnlyDateTime);
2873+
2874+
}
2875+
2876+
return timeOnlyDateTime;
2877+
}
2878+
}
28532879
public static DateTime CToT2(string value, IGxContext context)
28542880
{
28552881
if (isNullJsonDate(value))
@@ -2890,6 +2916,10 @@ public static string TToC2(DateTime dt, IGxContext context)
28902916
{
28912917
return TToC2(dt, true, context);
28922918
}
2919+
public static string HToC2(DateTime dt)
2920+
{
2921+
return TToC2(dt, false);
2922+
}
28932923
//[Obsolete("TToC2 is deprecated, use TToC2(DateTime, bool, IGxContext) instead", false)]
28942924
public static string TToC2(DateTime dt, bool toUTC)
28952925
{

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.CToH2("19:00:00");
294+
DateTime expected = DateTime.MinValue.AddHours(19);
295+
Assert.Equal(expected, value);
296+
297+
value = DateTimeUtil.CToH2("19:00:00.000");
298+
Assert.Equal(expected, value);
299+
300+
value = DateTimeUtil.CToH2("1899-12-31T19:00:00.000");
301+
Assert.Equal(expected, value);
290302

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

0 commit comments

Comments
 (0)