-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.c
131 lines (115 loc) · 3.24 KB
/
time.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <assert.h>
#include <string.h>
#include "time.h"
/*
* Rough millisecond estimations.
*
* We don't need accurate timekeeping since we are only
* concerned with average durations between path transitions, so
* we can take this faster shortcut with manual parsing.
*/
/*
* TODO: refactor to use common functions
*/
static const char ctoi[256] = {
['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, ['4'] = 4,
['5'] = 5, ['6'] = 6, ['7'] = 7, ['8'] = 8, ['9'] = 9
};
#define MS_IN_YEAR 31104000000ULL
#define MS_IN_MONTH 2592000000ULL
#define MS_IN_DAY 86400000ULL
#define MS_IN_HOUR 3600000ULL
#define MS_IN_MIN 60000ULL
#define MS_IN_SEC 1000ULL
uint64_t
rfc3339_to_ms(const char *s)
{
uint64_t year = (ctoi[(int)s[0]] * 1000
+ ctoi[(int)s[1]] * 100
+ ctoi[(int)s[2]] * 10
+ ctoi[(int)s[3]])
- 1970;
s += 5; // Skip '-'
uint64_t month = ctoi[(int)s[0]] * 10
+ ctoi[(int)s[1]];
s += 3; // Skip '-'
uint64_t day = ctoi[(int)s[0]] * 10 + ctoi[(int)s[1]];
s += 3; // Skip 'T'
uint64_t hour = ctoi[(int)s[0]] * 10 + ctoi[(int)s[1]];
s += 3; // Skip ':'
uint64_t min = ctoi[(int)s[0]] * 10 + ctoi[(int)s[1]];
s += 3; // Skip ':'
uint64_t sec = ctoi[(int)s[0]] * 10 + ctoi[(int)s[1]];
s += 3; // Skip '.'
uint64_t ms = ctoi[(int)s[0]] * 100
+ ctoi[(int)s[1]] * 10
+ ctoi[(int)s[2]];
return year * MS_IN_YEAR
+ month * MS_IN_MONTH
+ day * MS_IN_DAY
+ hour * MS_IN_HOUR
+ min * MS_IN_MIN
+ sec * MS_IN_SEC
+ ms;
}
uint64_t
rfc3339_no_ms_to_ms(const char *s)
{
uint64_t year = (ctoi[(int)s[0]] * 1000
+ ctoi[(int)s[1]] * 100
+ ctoi[(int)s[2]] * 10
+ ctoi[(int)s[3]])
- 1970;
s += 5; // Skip '-'
uint64_t month = ctoi[(int)s[0]] * 10
+ ctoi[(int)s[1]];
s += 3; // Skip '-'
uint64_t day = ctoi[(int)s[0]] * 10 + ctoi[(int)s[1]];
s += 3; // Skip 'T'
uint64_t hour = ctoi[(int)s[0]] * 10 + ctoi[(int)s[1]];
s += 3; // Skip ':'
uint64_t min = ctoi[(int)s[0]] * 10 + ctoi[(int)s[1]];
s += 3; // Skip ':'
uint64_t sec = ctoi[(int)s[0]] * 10 + ctoi[(int)s[1]];
return year * MS_IN_YEAR
+ month * MS_IN_MONTH
+ day * MS_IN_DAY
+ hour * MS_IN_HOUR
+ min * MS_IN_MIN
+ sec * MS_IN_SEC;
}
uint64_t
date_to_ms(const char *s)
{
assert(s != NULL);
assert(strnlen(s, 10) == 10);
uint64_t year = (ctoi[(int)s[0]] * 1000
+ ctoi[(int)s[1]] * 100
+ ctoi[(int)s[2]] * 10
+ ctoi[(int)s[3]])
- 1970;
s += 5; // Skip '-'
uint64_t month = ctoi[(int)s[0]] * 10
+ ctoi[(int)s[1]];
s += 3; // Skip '-'
uint64_t day = ctoi[(int)s[0]] * 10 + ctoi[(int)s[1]];
s += 2;
uint64_t ms = year * MS_IN_YEAR
+ month * MS_IN_MONTH
+ day * MS_IN_DAY;
return ms;
}
uint64_t
time_to_ms(const char *s)
{
uint64_t hour = ctoi[(int)s[0]] * 10 + ctoi[(int)s[1]];
s += 3; // Skip ':'
uint64_t min = ctoi[(int)s[0]] * 10 + ctoi[(int)s[1]];
s += 3; // Skip ':'
uint64_t sec = ctoi[(int)s[0]] * 10 + ctoi[(int)s[1]];
s += 2;
uint64_t ms = hour * MS_IN_HOUR
+ min * MS_IN_MIN
+ sec * MS_IN_SEC;
return ms;
}