1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:18:47 +00:00

🔨 Use DateTimeOffset to convert Unix timestamps

This commit is contained in:
Oliver Booth 2019-11-16 20:16:47 +00:00
parent 0344851c32
commit 10fea0b372
No known key found for this signature in database
GPG Key ID: 4B0992B2602C3778
4 changed files with 10 additions and 33 deletions

View File

@ -11,11 +11,6 @@
/// </summary> /// </summary>
public static class DateTimeExtensions 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> /// <summary>
/// Calculates someone's age based on a date of birth. /// Calculates someone's age based on a date of birth.
/// </summary> /// </summary>
@ -146,8 +141,8 @@
/// <returns>Returns a Unix timestamp representing the provided <see cref="DateTime"/>.</returns> /// <returns>Returns a Unix timestamp representing the provided <see cref="DateTime"/>.</returns>
public static long ToUnixTimeStamp(this DateTime time, bool useMillis = false) public static long ToUnixTimeStamp(this DateTime time, bool useMillis = false)
{ {
TimeSpan difference = time - UnixEpoch; DateTimeOffset offset = time;
return (long)(useMillis ? difference.TotalMilliseconds : difference.TotalSeconds); return useMillis ? offset.ToUnixTimeSeconds() : offset.ToUnixTimeMilliseconds();
} }
/// <summary> /// <summary>

View File

@ -31,19 +31,6 @@
public static double DegreesToRadians(this double angle) => public static double DegreesToRadians(this double angle) =>
Math.PI * angle / 180.0; 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> /// <summary>
/// Converts the <see cref="Double"/> to a <see cref="Byte"/>[]. /// Converts the <see cref="Double"/> to a <see cref="Byte"/>[].
/// </summary> /// </summary>

View File

@ -82,8 +82,14 @@
/// to <see langword="false"/>.</param> /// to <see langword="false"/>.</param>
/// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix /// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix
/// epoch.</returns> /// epoch.</returns>
public static DateTime FromUnixTimestamp(this long timestamp, bool isMillis = false) => public static DateTime FromUnixTimestamp(this long timestamp, bool isMillis = false)
((double)timestamp).FromUnixTimestamp(isMillis); {
DateTimeOffset offset = isMillis
? DateTimeOffset.FromUnixTimeMilliseconds(timestamp)
: DateTimeOffset.FromUnixTimeSeconds(timestamp);
return offset.DateTime;
}
/// <summary> /// <summary>
/// Converts the <see cref="UInt64"/> to a <see cref="Byte"/>[]. /// Converts the <see cref="UInt64"/> to a <see cref="Byte"/>[].

View File

@ -31,17 +31,6 @@
public static float DegreesToRadians(this float angle) => public static float DegreesToRadians(this float angle) =>
(float)((double)angle).DegreesToRadians(); (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> /// <summary>
/// Converts the <see cref="Single"/> to a <see cref="Byte"/>[]. /// Converts the <see cref="Single"/> to a <see cref="Byte"/>[].
/// </summary> /// </summary>