aboutsummaryrefslogtreecommitdiffstats
path: root/util/datetime/systime.h
blob: 491d36e802a54b750d71c3347ba6f9d601cdbadd (plain) (blame)
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
46
47
#pragma once

#include <util/system/platform.h>
#include <util/generic/string.h>

#include <ctime>

// timegm and gmtime_r versions that don't need access to filesystem or a big stack
time_t TimeGM(const struct tm* t);
struct tm* GmTimeR(const time_t* timer, struct tm* tmbuf);
// safe version of ctime, convinient version of ctime_r
TString CTimeR(const time_t* timer);

#ifdef _win_
    #include <util/system/winint.h>
    #include <winsock2.h>

void FileTimeToTimeval(const FILETIME* ft, struct timeval* tv);

// obtains the current time, expressed as seconds and microseconds since 00:00 UTC, January 1, 1970
int gettimeofday(struct timeval* tp, void*);

// thou should not mix these with non-_r functions
tm* localtime_r(const time_t* clock, tm* result);
tm* gmtime_r(const time_t* clock, tm* result);
char* ctime_r(const time_t* clock, char* buf);

inline time_t timegm(struct tm* t) {
    return TimeGM(t);
}

char* strptime(const char* buf, const char* fmt, struct tm* tm); // strptime.cpp
#else
    #include <sys/time.h>
#endif

#ifndef timersub
    #define timersub(tvp, uvp, vvp)                           \
        do {                                                  \
            (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;    \
            (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
            if ((vvp)->tv_usec < 0) {                         \
                (vvp)->tv_sec--;                              \
                (vvp)->tv_usec += 1000000;                    \
            }                                                 \
        } while (0)
#endif