1*4afad4b7Schristos /* $NetBSD: time.c,v 1.1 2024/02/18 20:57:34 christos Exp $ */
2*4afad4b7Schristos
3*4afad4b7Schristos /*
4*4afad4b7Schristos * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5*4afad4b7Schristos *
6*4afad4b7Schristos * SPDX-License-Identifier: MPL-2.0
7*4afad4b7Schristos *
8*4afad4b7Schristos * This Source Code Form is subject to the terms of the Mozilla Public
9*4afad4b7Schristos * License, v. 2.0. If a copy of the MPL was not distributed with this
10*4afad4b7Schristos * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11*4afad4b7Schristos *
12*4afad4b7Schristos * See the COPYRIGHT file distributed with this work for additional
13*4afad4b7Schristos * information regarding copyright ownership.
14*4afad4b7Schristos */
15*4afad4b7Schristos
16*4afad4b7Schristos /*! \file */
17*4afad4b7Schristos
18*4afad4b7Schristos #include <ctype.h>
19*4afad4b7Schristos #include <inttypes.h>
20*4afad4b7Schristos #include <stdio.h>
21*4afad4b7Schristos #include <time.h>
22*4afad4b7Schristos
23*4afad4b7Schristos #include <isc/print.h>
24*4afad4b7Schristos #include <isc/region.h>
25*4afad4b7Schristos #include <isc/serial.h>
26*4afad4b7Schristos #include <isc/stdtime.h>
27*4afad4b7Schristos #include <isc/string.h> /* Required for HP/UX (and others?) */
28*4afad4b7Schristos #include <isc/util.h>
29*4afad4b7Schristos
30*4afad4b7Schristos #include <dns/result.h>
31*4afad4b7Schristos #include <dns/time.h>
32*4afad4b7Schristos
33*4afad4b7Schristos static const int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
34*4afad4b7Schristos
35*4afad4b7Schristos isc_result_t
dns_time64_totext(int64_t t,isc_buffer_t * target)36*4afad4b7Schristos dns_time64_totext(int64_t t, isc_buffer_t *target) {
37*4afad4b7Schristos struct tm tm;
38*4afad4b7Schristos char buf[sizeof("!!!!!!YYYY!!!!!!!!MM!!!!!!!!DD!!!!!!!!HH!!!!!!!!MM!!!!"
39*4afad4b7Schristos "!!!!SS")];
40*4afad4b7Schristos int secs;
41*4afad4b7Schristos unsigned int l;
42*4afad4b7Schristos isc_region_t region;
43*4afad4b7Schristos
44*4afad4b7Schristos /*
45*4afad4b7Schristos * Warning. Do NOT use arguments with side effects with these macros.
46*4afad4b7Schristos */
47*4afad4b7Schristos #define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
48*4afad4b7Schristos #define year_secs(y) ((is_leap(y) ? 366 : 365) * 86400)
49*4afad4b7Schristos #define month_secs(m, y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0)) * 86400)
50*4afad4b7Schristos
51*4afad4b7Schristos tm.tm_year = 70;
52*4afad4b7Schristos while (t < 0) {
53*4afad4b7Schristos if (tm.tm_year == 0) {
54*4afad4b7Schristos return (ISC_R_RANGE);
55*4afad4b7Schristos }
56*4afad4b7Schristos tm.tm_year--;
57*4afad4b7Schristos secs = year_secs(tm.tm_year + 1900);
58*4afad4b7Schristos t += secs;
59*4afad4b7Schristos }
60*4afad4b7Schristos while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
61*4afad4b7Schristos t -= secs;
62*4afad4b7Schristos tm.tm_year++;
63*4afad4b7Schristos if (tm.tm_year + 1900 > 9999) {
64*4afad4b7Schristos return (ISC_R_RANGE);
65*4afad4b7Schristos }
66*4afad4b7Schristos }
67*4afad4b7Schristos tm.tm_mon = 0;
68*4afad4b7Schristos while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
69*4afad4b7Schristos t -= secs;
70*4afad4b7Schristos tm.tm_mon++;
71*4afad4b7Schristos }
72*4afad4b7Schristos tm.tm_mday = 1;
73*4afad4b7Schristos while (86400 <= t) {
74*4afad4b7Schristos t -= 86400;
75*4afad4b7Schristos tm.tm_mday++;
76*4afad4b7Schristos }
77*4afad4b7Schristos tm.tm_hour = 0;
78*4afad4b7Schristos while (3600 <= t) {
79*4afad4b7Schristos t -= 3600;
80*4afad4b7Schristos tm.tm_hour++;
81*4afad4b7Schristos }
82*4afad4b7Schristos tm.tm_min = 0;
83*4afad4b7Schristos while (60 <= t) {
84*4afad4b7Schristos t -= 60;
85*4afad4b7Schristos tm.tm_min++;
86*4afad4b7Schristos }
87*4afad4b7Schristos tm.tm_sec = (int)t;
88*4afad4b7Schristos /* yyyy mm dd HH MM SS */
89*4afad4b7Schristos snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
90*4afad4b7Schristos tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
91*4afad4b7Schristos tm.tm_min, tm.tm_sec);
92*4afad4b7Schristos
93*4afad4b7Schristos isc_buffer_availableregion(target, ®ion);
94*4afad4b7Schristos l = strlen(buf);
95*4afad4b7Schristos
96*4afad4b7Schristos if (l > region.length) {
97*4afad4b7Schristos return (ISC_R_NOSPACE);
98*4afad4b7Schristos }
99*4afad4b7Schristos
100*4afad4b7Schristos memmove(region.base, buf, l);
101*4afad4b7Schristos isc_buffer_add(target, l);
102*4afad4b7Schristos return (ISC_R_SUCCESS);
103*4afad4b7Schristos }
104*4afad4b7Schristos
105*4afad4b7Schristos int64_t
dns_time64_from32(uint32_t value)106*4afad4b7Schristos dns_time64_from32(uint32_t value) {
107*4afad4b7Schristos isc_stdtime_t now;
108*4afad4b7Schristos int64_t start;
109*4afad4b7Schristos int64_t t;
110*4afad4b7Schristos
111*4afad4b7Schristos /*
112*4afad4b7Schristos * Adjust the time to the closest epoch. This should be changed
113*4afad4b7Schristos * to use a 64-bit counterpart to isc_stdtime_get() if one ever
114*4afad4b7Schristos * is defined, but even the current code is good until the year
115*4afad4b7Schristos * 2106.
116*4afad4b7Schristos */
117*4afad4b7Schristos isc_stdtime_get(&now);
118*4afad4b7Schristos start = (int64_t)now;
119*4afad4b7Schristos if (isc_serial_gt(value, now)) {
120*4afad4b7Schristos t = start + (value - now);
121*4afad4b7Schristos } else {
122*4afad4b7Schristos t = start - (now - value);
123*4afad4b7Schristos }
124*4afad4b7Schristos
125*4afad4b7Schristos return (t);
126*4afad4b7Schristos }
127*4afad4b7Schristos
128*4afad4b7Schristos isc_result_t
dns_time32_totext(uint32_t value,isc_buffer_t * target)129*4afad4b7Schristos dns_time32_totext(uint32_t value, isc_buffer_t *target) {
130*4afad4b7Schristos return (dns_time64_totext(dns_time64_from32(value), target));
131*4afad4b7Schristos }
132*4afad4b7Schristos
133*4afad4b7Schristos isc_result_t
dns_time64_fromtext(const char * source,int64_t * target)134*4afad4b7Schristos dns_time64_fromtext(const char *source, int64_t *target) {
135*4afad4b7Schristos int year, month, day, hour, minute, second;
136*4afad4b7Schristos int64_t value;
137*4afad4b7Schristos int secs;
138*4afad4b7Schristos int i;
139*4afad4b7Schristos
140*4afad4b7Schristos #define RANGE(min, max, value) \
141*4afad4b7Schristos do { \
142*4afad4b7Schristos if (value < (min) || value > (max)) \
143*4afad4b7Schristos return ((ISC_R_RANGE)); \
144*4afad4b7Schristos } while (0)
145*4afad4b7Schristos
146*4afad4b7Schristos if (strlen(source) != 14U) {
147*4afad4b7Schristos return (DNS_R_SYNTAX);
148*4afad4b7Schristos }
149*4afad4b7Schristos /*
150*4afad4b7Schristos * Confirm the source only consists digits. sscanf() allows some
151*4afad4b7Schristos * minor exceptions.
152*4afad4b7Schristos */
153*4afad4b7Schristos for (i = 0; i < 14; i++) {
154*4afad4b7Schristos if (!isdigit((unsigned char)source[i])) {
155*4afad4b7Schristos return (DNS_R_SYNTAX);
156*4afad4b7Schristos }
157*4afad4b7Schristos }
158*4afad4b7Schristos if (sscanf(source, "%4d%2d%2d%2d%2d%2d", &year, &month, &day, &hour,
159*4afad4b7Schristos &minute, &second) != 6)
160*4afad4b7Schristos {
161*4afad4b7Schristos return (DNS_R_SYNTAX);
162*4afad4b7Schristos }
163*4afad4b7Schristos
164*4afad4b7Schristos RANGE(0, 9999, year);
165*4afad4b7Schristos RANGE(1, 12, month);
166*4afad4b7Schristos RANGE(1, days[month - 1] + ((month == 2 && is_leap(year)) ? 1 : 0),
167*4afad4b7Schristos day);
168*4afad4b7Schristos #ifdef __COVERITY__
169*4afad4b7Schristos /*
170*4afad4b7Schristos * Use a simplified range to silence Coverity warning (in
171*4afad4b7Schristos * arithmetic with day below).
172*4afad4b7Schristos */
173*4afad4b7Schristos RANGE(1, 31, day);
174*4afad4b7Schristos #endif /* __COVERITY__ */
175*4afad4b7Schristos
176*4afad4b7Schristos RANGE(0, 23, hour);
177*4afad4b7Schristos RANGE(0, 59, minute);
178*4afad4b7Schristos RANGE(0, 60, second); /* 60 == leap second. */
179*4afad4b7Schristos
180*4afad4b7Schristos /*
181*4afad4b7Schristos * Calculate seconds from epoch.
182*4afad4b7Schristos * Note: this uses a idealized calendar.
183*4afad4b7Schristos */
184*4afad4b7Schristos value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
185*4afad4b7Schristos for (i = 0; i < (month - 1); i++) {
186*4afad4b7Schristos value += days[i] * 86400;
187*4afad4b7Schristos }
188*4afad4b7Schristos if (is_leap(year) && month > 2) {
189*4afad4b7Schristos value += 86400;
190*4afad4b7Schristos }
191*4afad4b7Schristos if (year < 1970) {
192*4afad4b7Schristos for (i = 1969; i >= year; i--) {
193*4afad4b7Schristos secs = (is_leap(i) ? 366 : 365) * 86400;
194*4afad4b7Schristos value -= secs;
195*4afad4b7Schristos }
196*4afad4b7Schristos } else {
197*4afad4b7Schristos for (i = 1970; i < year; i++) {
198*4afad4b7Schristos secs = (is_leap(i) ? 366 : 365) * 86400;
199*4afad4b7Schristos value += secs;
200*4afad4b7Schristos }
201*4afad4b7Schristos }
202*4afad4b7Schristos
203*4afad4b7Schristos *target = value;
204*4afad4b7Schristos return (ISC_R_SUCCESS);
205*4afad4b7Schristos }
206*4afad4b7Schristos
207*4afad4b7Schristos isc_result_t
dns_time32_fromtext(const char * source,uint32_t * target)208*4afad4b7Schristos dns_time32_fromtext(const char *source, uint32_t *target) {
209*4afad4b7Schristos int64_t value64;
210*4afad4b7Schristos isc_result_t result;
211*4afad4b7Schristos result = dns_time64_fromtext(source, &value64);
212*4afad4b7Schristos if (result != ISC_R_SUCCESS) {
213*4afad4b7Schristos return (result);
214*4afad4b7Schristos }
215*4afad4b7Schristos *target = (uint32_t)value64;
216*4afad4b7Schristos
217*4afad4b7Schristos return (ISC_R_SUCCESS);
218*4afad4b7Schristos }
219