5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2003 - 2008 Pekka Riikonen
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.
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.
20 /****h* silcutil/Time Interface
24 * This interface provides various utility functions for getting current
25 * time and converting different time representations into the SilcTime
33 /****s* silcutil/SilcTime
37 * typedef struct { ... } *SilcTime, SilcTimeStruct;
41 * This context represents time value. It includes date and time
42 * down to millisecond precision. The structure size is 64 bits.
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;
62 /****s* silcutil/SilcTimeStruct
66 * typedef struct { ... } *SilcTime, SilcTimeStruct;
70 * This context represents time value. It includes date and time
71 * down to millisecond precision. The structure size is 64 bits.
75 /****f* silcutil/silc_time
79 * SilcInt64 silc_time(void);
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.
87 SilcInt64 silc_time(void);
89 /****f* silcutil/silc_time_msec
93 * SilcInt64 silc_time_msec(void);
97 * Returns the current time of the system since Epoch in millisecond
98 * resolution. Returns - 1 on error.
101 SilcInt64 silc_time_msec(void);
103 /****f* silcutil/silc_time_usec
107 * SilcInt64 silc_time_usec(void);
111 * Returns the current time of the system since Epoch in microsecond
112 * resolution. Returns - 1 on error.
115 SilcInt64 silc_time_usec(void);
117 /****f* silcutil/silc_time_string
121 * const char *silc_time_string(SilcInt64 time_val_sec);
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.
132 const char *silc_time_string(SilcInt64 time_val_sec);
134 /****f* silcutil/silc_time_value
138 * SilcBool silc_time_value(SilcInt64 time_val_msec, SilcTime ret_time);
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.
148 SilcBool silc_time_value(SilcInt64 time_val_msec, SilcTime ret_time);
150 /****f* silcutil/silc_time_epoch
154 * SilcUInt64 silc_time_epoch(SilcTime timeval);
158 * Converts SilcTime into Epoch time value and returns the converted
162 SilcUInt64 silc_time_epoch(SilcTime timeval);
164 /****f* silcutil/silc_timezone
168 * SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size);
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.
178 * Returns FALSE on error, TRUE otherwise.
181 SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size);
183 /****f* silcutil/silc_time_universal
187 * SilcBool silc_time_universal(const char *universal_time,
188 * SilcTime ret_time);
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.
197 * Returns FALSE on error, TRUE otherwise.
201 * SilcTimeStruct ret_time;
203 * time is 03/02/19 19:04:03 Zulu (UTC)
204 * silc_time_universal("030219190403Z", &ret_time);
207 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time);
209 /****f* silcutil/silc_time_universal_string
213 * SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
214 * SilcUInt32 ret_string_size);
218 * Encodes the SilcTime `time' into the universal time format into the
219 * `ret_string' buffer. Returns FALSE if the buffer is too small.
222 SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
223 SilcUInt32 ret_string_size);
225 /****f* silcutil/silc_time_generalized
229 * SilcBool silc_time_generalized(const char *generalized_time,
230 * SilcTime ret_time);
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.
240 * Returns FALSE on error, TRUE otherwise.
244 * SilcTimeStruct ret_time;
246 * time is 2003/02/19 19:04:03 Zulu (UTC)
247 * silc_time_generalized("20030219190403Z", &ret_time);
249 * time is 2003/02/19 19:05:10.212 Zulu (UTC)
250 * silc_time_generalized("20030219190510.212Z", &ret_time);
254 silc_time_generalized(const char *generalized_time, SilcTime ret_time);
256 /****f* silcutil/silc_time_generalized_string
260 * SilcBool silc_time_generalized_string(SilcTime time_val,
262 * SilcUInt32 ret_string_size);
266 * Encodes the SilcTime `time' into the generalized time format into the
267 * `ret_string' buffer. Returns FALSE if the buffer is too small.
270 SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
271 SilcUInt32 ret_string_size);
273 /****f* silcutil/silc_compare_timeval
277 * int silc_compare_timeval(struct time_val *t1, struct time_val *t2);
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.
286 int silc_compare_timeval(struct timeval *t1, struct timeval *t2);
288 /****f* silcutil/silc_gettimeofday
292 * int silc_gettimeofday(struct timeval *p);
296 * Return current time to struct timeval. This function is system
297 * dependant. Returns 0 on success and -1 on error.
300 int silc_gettimeofday(struct timeval *p);
302 #endif /* SILCTIME_H */