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 /****s* silcutil/SilcTimeStruct
51 * typedef struct { ... } *SilcTime, SilcTimeStruct;
55 * This context represents time value. It includes date and time
56 * down to millisecond precision. The structure size is 64 bits.
61 typedef struct SilcTimeObject {
62 unsigned int year : 15; /* Year, 0 - 32768 */
63 unsigned int month : 4; /* Month, 1 - 12 */
64 unsigned int day : 5; /* Day, 1 - 31 */
65 unsigned int hour : 5; /* Hour, 0 - 23 */
66 unsigned int minute : 6; /* Minute, 0 - 59 */
67 unsigned int second : 6; /* Second, 0 - 61 */
68 unsigned int msecond : 10; /* Millisec, 0 - 1000 */
69 unsigned int utc_hour : 5; /* Offset to Zulu (UTC) hours */
70 unsigned int utc_minute : 6; /* Offset to Zulu (UTC) minutes */
71 unsigned int utc_east : 1; /* Offset, 1 east (+), 0 west (-) */
72 unsigned int dst : 1; /* Set if daylight saving time */
73 } *SilcTime, SilcTimeStruct;
76 /****f* silcutil/silc_time
80 * SilcInt64 silc_time(void);
84 * Returns the current time of the system since Epoch. The returned
85 * value is seconds since Epoch (1.1.1970). Returns -1 on error.
88 SilcInt64 silc_time(void);
90 /****f* silcutil/silc_time_msec
94 * SilcInt64 silc_time_msec(void);
98 * Returns the current time of the system since Epoch in millisecond
99 * resolution. Returns - 1 on error.
102 SilcInt64 silc_time_msec(void);
104 /****f* silcutil/silc_time_usec
108 * SilcInt64 silc_time_usec(void);
112 * Returns the current time of the system since Epoch in microsecond
113 * resolution. Returns - 1 on error.
116 SilcInt64 silc_time_usec(void);
118 /****f* silcutil/silc_time_string
122 * const char *silc_time_string(SilcInt64 time_val_sec);
126 * Returns time and date as string. The caller must not free the string
127 * and next call to this function will delete the old string. If the
128 * `time_val_sec' is zero (0) returns current time as string, otherwise the
129 * `time_val_sec' as string. The `time_val_sec' is in seconds since Epoch.
130 * Returns NULL on error.
133 const char *silc_time_string(SilcInt64 time_val_sec);
135 /****f* silcutil/silc_time_value
139 * SilcBool silc_time_value(SilcInt64 time_val_msec, SilcTime ret_time);
143 * Returns time and date as SilcTime. If the `time_val_msec' is zero (0)
144 * returns current time as SilcTime, otherwise the `time_val_msec' as
145 * SilcTime. The `time_val_msec' is in milliseconds since Epoch. Returns
146 * FALSE on error, TRUE otherwise.
149 SilcBool silc_time_value(SilcInt64 time_val_msec, SilcTime ret_time);
151 /****f* silcutil/silc_timezone
155 * SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size);
159 * Returns current timezone in Universal time format into the `timezone'
160 * buffer of size of `timezone_size'. The possible values this function
161 * returns are: Z (For UTC timezone), +hh (UTC + hours) -hh (UTC - hours),
162 * +hh:mm (UTC + hours:minutes) or -hh:mm (UTC - hours:minutes). The
163 * returned values are always offsets to UTC.
165 * Returns FALSE on error, TRUE otherwise.
168 SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size);
170 /****f* silcutil/silc_time_universal
174 * SilcBool silc_time_universal(const char *universal_time,
175 * SilcTime ret_time);
179 * Returns time and date as SilcTime from `universal_time' string which
180 * format is "YYMMDDhhmmssZ", where YY is year, MM is month, DD is day,
181 * hh is hour, mm is minutes, ss is seconds and Z is timezone, which
182 * by default is Zulu (UTC). Universal time is defined in ISO/EIC 8824-1.
184 * Returns FALSE on error, TRUE otherwise.
188 * SilcTimeStruct ret_time;
190 * time is 03/02/19 19:04:03 Zulu (UTC)
191 * silc_time_universal("030219190403Z", &ret_time);
194 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time);
196 /****f* silcutil/silc_time_universal_string
200 * SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
201 * SilcUInt32 ret_string_size);
205 * Encodes the SilcTime `time' into the universal time format into the
206 * `ret_string' buffer. Returns FALSE if the buffer is too small.
209 SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
210 SilcUInt32 ret_string_size);
212 /****f* silcutil/silc_time_generalized
216 * SilcBool silc_time_generalized(const char *generalized_time,
217 * SilcTime ret_time);
221 * Returns time and date as SilcTime from `generalized_time' string which
222 * format is "YYYYMMDDhhmmss.ppZ", where YYYY is year, MM is month, DD
223 * is day, hh is hour, mm is minutes, ss is seconds which may have optional
224 * precision pp, and Z is timezone, which by default is Zulu (UTC).
225 * Generalized time is defined in ISO/EIC 8824-1.
227 * Returns FALSE on error, TRUE otherwise.
231 * SilcTimeStruct ret_time;
233 * time is 2003/02/19 19:04:03 Zulu (UTC)
234 * silc_time_generalized("20030219190403Z", &ret_time);
236 * time is 2003/02/19 19:05:10.212 Zulu (UTC)
237 * silc_time_generalized("20030219190510.212Z", &ret_time);
241 silc_time_generalized(const char *generalized_time, SilcTime ret_time);
243 /****f* silcutil/silc_time_generalized_string
247 * SilcBool silc_time_generalized_string(SilcTime time_val,
249 * SilcUInt32 ret_string_size);
253 * Encodes the SilcTime `time' into the generalized time format into the
254 * `ret_string' buffer. Returns FALSE if the buffer is too small.
257 SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
258 SilcUInt32 ret_string_size);
260 /****f* silcutil/silc_compare_timeval
264 * int silc_compare_timeval(struct time_val *t1, struct time_val *t2);
268 * Compares `t1' and `t2' time structures and returns less than zero,
269 * zero or more than zero when `t1' is smaller, equal or bigger than
270 * `t2', respectively.
273 int silc_compare_timeval(struct timeval *t1, struct timeval *t2);
275 /****f* silcutil/silc_gettimeofday
279 * int silc_gettimeofday(struct timeval *p);
283 * Return current time to struct timeval. This function is system
284 * dependant. Returns 0 on success and -1 on error.
287 int silc_gettimeofday(struct timeval *p);
289 #endif /* SILCTIME_H */