Fixed ROBODoc header.
[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_timezone
151  *
152  * SYNOPSIS
153  *
154  *    SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size);
155  *
156  * DESCRIPTION
157  *
158  *    Returns current timezone in Universal time format into the `timezone'
159  *    buffer of size of `timezone_size'.  The possible values this function
160  *    returns are: Z (For UTC timezone), +hh (UTC + hours) -hh (UTC - hours),
161  *    +hh:mm (UTC + hours:minutes) or -hh:mm (UTC - hours:minutes).  The
162  *    returned values are always offsets to UTC.
163  *
164  *    Returns FALSE on error, TRUE otherwise.
165  *
166  ***/
167 SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size);
168
169 /****f* silcutil/silc_time_universal
170  *
171  * SYNOPSIS
172  *
173  *    SilcBool silc_time_universal(const char *universal_time,
174  *                                 SilcTime ret_time);
175  *
176  * DESCRIPTION
177  *
178  *    Returns time and date as SilcTime from `universal_time' string which
179  *    format is "YYMMDDhhmmssZ", where YY is year, MM is month, DD is day,
180  *    hh is hour, mm is minutes, ss is seconds and Z is timezone, which
181  *    by default is Zulu (UTC).  Universal time is defined in ISO/EIC 8824-1.
182  *
183  *    Returns FALSE on error, TRUE otherwise.
184  *
185  * EXAMPLE
186  *
187  *    SilcTimeStruct ret_time;
188  *
189  *    time is 03/02/19 19:04:03 Zulu (UTC)
190  *    silc_time_universal("030219190403Z", &ret_time);
191  *
192  ***/
193 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time);
194
195 /****f* silcutil/silc_time_universal_string
196  *
197  * SYNOPSIS
198  *
199  *    SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
200  *                                        SilcUInt32 ret_string_size);
201  *
202  * DESCRIPTION
203  *
204  *    Encodes the SilcTime `time' into the universal time format into the
205  *    `ret_string' buffer.  Returns FALSE if the buffer is too small.
206  *
207  ***/
208 SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
209                                     SilcUInt32 ret_string_size);
210
211 /****f* silcutil/silc_time_generalized
212  *
213  * SYNOPSIS
214  *
215  *    SilcBool silc_time_generalized(const char *generalized_time,
216  *                                   SilcTime ret_time);
217  *
218  * DESCRIPTION
219  *
220  *    Returns time and date as SilcTime from `generalized_time' string which
221  *    format is "YYYYMMDDhhmmss.ppZ", where YYYY is year, MM is month, DD
222  *    is day, hh is hour, mm is minutes, ss is seconds which may have optional
223  *    precision pp, and Z is timezone, which by default is Zulu (UTC).
224  *    Generalized time is defined in ISO/EIC 8824-1.
225  *
226  *    Returns FALSE on error, TRUE otherwise.
227  *
228  * EXAMPLE
229  *
230  *    SilcTimeStruct ret_time;
231  *
232  *    time is 2003/02/19 19:04:03 Zulu (UTC)
233  *    silc_time_generalized("20030219190403Z", &ret_time);
234  *
235  *    time is 2003/02/19 19:05:10.212 Zulu (UTC)
236  *    silc_time_generalized("20030219190510.212Z", &ret_time);
237  *
238  ***/
239 SilcBool
240 silc_time_generalized(const char *generalized_time, SilcTime ret_time);
241
242 /****f* silcutil/silc_time_generalized_string
243  *
244  * SYNOPSIS
245  *
246  *    SilcBool silc_time_generalized_string(SilcTime time_val,
247  *                                          char *ret_string,
248  *                                          SilcUInt32 ret_string_size);
249  *
250  * DESCRIPTION
251  *
252  *    Encodes the SilcTime `time' into the generalized time format into the
253  *    `ret_string' buffer.  Returns FALSE if the buffer is too small.
254  *
255  ***/
256 SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
257                                       SilcUInt32 ret_string_size);
258
259 /****f* silcutil/silc_compare_timeval
260  *
261  * SYNOPSIS
262  *
263  *    int silc_compare_timeval(struct time_val *t1, struct time_val *t2);
264  *
265  * DESCRIPTION
266  *
267  *    Compares `t1' and `t2' time structures and returns less than zero,
268  *    zero or more than zero when `t1' is smaller, equal or bigger than
269  *    `t2', respectively.
270  *
271  ***/
272 int silc_compare_timeval(struct timeval *t1, struct timeval *t2);
273
274 /****f* silcutil/silc_gettimeofday
275  *
276  * SYNOPSIS
277  *
278  *    int silc_gettimeofday(struct timeval *p);
279  *
280  * DESCRIPTION
281  *
282  *    Return current time to struct timeval.  This function is system
283  *    dependant.  Returns 0 on success and -1 on error.
284  *
285  ***/
286 int silc_gettimeofday(struct timeval *p);
287
288 #endif /* SILCTIME_H */