Added silc_time_epoch, converting SilcTime to Epoch time value
[runtime.git] / lib / silcutil / silctime.h
1 /*
2
3   silctime.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2003 - 2008 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 /****h* silcutil/Time Interface
21  *
22  * DESCRIPTION
23  *
24  * This interface provides various utility functions for getting current
25  * time and converting different time representations into the SilcTime
26  * representation.
27  *
28  ***/
29
30 #ifndef SILCTIME_H
31 #define SILCTIME_H
32
33 /****s* silcutil/SilcTime
34  *
35  * NAME
36  *
37  *    typedef struct { ... } *SilcTime, SilcTimeStruct;
38  *
39  * DESCRIPTION
40  *
41  *    This context represents time value.  It includes date and time
42  *    down to millisecond precision.  The structure size is 64 bits.
43  *
44  * SOURCE
45  *
46  */
47 typedef struct SilcTimeObject {
48   unsigned int year       : 15;    /* Year,     0 - 32768 */
49   unsigned int month      : 4;     /* Month,    1 - 12 */
50   unsigned int day        : 5;     /* Day,      1 - 31 */
51   unsigned int hour       : 5;     /* Hour,     0 - 23 */
52   unsigned int minute     : 6;     /* Minute,   0 - 59 */
53   unsigned int second     : 6;     /* Second,   0 - 61 */
54   unsigned int msecond    : 10;    /* Millisec, 0 - 1000 */
55   unsigned int utc_hour   : 5;     /* Offset to Zulu (UTC) hours */
56   unsigned int utc_minute : 6;     /* Offset to Zulu (UTC) minutes */
57   unsigned int utc_east   : 1;     /* Offset, 1 east (+), 0 west (-) */
58   unsigned int dst        : 1;     /* Set if daylight saving time */
59 } *SilcTime, SilcTimeStruct;
60 /***/
61
62 /****s* silcutil/SilcTimeStruct
63  *
64  * NAME
65  *
66  *    typedef struct { ... } *SilcTime, SilcTimeStruct;
67  *
68  * DESCRIPTION
69  *
70  *    This context represents time value.  It includes date and time
71  *    down to millisecond precision.  The structure size is 64 bits.
72  *
73  ***/
74
75 /****f* silcutil/silc_time
76  *
77  * SYNOPSIS
78  *
79  *    SilcInt64 silc_time(void);
80  *
81  * DESCRIPTION
82  *
83  *    Returns the current time of the system since Epoch.  The returned
84  *    value is seconds since Epoch (1.1.1970).  Returns -1 on error.
85  *
86  ***/
87 SilcInt64 silc_time(void);
88
89 /****f* silcutil/silc_time_msec
90  *
91  * SYNOPSIS
92  *
93  *    SilcInt64 silc_time_msec(void);
94  *
95  * DESCRIPTION
96  *
97  *    Returns the current time of the system since Epoch in millisecond
98  *    resolution.  Returns - 1 on error.
99  *
100  ***/
101 SilcInt64 silc_time_msec(void);
102
103 /****f* silcutil/silc_time_usec
104  *
105  * SYNOPSIS
106  *
107  *    SilcInt64 silc_time_usec(void);
108  *
109  * DESCRIPTION
110  *
111  *    Returns the current time of the system since Epoch in microsecond
112  *    resolution.  Returns - 1 on error.
113  *
114  ***/
115 SilcInt64 silc_time_usec(void);
116
117 /****f* silcutil/silc_time_string
118  *
119  * SYNOPSIS
120  *
121  *    const char *silc_time_string(SilcInt64 time_val_sec);
122  *
123  * DESCRIPTION
124  *
125  *    Returns time and date as string.  The caller must not free the string
126  *    and next call to this function will delete the old string.  If the
127  *    `time_val_sec' is zero (0) returns current time as string, otherwise the
128  *    `time_val_sec' as string.  The `time_val_sec' is in seconds since Epoch.
129  *    Returns NULL on error.
130  *
131  ***/
132 const char *silc_time_string(SilcInt64 time_val_sec);
133
134 /****f* silcutil/silc_time_value
135  *
136  * SYNOPSIS
137  *
138  *    SilcBool silc_time_value(SilcInt64 time_val_msec, SilcTime ret_time);
139  *
140  * DESCRIPTION
141  *
142  *    Returns time and date as SilcTime.  If the `time_val_msec' is zero (0)
143  *    returns current time as SilcTime, otherwise the `time_val_msec' as
144  *    SilcTime.  The `time_val_msec' is in milliseconds since Epoch.  Returns
145  *    FALSE on error, TRUE otherwise.
146  *
147  ***/
148 SilcBool silc_time_value(SilcInt64 time_val_msec, SilcTime ret_time);
149
150 /****f* silcutil/silc_time_value
151  *
152  * SYNOPSIS
153  *
154  *    SilcUInt64 silc_time_epoch(SilcTime timeval);
155  *
156  * DESCRIPTION
157  *
158  *    Converts SilcTime into Epoch time value and returns the converted
159  *    value.
160  *
161  ***/
162 SilcUInt64 silc_time_epoch(SilcTime timeval);
163
164 /****f* silcutil/silc_timezone
165  *
166  * SYNOPSIS
167  *
168  *    SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size);
169  *
170  * DESCRIPTION
171  *
172  *    Returns current timezone in Universal time format into the `timezone'
173  *    buffer of size of `timezone_size'.  The possible values this function
174  *    returns are: Z (For UTC timezone), +hh (UTC + hours) -hh (UTC - hours),
175  *    +hh:mm (UTC + hours:minutes) or -hh:mm (UTC - hours:minutes).  The
176  *    returned values are always offsets to UTC.
177  *
178  *    Returns FALSE on error, TRUE otherwise.
179  *
180  ***/
181 SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size);
182
183 /****f* silcutil/silc_time_universal
184  *
185  * SYNOPSIS
186  *
187  *    SilcBool silc_time_universal(const char *universal_time,
188  *                                 SilcTime ret_time);
189  *
190  * DESCRIPTION
191  *
192  *    Returns time and date as SilcTime from `universal_time' string which
193  *    format is "YYMMDDhhmmssZ", where YY is year, MM is month, DD is day,
194  *    hh is hour, mm is minutes, ss is seconds and Z is timezone, which
195  *    by default is Zulu (UTC).  Universal time is defined in ISO/EIC 8824-1.
196  *
197  *    Returns FALSE on error, TRUE otherwise.
198  *
199  * EXAMPLE
200  *
201  *    SilcTimeStruct ret_time;
202  *
203  *    time is 03/02/19 19:04:03 Zulu (UTC)
204  *    silc_time_universal("030219190403Z", &ret_time);
205  *
206  ***/
207 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time);
208
209 /****f* silcutil/silc_time_universal_string
210  *
211  * SYNOPSIS
212  *
213  *    SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
214  *                                        SilcUInt32 ret_string_size);
215  *
216  * DESCRIPTION
217  *
218  *    Encodes the SilcTime `time' into the universal time format into the
219  *    `ret_string' buffer.  Returns FALSE if the buffer is too small.
220  *
221  ***/
222 SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
223                                     SilcUInt32 ret_string_size);
224
225 /****f* silcutil/silc_time_generalized
226  *
227  * SYNOPSIS
228  *
229  *    SilcBool silc_time_generalized(const char *generalized_time,
230  *                                   SilcTime ret_time);
231  *
232  * DESCRIPTION
233  *
234  *    Returns time and date as SilcTime from `generalized_time' string which
235  *    format is "YYYYMMDDhhmmss.ppZ", where YYYY is year, MM is month, DD
236  *    is day, hh is hour, mm is minutes, ss is seconds which may have optional
237  *    precision pp, and Z is timezone, which by default is Zulu (UTC).
238  *    Generalized time is defined in ISO/EIC 8824-1.
239  *
240  *    Returns FALSE on error, TRUE otherwise.
241  *
242  * EXAMPLE
243  *
244  *    SilcTimeStruct ret_time;
245  *
246  *    time is 2003/02/19 19:04:03 Zulu (UTC)
247  *    silc_time_generalized("20030219190403Z", &ret_time);
248  *
249  *    time is 2003/02/19 19:05:10.212 Zulu (UTC)
250  *    silc_time_generalized("20030219190510.212Z", &ret_time);
251  *
252  ***/
253 SilcBool
254 silc_time_generalized(const char *generalized_time, SilcTime ret_time);
255
256 /****f* silcutil/silc_time_generalized_string
257  *
258  * SYNOPSIS
259  *
260  *    SilcBool silc_time_generalized_string(SilcTime time_val,
261  *                                          char *ret_string,
262  *                                          SilcUInt32 ret_string_size);
263  *
264  * DESCRIPTION
265  *
266  *    Encodes the SilcTime `time' into the generalized time format into the
267  *    `ret_string' buffer.  Returns FALSE if the buffer is too small.
268  *
269  ***/
270 SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
271                                       SilcUInt32 ret_string_size);
272
273 /****f* silcutil/silc_compare_timeval
274  *
275  * SYNOPSIS
276  *
277  *    int silc_compare_timeval(struct time_val *t1, struct time_val *t2);
278  *
279  * DESCRIPTION
280  *
281  *    Compares `t1' and `t2' time structures and returns less than zero,
282  *    zero or more than zero when `t1' is smaller, equal or bigger than
283  *    `t2', respectively.
284  *
285  ***/
286 int silc_compare_timeval(struct timeval *t1, struct timeval *t2);
287
288 /****f* silcutil/silc_gettimeofday
289  *
290  * SYNOPSIS
291  *
292  *    int silc_gettimeofday(struct timeval *p);
293  *
294  * DESCRIPTION
295  *
296  *    Return current time to struct timeval.  This function is system
297  *    dependant.  Returns 0 on success and -1 on error.
298  *
299  ***/
300 int silc_gettimeofday(struct timeval *p);
301
302 #endif /* SILCTIME_H */