-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.c
129 lines (111 loc) · 2.9 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
/*
* Copyright (c) 2014-2020 by Farsight Security, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#include <sys/types.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "defs.h"
#include "pdns.h"
#include "time.h"
#include "globals.h"
#include "ns_ttl.h"
/* time_cmp -- compare two absolute timestamps, give -1, 0, or 1.
*/
int
time_cmp(u_long a, u_long b) {
if (a < b)
return (-1);
if (a > b)
return (1);
return (0);
}
/* time_str -- format one (possibly relative) timestamp (returns static string)
*/
const char *
time_str(u_long x) {
static char ret[sizeof "yyyy-mm-ddThh:mm:ssZ"];
if (x == 0) {
strcpy(ret, "0");
} else {
time_t t = (time_t)x;
struct tm result, *y = gmtime_r(&t, &result);
strftime(ret, sizeof ret, "%F %T", y);
}
return ret;
}
/* timeval_str -- format one timeval (NULL means current time)
*
* returns static string. always uses GMT.
*
* output format: yyyy-mm-dd hh:mm:ss.fff[fff]
*/
const char *
timeval_str(const struct timeval *src, bool milliseconds) {
static char ret[sizeof "yyyy-mm-dd hh:mm:ss.ffffff"];
char *dst;
struct timeval now;
if (src == NULL) {
gettimeofday(&now, NULL);
src = &now;
}
time_t t = (time_t)src->tv_sec;
long usecs = (long)src->tv_usec;
/* in the unlikely event that usecs is more than one second, move the excess time to the seconds value */
long excess_seconds = usecs / 1000000;
t += excess_seconds;
usecs -= excess_seconds * 1000000;
struct tm result, *y = gmtime_r(&t, &result);
dst = ret + strftime(ret, sizeof ret, "%F %T", y);
if (milliseconds)
sprintf(dst, ".%03ld", usecs / 1000);
else
sprintf(dst, ".%06ld", usecs);
return ret;
}
/* time_get -- parse and return one (possibly relative) timestamp.
*/
int
time_get(const char *src, u_long *dst) {
struct tm tt;
long long ll;
u_long t;
char *ep;
memset(&tt, 0, sizeof tt);
if (((ep = strptime(src, "%F %T", &tt)) != NULL && *ep == '\0') ||
((ep = strptime(src, "%F", &tt)) != NULL && *ep == '\0'))
{
*dst = (u_long)(timegm(&tt));
return (1);
}
ll = strtoll(src, &ep, 10);
if (*src != '\0' && *ep == '\0') {
if (ll < 0)
*dst = (u_long)startup_time.tv_sec -
(u_long)imaxabs(ll);
else
*dst = (u_long)ll;
return (1);
}
if (ns_parse_ttl(src, &t) == 0) {
*dst = (u_long)startup_time.tv_sec - t;
return (1);
}
return (0);
}