diff options
author | ts2do <tsdodo@gmail.com> | 2019-07-29 01:52:17 +0300 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2019-08-03 21:55:40 +0300 |
commit | 5104fd4b2f6849f3c7549fbb66fc49c1dd6fd57b (patch) | |
tree | 2914528ce134ab2ede718689ae271c0c597e1f49 /netcore | |
parent | 6345e1810869d8feb742473393e4fc0b7cd1c01a (diff) |
Eliminate extra time zone conversions in DateTimeOffset (#25658)
* Eliminate extra time zone conversions in DateTimeOffset
Revised members dealing specifically with local time by removing delegation to DateTime methods to avoid an extra call to TimeZoneInfo to determine offset:
Now
FromFileTime(long)
ToLocalTime()
* Fix DateTimeOffset.ToLocalTime(bool)
* Consolidate conversion from UTC DateTime to Local DateTimeOffset
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Diffstat (limited to 'netcore')
-rw-r--r-- | netcore/System.Private.CoreLib/shared/System/DateTimeOffset.cs | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/netcore/System.Private.CoreLib/shared/System/DateTimeOffset.cs b/netcore/System.Private.CoreLib/shared/System/DateTimeOffset.cs index 6cc68de64d4..f3a604ca7cd 100644 --- a/netcore/System.Private.CoreLib/shared/System/DateTimeOffset.cs +++ b/netcore/System.Private.CoreLib/shared/System/DateTimeOffset.cs @@ -176,7 +176,7 @@ namespace System { get { - return new DateTimeOffset(DateTime.Now); + return ToLocalTime(DateTime.UtcNow, true); } } @@ -559,7 +559,7 @@ namespace System // public static DateTimeOffset FromFileTime(long fileTime) { - return new DateTimeOffset(DateTime.FromFileTime(fileTime)); + return ToLocalTime(DateTime.FromFileTimeUtc(fileTime), true); } public static DateTimeOffset FromUnixTimeSeconds(long seconds) @@ -792,7 +792,22 @@ namespace System internal DateTimeOffset ToLocalTime(bool throwOnOverflow) { - return new DateTimeOffset(UtcDateTime.ToLocalTime(throwOnOverflow)); + return ToLocalTime(UtcDateTime, throwOnOverflow); + } + + private static DateTimeOffset ToLocalTime(DateTime utcDateTime, bool throwOnOverflow) + { + TimeSpan offset = TimeZoneInfo.GetLocalUtcOffset(utcDateTime, TimeZoneInfoOptions.NoThrowOnInvalidTime); + long localTicks = utcDateTime.Ticks + offset.Ticks; + if (localTicks < DateTime.MinTicks || localTicks > DateTime.MaxTicks) + { + if (throwOnOverflow) + throw new ArgumentException(SR.Arg_ArgumentOutOfRangeException); + + localTicks = localTicks < DateTime.MinTicks ? DateTime.MinTicks : DateTime.MaxTicks; + } + + return new DateTimeOffset(localTicks, offset); } public override string ToString() |