1 /// Utilties related to epoch time
2 module dcord.util.time;
3 
4 version (Posix) {
5   import core.sys.posix.sys.time;
6 
7   /// Returns UTC time in microseconds
8   long getEpochTimeMicro() {
9     timeval t;
10     gettimeofday(&t, null);
11     return 10_000_00 * t.tv_sec + t.tv_usec;
12   }
13 
14   /// Returns UTC time in milliseconds.
15   long getEpochTimeMilli() {
16     timeval t;
17     gettimeofday(&t, null);
18     return t.tv_sec * 1000 + t.tv_usec / 1000;
19   }
20 
21   /// Returns UTC time in seconds.
22   long getEpochTime() {
23     return getEpochTimeMilli() / 1000;
24   }
25 }
26 
27 version (Windows) {
28   import core.sys.windows.winbase;
29 
30   /// Returns UTC time in milliseconds.
31   long getEpochTimeMilli() {
32     SYSTEMTIME systemTime;
33     GetSystemTime(&systemTime);
34     FILETIME fileTime;
35     SystemTimeToFileTime( &systemTime, &fileTime );
36     long fileTimeNano100;
37     fileTimeNano100 = ((cast(long)fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime;
38     long posixTime = fileTimeNano100/10_000 - 116_444_736_000_00;
39     return posixTime;
40   }
41 
42   /// Returns UTC time in seconds.
43   long getEpochTime() {
44     return getEpochTimeMilli() / 1000;
45   }
46 }