-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDateTimeExtensions.cs
More file actions
27 lines (25 loc) · 925 Bytes
/
DateTimeExtensions.cs
File metadata and controls
27 lines (25 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
namespace OpenWeatherMapSharp.Utils
{
/// <summary>
/// Provides extension methods for converting
/// a <see cref="DateTime"/> to a Unix timestamps.
/// </summary>
internal static class DateTimeExtensions
{
/// <summary>
/// Converts a DateTime object to a Unix timestamp (seconds since 1970-01-01T00:00:00Z).
/// </summary>
/// <param name="dateTime">The DateTime to convert.
/// Should be in UTC or convertible to UTC.</param>
/// <returns>The Unix timestamp in seconds.</returns>
public static long ToUnixTimestamp(this DateTime dateTime)
{
// Ensure the DateTime is in UTC
var utcDateTime = dateTime.Kind == DateTimeKind.Utc
? dateTime
: dateTime.ToUniversalTime();
return new DateTimeOffset(utcDateTime).ToUnixTimeSeconds();
}
}
}