summaryrefslogtreecommitdiffstats
path: root/usr.bin/dig/lib/dns/dns_time.c
blob: 9d564a411a21c44c11a996263182ac2ab96cdf4d (plain) (blame)
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
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/* $Id: dns_time.c,v 1.7 2020/04/02 16:57:45 florian Exp $ */

/*! \file */

#include <stdio.h>
#include <string.h>
#include <time.h>

#include <isc/region.h>
#include <isc/serial.h>
#include <isc/result.h>

#include <dns/time.h>

static isc_result_t
dns_time64_totext(time_t t, isc_buffer_t *target) {
	struct tm *tm;
	char buf[sizeof("YYYYMMDDHHMMSS")];
	size_t l;
	isc_region_t region;

	tm = gmtime(&t);
	if ((l = strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", tm)) == 0)
		return (ISC_R_NOSPACE);

	isc_buffer_availableregion(target, &region);

	if (l > region.length)
		return (ISC_R_NOSPACE);

	memmove(region.base, buf, l);
	isc_buffer_add(target, l);
	return (ISC_R_SUCCESS);
}

static time_t
dns_time64_from32(uint32_t value) {
	uint32_t now32;
	time_t start;
	time_t t;

	time(&start);
	now32 = (uint32_t) start;

	/* Adjust the time to the closest epoch. */
	if (isc_serial_gt(value, now32))
		t = start + (value - now32);
	else
		t = start - (now32 - value);

	return (t);
}

isc_result_t
dns_time32_totext(uint32_t value, isc_buffer_t *target) {
	return (dns_time64_totext(dns_time64_from32(value), target));
}