-
Notifications
You must be signed in to change notification settings - Fork 36
/
time.c
192 lines (166 loc) · 4.09 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/* The tai and taia routines are based on djb's libtai which is placed in the
* public domain. See http://cr.yp.to/libtai.html for details. The taia code
* was slightly modified to only take into account nano-second based timing.
* All the atto-second related code was removed to save some space when packing
* taia structures on disk. */
#define _XOPEN_SOURCE /* needed for strptime */
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include "time.h"
void
tai_now(struct tai * t)
{
tai_unix(t, time((time_t *)0));
}
void
tai_pack(char * s, const struct tai * t)
{
uint64_t x;
x = t->x;
s[7] = x & 255; x >>= 8;
s[6] = x & 255; x >>= 8;
s[5] = x & 255; x >>= 8;
s[4] = x & 255; x >>= 8;
s[3] = x & 255; x >>= 8;
s[2] = x & 255; x >>= 8;
s[1] = x & 255; x >>= 8;
s[0] = x;
}
void
tai_unpack(const char * s, struct tai * t)
{
uint64_t x;
x = (unsigned char)s[0];
x <<= 8; x += (unsigned char) s[1];
x <<= 8; x += (unsigned char) s[2];
x <<= 8; x += (unsigned char) s[3];
x <<= 8; x += (unsigned char) s[4];
x <<= 8; x += (unsigned char) s[5];
x <<= 8; x += (unsigned char) s[6];
x <<= 8; x += (unsigned char) s[7];
t->x = x;
}
void
taia_now(struct taia * t)
{
struct timeval now;
gettimeofday(&now, (struct timezone *) 0);
tai_unix(&t->sec, now.tv_sec);
t->nano = 1000 * now.tv_usec + 500;
}
void
taia_pack(char * s, const struct taia * t)
{
unsigned long x;
tai_pack(s, &t->sec);
s += 8;
x = t->nano;
s[3] = x & 255; x >>= 8;
s[2] = x & 255; x >>= 8;
s[1] = x & 255; x >>= 8;
s[0] = x;
}
void
taia_unpack(const char * s, struct taia * t)
{
unsigned long x;
tai_unpack(s, &t->sec);
s += 8;
x = (unsigned char)s[0];
x <<= 8; x += (unsigned char)s[1];
x <<= 8; x += (unsigned char)s[2];
x <<= 8; x += (unsigned char)s[3];
t->nano = x;
}
int
taia_less(struct taia * t, struct taia * u)
{
if (t->sec.x < u->sec.x) return 1;
if (t->sec.x > u->sec.x) return 0;
return t->nano < u->nano;
}
int
taia_leq(struct taia * t, struct taia * u)
{
if (t->sec.x <= u->sec.x) return 1;
if (t->sec.x > u->sec.x) return 0;
return t->nano <= u->nano;
}
void
taia_diff(struct taia * t, struct taia * u, struct taia * res)
{
res->sec.x = u->sec.x - t->sec.x;
res->nano = u->nano - t->nano;
}
int
timestr_parse(const char * input, struct taia * res, struct taia * b)
{
struct taia base, * pbase;
const char * p;
uint32_t w, d, h, m, s;
unsigned int n;
if (!input || !res) return -1;
if (b) pbase = b;
else {
taia_now(&base);
pbase = &base;
}
p = input;
w = d = h = m = s = 0;
next:
n = 0;
while (*p && (*p >= '0' && *p <= '9')) {
n *= 10;
n += (*p-'0');
p++;
}
if (!*p) return -1;
switch (*p) {
case 'w':
if ((d*h*m*s)!=0) return -1;
w = n*604800;
break;
case 'd':
if ((h*m*s)!=0) return -1;
d = n*86400;
break;
case 'h':
if ((m*s)!=0) return -1;
h = n*3600;
break;
case 'm':
if (s) return -1;
m = n*60;
break;
case 's':
s = n;
break;
default:
return -1;
}
if (*++p) goto next;
res->sec.x = pbase->sec.x - (w+d+h+m+s);
res->nano = pbase->nano;
return 0;
}