-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Backports/Features/ToUnixTimeMilliseconds/System/DateTimeOffset.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// This file is part of Hawkynt's .NET Framework extensions. | ||
// | ||
// Hawkynt's .NET Framework extensions are free software: | ||
// you can redistribute and/or modify it under the terms | ||
// given in the LICENSE file. | ||
// | ||
// Hawkynt's .NET Framework extensions is distributed in the hope that | ||
// it will be useful, but WITHOUT ANY WARRANTY without even the implied | ||
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
// See the LICENSE file for more details. | ||
// | ||
// You should have received a copy of the License along with Hawkynt's | ||
// .NET Framework extensions. If not, see | ||
// <https://github.com/Hawkynt/C--FrameworkExtensions/blob/master/LICENSE>. | ||
// | ||
|
||
#if !SUPPORTS_TO_UNIX_TIME_MILLISECONDS | ||
|
||
using System.Diagnostics; | ||
#if SUPPORTS_INLINING | ||
using System.Runtime.CompilerServices; | ||
#endif | ||
|
||
namespace System; | ||
|
||
public static partial class DateTimeOffsetPolyfills { | ||
|
||
private const int DaysPerYear = 365; | ||
private const int DaysPer4Years = DaysPerYear * 4 + 1; // 1461 | ||
private const int DaysPer100Years = DaysPer4Years * 25 - 1; // 36524 | ||
private const int DaysPer400Years = DaysPer100Years * 4 + 1; // 146097 | ||
private const int DaysTo1970 = DaysPer400Years * 4 + DaysPer100Years * 3 + DaysPer4Years * 17 + DaysPerYear; // 719,162 | ||
private const long UnixEpochTicks = TimeSpan.TicksPerDay * DaysTo1970; // 621,355,968,000,000,000 | ||
private const long UnixEpochSeconds = UnixEpochTicks / TimeSpan.TicksPerSecond; // 62,135,596,800 | ||
private const long UnixEpochMilliseconds = UnixEpochTicks / TimeSpan.TicksPerMillisecond; // 62,135,596,800,000 | ||
|
||
/// <summary>Returns the number of milliseconds that have elapsed since 1970-01-01T00:00:00.000Z.</summary> | ||
/// <returns>The number of milliseconds that have elapsed since 1970-01-01T00:00:00.000Z.</returns> | ||
[DebuggerStepThrough] | ||
#if SUPPORTS_INLINING | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#endif | ||
public static long ToUnixTimeMilliseconds(this DateTimeOffset @this) => @this.UtcDateTime.Ticks / TimeSpan.TicksPerMillisecond - UnixEpochMilliseconds; | ||
|
||
/// <summary>Returns the number of seconds that have elapsed since 1970-01-01T00:00:00.000Z.</summary> | ||
/// <returns>The number of seconds that have elapsed since 1970-01-01T00:00:00.000Z.</returns> | ||
[DebuggerStepThrough] | ||
#if SUPPORTS_INLINING | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#endif | ||
public static long ToUnixTimeSeconds(this DateTimeOffset @this) => @this.UtcDateTime.Ticks / TimeSpan.TicksPerSecond - UnixEpochSeconds; | ||
|
||
} | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.