Skip to content

Commit 8f4a6e4

Browse files
committed
Fix UtcNow and Today
- These are now calling a native method that returns the corresponding ticks and instantiates a DateTime with those.
1 parent d291a82 commit 8f4a6e4

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

nanoFramework.CoreLibrary/System/DateTime.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,12 @@ public enum DateTimeKind
8282
#endif // NANOCLR_REFLECTION
8383
public struct DateTime
8484
{
85-
/// Our origin is at 1601/01/01:00:00:00.000
86-
/// While desktop CLR's origin is at 0001/01/01:00:00:00.000.
87-
/// There are 504911232000000000 ticks between them which we are subtracting.
85+
// Our origin is at 1601/01/01:00:00:00.000
86+
// While desktop CLR's origin is at 0001/01/01:00:00:00.000.
87+
// There are 504911232000000000 ticks between them which we are subtracting.
88+
//////////////////////////////////////////////////////////////////////////////////////////
89+
/// Keep in sync with native define TICKS_AT_ORIGIN @ corlib_native_System_DateTime.cpp //
90+
//////////////////////////////////////////////////////////////////////////////////////////
8891
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
8992
private const long _ticksAtOrigin = 504911232000000000;
9093

@@ -195,7 +198,7 @@ public DateTime(long ticks)
195198
/// nanoFramework doesn't support local time, only UTC, so it's not possible to specify <see cref="DateTimeKind.Local"/>.
196199
/// </remarks>
197200
public DateTime(long ticks, DateTimeKind kind)
198-
:this(ticks)
201+
: this(ticks)
199202
{
200203
// it's OK to check kind parameter only here
201204
// if it's invalid the exception will be thrown anyway and allows the constructor to be reused
@@ -505,8 +508,7 @@ public int Month
505508
/// </value>
506509
public static DateTime UtcNow
507510
{
508-
[MethodImpl(MethodImplOptions.InternalCall)]
509-
get => new DateTime();
511+
get => new DateTime(GetUtcNowAsTicks(), DateTimeKind.Utc);
510512
}
511513

512514
/// <summary>
@@ -556,8 +558,7 @@ public TimeSpan TimeOfDay
556558
/// </value>
557559
public static DateTime Today
558560
{
559-
[MethodImpl(MethodImplOptions.InternalCall)]
560-
get => new DateTime();
561+
get => new DateTime(GetTodayAsTicks(), DateTimeKind.Utc);
561562
}
562563

563564
/// <summary>
@@ -826,10 +827,10 @@ public static bool TryParse(
826827
string s,
827828
out DateTime result)
828829
{
829-
result = Convert.NativeToDateTime(
830-
s,
830+
Convert.NativeToDateTime(s,
831831
false,
832-
out bool success);
832+
out bool success,
833+
out result);
833834

834835
return success;
835836
}
@@ -839,5 +840,11 @@ public static bool TryParse(
839840

840841
[MethodImpl(MethodImplOptions.InternalCall)]
841842
private extern int GetDateTimePart(DateTimePart part);
843+
844+
[MethodImpl(MethodImplOptions.InternalCall)]
845+
private extern static long GetUtcNowAsTicks();
846+
847+
[MethodImpl(MethodImplOptions.InternalCall)]
848+
private extern static long GetTodayAsTicks();
842849
}
843850
}

0 commit comments

Comments
 (0)