Skip to content

Commit 00d9158

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 00d9158

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

nanoFramework.CoreLibrary/System/DateTime.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public DateTime(long ticks)
195195
/// nanoFramework doesn't support local time, only UTC, so it's not possible to specify <see cref="DateTimeKind.Local"/>.
196196
/// </remarks>
197197
public DateTime(long ticks, DateTimeKind kind)
198-
:this(ticks)
198+
: this(ticks)
199199
{
200200
// it's OK to check kind parameter only here
201201
// if it's invalid the exception will be thrown anyway and allows the constructor to be reused
@@ -505,8 +505,7 @@ public int Month
505505
/// </value>
506506
public static DateTime UtcNow
507507
{
508-
[MethodImpl(MethodImplOptions.InternalCall)]
509-
get => new DateTime();
508+
get => new DateTime(GetUtcNowAsTicks(), DateTimeKind.Utc);
510509
}
511510

512511
/// <summary>
@@ -556,8 +555,7 @@ public TimeSpan TimeOfDay
556555
/// </value>
557556
public static DateTime Today
558557
{
559-
[MethodImpl(MethodImplOptions.InternalCall)]
560-
get => new DateTime();
558+
get => new DateTime(GetTodayAsTicks(), DateTimeKind.Utc);
561559
}
562560

563561
/// <summary>
@@ -826,10 +824,10 @@ public static bool TryParse(
826824
string s,
827825
out DateTime result)
828826
{
829-
result = Convert.NativeToDateTime(
830-
s,
827+
Convert.NativeToDateTime(s,
831828
false,
832-
out bool success);
829+
out bool success,
830+
out result);
833831

834832
return success;
835833
}
@@ -839,5 +837,11 @@ public static bool TryParse(
839837

840838
[MethodImpl(MethodImplOptions.InternalCall)]
841839
private extern int GetDateTimePart(DateTimePart part);
840+
841+
[MethodImpl(MethodImplOptions.InternalCall)]
842+
private extern static long GetUtcNowAsTicks();
843+
844+
[MethodImpl(MethodImplOptions.InternalCall)]
845+
private extern static long GetTodayAsTicks();
842846
}
843847
}

0 commit comments

Comments
 (0)