-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TimeProvider GetLocalNow enhancement #90060
Comments
Tagging subscribers to this area: @dotnet/area-system-datetime Issue DetailsCurrently, we're creating a new DateTimeOffset even when the local time zone is the same as UTC runtime/src/libraries/Common/src/System/TimeProvider.cs Lines 47 to 68 in bb0be13
Could we use the utc now when the local time zone is UTC, possible update: TimeSpan offset = zoneInfo.GetUtcOffset(utcDateTime);
+ if (offset.Ticks == 0) return utcDateTime;
|
@WeihanLi did you measure it and see noticeable perf improvement? |
This issue has been marked |
@tarekgh here's the test on my local device test code: [MemoryDiagnoser]
public class TimeProviderLocalTimeTest
{
private static readonly long s_minDateTicks = DateTime.MinValue.Ticks;
private static readonly long s_maxDateTicks = DateTime.MaxValue.Ticks;
[Benchmark(Baseline = true)]
public DateTimeOffset GetLocalNow()
{
var utcDateTime = DateTimeOffset.UtcNow;
var zoneInfo = TimeZoneInfo.Utc;
var offset = zoneInfo.GetUtcOffset(utcDateTime);
var localTicks = utcDateTime.Ticks + offset.Ticks;
if ((ulong)localTicks > (ulong)s_maxDateTicks)
{
localTicks = localTicks < s_minDateTicks ? s_minDateTicks : s_maxDateTicks;
}
return new DateTimeOffset(localTicks, offset);
}
[Benchmark]
public DateTimeOffset GetLocalNow_Update()
{
var utcDateTime = DateTimeOffset.UtcNow;
var zoneInfo = TimeZoneInfo.Utc;
var offset = zoneInfo.GetUtcOffset(utcDateTime);
if (offset.Ticks is 0) return utcDateTime;
var localTicks = utcDateTime.Ticks + offset.Ticks;
if ((ulong)localTicks > (ulong)s_maxDateTicks)
{
localTicks = localTicks < s_minDateTicks ? s_minDateTicks : s_maxDateTicks;
}
return new DateTimeOffset(localTicks, offset);
}
} |
Thanks @WeihanLi, could you please add the measurement when using non-UTC zone too? to compare the perf cost for the added condition too. |
This issue has been marked |
@tarekgh test result for the non-UTC test I'm using the [MemoryDiagnoser]
public class TimeProviderLocalTimeNonUtcTest
{
private static readonly long s_minDateTicks = DateTime.MinValue.Ticks;
private static readonly long s_maxDateTicks = DateTime.MaxValue.Ticks;
[Benchmark(Baseline = true)]
public DateTimeOffset GetLocalNow()
{
var utcDateTime = DateTimeOffset.UtcNow;
var zoneInfo = TimeZoneInfo.Local;
var offset = zoneInfo.GetUtcOffset(utcDateTime);
var localTicks = utcDateTime.Ticks + offset.Ticks;
if ((ulong)localTicks > (ulong)s_maxDateTicks)
{
localTicks = localTicks < s_minDateTicks ? s_minDateTicks : s_maxDateTicks;
}
return new DateTimeOffset(localTicks, offset);
}
[Benchmark]
public DateTimeOffset GetLocalNow_Update()
{
var utcDateTime = DateTimeOffset.UtcNow;
var zoneInfo = TimeZoneInfo.Local;
var offset = zoneInfo.GetUtcOffset(utcDateTime);
if (offset.Ticks is 0) return utcDateTime;
var localTicks = utcDateTime.Ticks + offset.Ticks;
if ((ulong)localTicks > (ulong)s_maxDateTicks)
{
localTicks = localTicks < s_minDateTicks ? s_minDateTicks : s_maxDateTicks;
}
return new DateTimeOffset(localTicks, offset);
}
} |
If I read these stats correctly, it seems like the rare but least-expected, best-case scenario got slightly better but the most expected, normal-case scenario got slightly worse. Is that correct? |
From the test result In our cases, our app services are deployed with docker containers and the default timezone, which would be UTC timezone, we would try to use the Utc time while local time may also be used by mistake. |
Currently, we're creating a new DateTimeOffset even when the local time zone is the same as UTC
runtime/src/libraries/Common/src/System/TimeProvider.cs
Lines 47 to 68 in bb0be13
Could we use the utc now when the local time zone is UTC, possible update:
TimeSpan offset = zoneInfo.GetUtcOffset(utcDateTime); + if (offset.Ticks is 0) return utcDateTime;
The text was updated successfully, but these errors were encountered: