mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 23:25:43 +00:00
🔨 Use DateTimeOffset to convert Unix timestamps
This commit is contained in:
parent
0344851c32
commit
10fea0b372
@ -11,11 +11,6 @@
|
||||
/// </summary>
|
||||
public static class DateTimeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the Unix epoch - midnight on January 1, 1970.
|
||||
/// </summary>
|
||||
public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0);
|
||||
|
||||
/// <summary>
|
||||
/// Calculates someone's age based on a date of birth.
|
||||
/// </summary>
|
||||
@ -146,8 +141,8 @@
|
||||
/// <returns>Returns a Unix timestamp representing the provided <see cref="DateTime"/>.</returns>
|
||||
public static long ToUnixTimeStamp(this DateTime time, bool useMillis = false)
|
||||
{
|
||||
TimeSpan difference = time - UnixEpoch;
|
||||
return (long)(useMillis ? difference.TotalMilliseconds : difference.TotalSeconds);
|
||||
DateTimeOffset offset = time;
|
||||
return useMillis ? offset.ToUnixTimeSeconds() : offset.ToUnixTimeMilliseconds();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -31,19 +31,6 @@
|
||||
public static double DegreesToRadians(this double angle) =>
|
||||
Math.PI * angle / 180.0;
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="Double"/> to a <see cref="DateTime"/> treating it as a Unix timestamp.
|
||||
/// </summary>
|
||||
/// <param name="timestamp">The timestamp.</param>
|
||||
/// <param name="isMillis">Optional. Whether or not the input value should be treated as milliseconds. Defaults
|
||||
/// to <see langword="false"/>.</param>
|
||||
/// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix
|
||||
/// epoch.</returns>
|
||||
public static DateTime FromUnixTimestamp(this double timestamp, bool isMillis = false) =>
|
||||
isMillis
|
||||
? DateTimeExtensions.UnixEpoch.AddMilliseconds(timestamp)
|
||||
: DateTimeExtensions.UnixEpoch.AddSeconds(timestamp);
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="Double"/> to a <see cref="Byte"/>[].
|
||||
/// </summary>
|
||||
|
@ -82,8 +82,14 @@
|
||||
/// to <see langword="false"/>.</param>
|
||||
/// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix
|
||||
/// epoch.</returns>
|
||||
public static DateTime FromUnixTimestamp(this long timestamp, bool isMillis = false) =>
|
||||
((double)timestamp).FromUnixTimestamp(isMillis);
|
||||
public static DateTime FromUnixTimestamp(this long timestamp, bool isMillis = false)
|
||||
{
|
||||
DateTimeOffset offset = isMillis
|
||||
? DateTimeOffset.FromUnixTimeMilliseconds(timestamp)
|
||||
: DateTimeOffset.FromUnixTimeSeconds(timestamp);
|
||||
|
||||
return offset.DateTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="UInt64"/> to a <see cref="Byte"/>[].
|
||||
|
@ -31,17 +31,6 @@
|
||||
public static float DegreesToRadians(this float angle) =>
|
||||
(float)((double)angle).DegreesToRadians();
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="Single"/> to a <see cref="DateTime"/> treating it as a Unix timestamp.
|
||||
/// </summary>
|
||||
/// <param name="timestamp">The timestamp.</param>
|
||||
/// <param name="isMillis">Optional. Whether or not the input value should be treated as milliseconds. Defaults
|
||||
/// to <see langword="false"/>.</param>
|
||||
/// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix
|
||||
/// epoch.</returns>
|
||||
public static DateTime FromUnixTimestamp(this float timestamp, bool isMillis = false) =>
|
||||
((double)timestamp).FromUnixTimestamp(isMillis);
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="Single"/> to a <see cref="Byte"/>[].
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user