summaryrefslogtreecommitdiffstats
path: root/lib/isccfg/include/isccfg/duration.h
blob: bd0c35bad8a0e1145e6ee9af30e102cadcd58ca0 (plain)
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
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

#pragma once

/*! \file */

#include <inttypes.h>
#include <stdbool.h>

#include <isc/lang.h>
#include <isc/types.h>

ISC_LANG_BEGINDECLS

#define CFG_DURATION_MAXLEN 80

/*%
 * A configuration object to store ISO 8601 durations.
 */
typedef struct isccfg_duration {
	/*
	 * The duration is stored in multiple parts:
	 * [0] Years
	 * [1] Months
	 * [2] Weeks
	 * [3] Days
	 * [4] Hours
	 * [5] Minutes
	 * [6] Seconds
	 */
	uint32_t parts[7];
	bool	 iso8601;
	bool	 unlimited;
} isccfg_duration_t;

isc_result_t
isccfg_duration_fromtext(isc_textregion_t *source, isccfg_duration_t *duration);
/*%<
 * Converts an ISO 8601 duration style value.
 *
 * Returns:
 *\li	ISC_R_SUCCESS
 *\li	DNS_R_BADNUMBER
 */

isc_result_t
isccfg_parse_duration(isc_textregion_t *source, isccfg_duration_t *duration);
/*%<
 * Converts a duration string to a ISO 8601 duration.
 * If the string does not start with a P (or p), fall back to TTL-style value.
 * In that case the duration will be treated in seconds only.
 *
 * Returns:
 *\li	ISC_R_SUCCESS
 *\li	DNS_R_BADNUMBER
 *\li	DNS_R_BADTTL
 */

uint32_t
isccfg_duration_toseconds(const isccfg_duration_t *duration);
/*%<
 * Converts an ISO 8601 duration to seconds.
 * The conversion is approximate:
 * - Months will be treated as 31 days.
 * - Years will be treated as 365 days.
 *
 * Notes:
 *\li	If the duration in seconds is greater than UINT32_MAX, the return value
 * 	will be UINT32_MAX.
 *
 * Returns:
 *\li	The duration in seconds.
 */

ISC_LANG_ENDDECLS