Skip to content

Commit

Permalink
+ DateTimeOffset polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawkynt committed Nov 18, 2024
1 parent 9ce2495 commit e30eadd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
56 changes: 56 additions & 0 deletions Backports/Features/ToUnixTimeMilliseconds/System/DateTimeOffset.cs
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
3 changes: 3 additions & 0 deletions Backports/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ __Note__: Performance is not a primary concern here. This focuses mainly on func
* System.Collections.Generic.Stack
* bool [TryPop](https://learn.microsoft.com/dotnet/api/system.collections.generic.stack-1.trypop)&lt;TItem&gt;(this Stack&lt;TItem&gt; @this, out TItem result)
* bool [TryPeek](https://learn.microsoft.com/dotnet/api/system.collections.generic.stack-1.trypeek)&lt;TItem&gt;(this Stack&lt;TItem&gt; @this, out TItem result)
* System.DateTimeOffset
* long [ToUnixTimeMilliseconds](https://learn.microsoft.com/dotnet/api/system.datetimeoffset.tounixtimemilliseconds)(this DateTimeOffset @this)
* long [ToUnixTimeSeconds](https://learn.microsoft.com/dotnet/api/system.datetimeoffset.tounixtimeseconds)(this DateTimeOffset @this)
* System.Diagnostics
* void [Restart](https://learn.microsoft.com/dotnet/api/system.diagnostics.stopwatch.restart)(this Stopwatch @this)
* System.Enum
Expand Down
10 changes: 10 additions & 0 deletions Corlib.Extensions/System/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
namespace System;

public static partial class DateTimeExtensions {
private const long _TICKS_PER_MILLISECOND = 10000;

/// <summary>
/// Returns the end of the day of the given date.
/// </summary>
Expand Down Expand Up @@ -163,4 +165,12 @@ public static IEnumerable<DateTime> DaysTill(this DateTime @this, DateTime endDa
public static DateTime SubstractWeeks(this DateTime @this, int weeks) => @this.AddWeeks(-weeks);
public static DateTime SubstractMonths(this DateTime @this, int months) => @this.AddMonths(-months);
public static DateTime SubstractYears(this DateTime @this, int value) => @this.AddYears(-value);

public static long AsUnixTicksUtc(this DateTime @this) => ((DateTimeOffset)@this.ToUniversalTime()).ToUnixTimeMilliseconds() * DateTimeExtensions._TICKS_PER_MILLISECOND;
public static long AsUnixMillisecondsUtc(this DateTime @this) => ((DateTimeOffset)@this.ToUniversalTime()).ToUnixTimeMilliseconds();

public static DateTime FromUnixTicks(long ticks, DateTimeKind kind = DateTimeKind.Unspecified) => DateTimeExtensions._CreateUnixEpochWithKind(kind).AddTicks(ticks);
public static DateTime FromUnixSeconds(long seconds, DateTimeKind kind = DateTimeKind.Unspecified) => DateTimeExtensions._CreateUnixEpochWithKind(kind).AddSeconds(seconds);

private static DateTime _CreateUnixEpochWithKind(DateTimeKind kind) => new(1970, 1, 1, 0, 0, 0, kind);
}
Loading

0 comments on commit e30eadd

Please sign in to comment.