1 /*
2  * Copyright (c) 2009 NLNet Labs. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 /**
28  *
29  * Durations.
30  */
31 
32 #ifndef UTIL_DURATION_H
33 #define UTIL_DURATION_H
34 
35 #include "config.h"
36 
37 #include <stdint.h>
38 #include <time.h>
39 
40 /**
41  * Duration.
42  *
43  */
44 typedef struct duration_struct duration_type;
45 struct duration_struct
46 {
47     time_t years;
48     time_t months;
49     time_t weeks;
50     time_t days;
51     time_t hours;
52     time_t minutes;
53     time_t seconds;
54 };
55 
56 /**
57  * Create a new 'instant' duration.
58  * \return duration_type* created duration
59  *
60  */
61 duration_type* duration_create(void);
62 
63 /**
64  * Compare durations.
65  * \param[in] d1 one duration
66  * \param[in] d2 another duration
67  * \return int 0 if equal, -1 if d1 < d2, 1 if d2 < d1
68  *
69  */
70 int duration_compare(duration_type* d1, duration_type* d2);
71 
72 /**
73  * Create a duration from string.
74  * \param[in] str string-format duration
75  * \return duration_type* created duration
76  *
77  */
78 duration_type* duration_create_from_string(const char* str);
79 
80 /**
81  * Convert a duration to a string.
82  * \param[in] duration duration to be converted
83  * \return char* string-format duration
84  *
85  */
86 char* duration2string(duration_type* duration);
87 
88 /**
89  * Convert a duration to a time.
90  * \param[in] duration duration to be converted
91  * \return time_t time-format duration
92  *
93  */
94 time_t duration2time(duration_type* duration);
95 
96 /**
97  * Set the duration based on a time_t.
98  * \param[in] duration a duration_type pointer.
99  * \param[in] time a time_t with the time to set.
100  * \return non-zero on error, otherwise success.
101  */
102 int duration_set_time(duration_type* duration, time_t time);
103 
104 /**
105  * Return a random time.
106  * \param[in] mod modulo
107  * \return time_t random time
108  *
109  */
110 time_t ods_rand(time_t mod);
111 
112 /**
113  * Return time in datestamp.
114  * \param[in] tt time
115  * \param[in] format stamp format
116  * \param[out] str store string
117  * \return uint32_t integer based datestamp.
118  *
119  */
120 uint32_t time_datestamp(time_t tt, const char* format, char** str);
121 
122 /**
123  * Set the time_now to a new value.
124  * As long as this new value is later than the real now time
125  * the overriden value is returned when time_now is called.
126  * \param[in] now override for time_now
127  *
128  */
129 void set_time_now(time_t now);
130 
131 /**
132  * Set the time_now to a new value.
133  * As long as this new value is later than the real now time
134  * the overriden value is returned when time_now is called.
135  * \param[in] now override for time_now in either seconds since
136  * epoch, or the format YYYY-mm-DD-HH:MM.
137  *
138  */
139 int set_time_now_str(char* now);
140 
141 /**
142  * Return the time since Epoch, measured in seconds.
143  * \return time_t now
144  *
145  */
146 time_t time_now(void);
147 
148 /**
149  * Clean up duration.
150  * \param[in] duration duration to be cleaned up
151  *
152  */
153 void duration_cleanup(duration_type* duration);
154 
155 #endif /* UTIL_DURATION_H */
156