xref: /netbsd-src/external/mpl/bind/dist/lib/isccfg/include/isccfg/duration.h (revision bcda20f65a8566e103791ec395f7f499ef322704)
1 /*	$NetBSD: duration.h,v 1.3 2025/01/26 16:25:45 christos Exp $	*/
2 
3 
4 /*
5  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
6  *
7  * SPDX-License-Identifier: MPL-2.0
8  *
9  * This Source Code Form is subject to the terms of the Mozilla Public
10  * License, v. 2.0. If a copy of the MPL was not distributed with this
11  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
12  *
13  * See the COPYRIGHT file distributed with this work for additional
14  * information regarding copyright ownership.
15  */
16 
17 #pragma once
18 
19 /*! \file */
20 
21 #include <inttypes.h>
22 #include <stdbool.h>
23 
24 #include <isc/lang.h>
25 #include <isc/types.h>
26 
27 ISC_LANG_BEGINDECLS
28 
29 #define CFG_DURATION_MAXLEN 80
30 
31 /*%
32  * A configuration object to store ISO 8601 durations.
33  */
34 typedef struct isccfg_duration {
35 	/*
36 	 * The duration is stored in multiple parts:
37 	 * [0] Years
38 	 * [1] Months
39 	 * [2] Weeks
40 	 * [3] Days
41 	 * [4] Hours
42 	 * [5] Minutes
43 	 * [6] Seconds
44 	 */
45 	uint32_t parts[7];
46 	bool	 iso8601;
47 	bool	 unlimited;
48 } isccfg_duration_t;
49 
50 isc_result_t
51 isccfg_duration_fromtext(isc_textregion_t *source, isccfg_duration_t *duration);
52 /*%<
53  * Converts an ISO 8601 duration style value.
54  *
55  * Returns:
56  *\li	ISC_R_SUCCESS
57  *\li	DNS_R_BADNUMBER
58  */
59 
60 isc_result_t
61 isccfg_parse_duration(isc_textregion_t *source, isccfg_duration_t *duration);
62 /*%<
63  * Converts a duration string to a ISO 8601 duration.
64  * If the string does not start with a P (or p), fall back to TTL-style value.
65  * In that case the duration will be treated in seconds only.
66  *
67  * Returns:
68  *\li	ISC_R_SUCCESS
69  *\li	DNS_R_BADNUMBER
70  *\li	DNS_R_BADTTL
71  */
72 
73 uint32_t
74 isccfg_duration_toseconds(const isccfg_duration_t *duration);
75 /*%<
76  * Converts an ISO 8601 duration to seconds.
77  * The conversion is approximate:
78  * - Months will be treated as 31 days.
79  * - Years will be treated as 365 days.
80  *
81  * Notes:
82  *\li	If the duration in seconds is greater than UINT32_MAX, the return value
83  * 	will be UINT32_MAX.
84  *
85  * Returns:
86  *\li	The duration in seconds.
87  */
88 
89 ISC_LANG_ENDDECLS
90