🔨 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>
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>

View File

@ -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>

View File

@ -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"/>[].

View File

@ -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>