mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-12 23:55:41 +00:00
🔨 Use DateTimeOffset to convert Unix timestamps
This commit is contained in:
parent
0344851c32
commit
10fea0b372
@ -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>
|
||||||
|
@ -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>
|
||||||
|
@ -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"/>[].
|
||||||
|
@ -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>
|
||||||
|
Loading…
Reference in New Issue
Block a user