-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnixTime.cs
42 lines (35 loc) · 1.16 KB
/
UnixTime.cs
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
using System;
using System.Collections.Generic;
using System.Text;
namespace UnifiProtect_ClipSaver
{
public static class UnixTime
{
private static readonly DateTime UnixEpoch =
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long GetUnixTime(this DateTime datetime)
{
return (long)datetime.Subtract(UnixEpoch).TotalSeconds;
}
public static long GetUnixTimeMillis(this DateTime datetime)
{
return (long)datetime.Subtract(UnixEpoch).TotalMilliseconds;
}
public static long GetCurrentUnixTimestampMillis()
{
return (long)(DateTime.UtcNow - UnixEpoch).TotalMilliseconds;
}
public static DateTime DateTimeFromUnixTimestampMillis(long millis)
{
return UnixEpoch.AddMilliseconds(millis);
}
public static long GetCurrentUnixTimestampSeconds()
{
return (long)(DateTime.UtcNow - UnixEpoch).TotalSeconds;
}
public static DateTime DateTimeFromUnixTimestampSeconds(long seconds)
{
return UnixEpoch.AddSeconds(seconds);
}
}
}