summaryrefslogtreecommitdiffstats
path: root/src/common/impl/time.c
blob: c60d1fecee3fd220130a927bd149dce66c4bbaf6 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "common/time.h"

#include <stdio.h>

char ffTimeInternalBuffer[64]; // Reduce memory usage and prevent redundant allocations

#ifdef _WIN32
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wformat"
#endif

const char* ffTimeToFullStr(uint64_t msec) {
    if (msec == 0) {
        return "";
    }
    time_t tsec = (time_t) (msec / 1000);
    const struct tm* tm = localtime(&tsec);

    uint32_t len = 0;
    len += (uint32_t) strftime(ffTimeInternalBuffer, ARRAY_SIZE(ffTimeInternalBuffer) - len, "%FT%T", tm);
    len += (uint32_t) snprintf(ffTimeInternalBuffer + len, ARRAY_SIZE(ffTimeInternalBuffer) - len, ".%03u", (unsigned) (msec % 1000));
    len += (uint32_t) strftime(ffTimeInternalBuffer + len, ARRAY_SIZE(ffTimeInternalBuffer) - len, "%z", tm);
    return ffTimeInternalBuffer;
}

const char* ffTimeToShortStr(uint64_t msec) {
    if (msec == 0) {
        return "";
    }
    time_t tsec = (time_t) (msec / 1000);

    strftime(ffTimeInternalBuffer, ARRAY_SIZE(ffTimeInternalBuffer), "%F %T", localtime(&tsec));
    return ffTimeInternalBuffer;
}

const char* ffTimeToTimeStr(uint64_t msec) {
    if (msec == 0) {
        return "";
    }
    time_t tsec = (time_t) (msec / 1000);

    uint32_t len = (uint32_t) strftime(ffTimeInternalBuffer, ARRAY_SIZE(ffTimeInternalBuffer), "%T", localtime(&tsec));
    sprintf(ffTimeInternalBuffer + len, ".%03u", (unsigned) (msec % 1000));
    return ffTimeInternalBuffer;
}

#ifdef _WIN32
    #pragma GCC diagnostic pop
#endif

FFTimeGetAgeResult ffTimeGetAge(uint64_t birthMs, uint64_t nowMs) {
    FFTimeGetAgeResult result = {};
    if (__builtin_expect(birthMs == 0 || nowMs < birthMs, 0)) {
        return result;
    }

    time_t birth_s = (time_t) (birthMs / 1000);
    struct tm birth_tm;
#ifdef _WIN32
    localtime_s(&birth_tm, &birth_s);
#else
    localtime_r(&birth_s, &birth_tm);
#endif

    time_t now_s = (time_t) (nowMs / 1000);
    struct tm now_tm;
#ifdef _WIN32
    localtime_s(&now_tm, &now_s);
#else
    localtime_r(&now_s, &now_tm);
#endif

    result.years = (uint32_t) (now_tm.tm_year - birth_tm.tm_year);
    if (now_tm.tm_yday < birth_tm.tm_yday) {
        result.years--;
    }

    birth_tm.tm_year += (int) result.years;
    birth_s = mktime(&birth_tm);
    uint32_t diff_s = (uint32_t) (now_s - birth_s);
    result.daysOfYear = diff_s / (24 * 60 * 60);

    birth_tm.tm_year += 1;
    result.yearsFraction = (double) diff_s / (double) (mktime(&birth_tm) - birth_s) + result.years;

    return result;
}

#ifdef _WIN32
double ffQpcMultiplier;

__attribute__((constructor)) static void ffTimeInitQpcMultiplier(void) {
    LARGE_INTEGER frequency;
    RtlQueryPerformanceFrequency(&frequency);
    ffQpcMultiplier = 1000. / (double) frequency.QuadPart;
}
#endif