15185a700Sflorian /*
25185a700Sflorian * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
35185a700Sflorian *
45185a700Sflorian * Permission to use, copy, modify, and/or distribute this software for any
55185a700Sflorian * purpose with or without fee is hereby granted, provided that the above
65185a700Sflorian * copyright notice and this permission notice appear in all copies.
75185a700Sflorian *
85185a700Sflorian * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
95185a700Sflorian * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
105185a700Sflorian * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
115185a700Sflorian * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
125185a700Sflorian * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
135185a700Sflorian * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
145185a700Sflorian * PERFORMANCE OF THIS SOFTWARE.
155185a700Sflorian */
165185a700Sflorian
17*3ec2dc6fSflorian /* $Id: dns_time.c,v 1.7 2020/04/02 16:57:45 florian Exp $ */
185185a700Sflorian
195185a700Sflorian /*! \file */
205185a700Sflorian
215185a700Sflorian #include <stdio.h>
22*3ec2dc6fSflorian #include <string.h>
235185a700Sflorian #include <time.h>
245185a700Sflorian
255185a700Sflorian #include <isc/region.h>
265185a700Sflorian #include <isc/serial.h>
274465bcfbSjsg #include <isc/result.h>
285185a700Sflorian
295185a700Sflorian #include <dns/time.h>
305185a700Sflorian
31*3ec2dc6fSflorian static isc_result_t
dns_time64_totext(time_t t,isc_buffer_t * target)32*3ec2dc6fSflorian dns_time64_totext(time_t t, isc_buffer_t *target) {
33*3ec2dc6fSflorian struct tm *tm;
34*3ec2dc6fSflorian char buf[sizeof("YYYYMMDDHHMMSS")];
35*3ec2dc6fSflorian size_t l;
365185a700Sflorian isc_region_t region;
375185a700Sflorian
38*3ec2dc6fSflorian tm = gmtime(&t);
39*3ec2dc6fSflorian if ((l = strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", tm)) == 0)
40*3ec2dc6fSflorian return (ISC_R_NOSPACE);
415185a700Sflorian
425185a700Sflorian isc_buffer_availableregion(target, ®ion);
435185a700Sflorian
445185a700Sflorian if (l > region.length)
455185a700Sflorian return (ISC_R_NOSPACE);
465185a700Sflorian
475185a700Sflorian memmove(region.base, buf, l);
485185a700Sflorian isc_buffer_add(target, l);
495185a700Sflorian return (ISC_R_SUCCESS);
505185a700Sflorian }
515185a700Sflorian
52*3ec2dc6fSflorian static time_t
dns_time64_from32(uint32_t value)535185a700Sflorian dns_time64_from32(uint32_t value) {
54*3ec2dc6fSflorian uint32_t now32;
55*3ec2dc6fSflorian time_t start;
56*3ec2dc6fSflorian time_t t;
575185a700Sflorian
58*3ec2dc6fSflorian time(&start);
59*3ec2dc6fSflorian now32 = (uint32_t) start;
60*3ec2dc6fSflorian
61*3ec2dc6fSflorian /* Adjust the time to the closest epoch. */
62*3ec2dc6fSflorian if (isc_serial_gt(value, now32))
63*3ec2dc6fSflorian t = start + (value - now32);
645185a700Sflorian else
65*3ec2dc6fSflorian t = start - (now32 - value);
665185a700Sflorian
675185a700Sflorian return (t);
685185a700Sflorian }
695185a700Sflorian
705185a700Sflorian isc_result_t
dns_time32_totext(uint32_t value,isc_buffer_t * target)715185a700Sflorian dns_time32_totext(uint32_t value, isc_buffer_t *target) {
725185a700Sflorian return (dns_time64_totext(dns_time64_from32(value), target));
735185a700Sflorian }
74