forked from linguanostra/GoogleMapsAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeExtensions.cs
More file actions
45 lines (33 loc) · 1.14 KB
/
Copy pathDateTimeExtensions.cs
File metadata and controls
45 lines (33 loc) · 1.14 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
namespace GoogleMapsAPI.NET.Extensions
{
/// <summary>
/// Date/Time extensions
/// </summary>
public static class DateTimeExtensions
{
#region Extension methods
/// <summary>
/// Get the number of seconds since Unix epoch
/// </summary>
/// <param name="value">Value</param>
/// <returns>Result value</returns>
public static long SecondsSinceEpoch(this DateTime value)
{
return (long)(value.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
}
/// <summary>
/// Convert numeric value of seconds since Unix epoch to DateTime
/// </summary>
/// <param name="seconds">Seconds since Unix epoch</param>
/// <returns>Result value</returns>
public static DateTime ToDateTimeFromEpochSeconds(this long seconds)
{
// Get Unix epoch date
var epochDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
// Add seconds
return epochDate.AddSeconds(seconds);
}
#endregion
}
}