5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2003 - 2007 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.
22 /* Fills the SilcTime structure with correct values */
24 static SilcBool silc_time_fill(SilcTime time,
35 if (month < 1 || month > 12)
37 if (day < 1 || day > 31)
52 time->minute = minute;
53 time->second = second;
59 /* Return time since Epoch */
61 SilcInt64 silc_time(void)
63 return (SilcInt64)time(NULL);
66 /* Return time since Epoch in milliseconds */
68 SilcInt64 silc_time_msec(void)
70 struct timeval curtime;
71 silc_gettimeofday(&curtime);
72 return (curtime.tv_sec * 1000) + (curtime.tv_usec / 1000);
75 /* Return time since Epoch in microseconds */
77 SilcInt64 silc_time_usec(void)
79 struct timeval curtime;
80 silc_gettimeofday(&curtime);
81 return (curtime.tv_sec * 1000000) + curtime.tv_usec;
84 /* Returns time as string */
86 const char *silc_time_string(SilcInt64 time_val)
92 curtime = silc_time();
94 curtime = (time_t)time_val;
95 return_time = ctime(&curtime);
98 return_time[strlen(return_time) - 1] = '\0';
100 return (const char *)return_time;
103 /* Returns time as SilcTime structure */
105 SilcBool silc_time_value(SilcInt64 time_val, SilcTime ret_time)
108 unsigned int msec = 0;
115 time_val = silc_time_msec();
117 msec = time_val % 1000;
120 time = localtime(&timeval);
125 memset(ret_time, 0, sizeof(*ret_time));
126 if (!silc_time_fill(ret_time, time->tm_year + 1900, time->tm_mon + 1,
127 time->tm_mday, time->tm_hour, time->tm_min,
131 ret_time->dst = time->tm_isdst ? 1 : 0;
133 ret_time->utc_east = _timezone < 0 ? 1 : 0;
134 ret_time->utc_hour = (ret_time->utc_east ? (-(_timezone)) / 3600 :
136 ret_time->utc_minute = (ret_time->utc_east ? (-(_timezone)) % 3600 :
139 #if defined(HAVE_TZSET)
140 ret_time->utc_east = timezone < 0 ? 1 : 0;
141 ret_time->utc_hour = (ret_time->utc_east ? (-(timezone)) / 3600 :
143 ret_time->utc_minute = (ret_time->utc_east ? (-(timezone)) % 3600 :
145 #endif /* HAVE_TZSET */
146 #endif /* SILC_WIN32 */
151 /* Returns time from universal time string into SilcTime */
153 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
156 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
161 memset(ret_time, 0, sizeof(*ret_time));
163 /* Parse the time string */
164 ret = sscanf(universal_time, "%02u%02u%02u%02u%02u%02u%c", &year, &month,
165 &day, &hour, &minute, &second, &z);
167 SILC_LOG_DEBUG(("Invalid UTC time string"));
171 /* Fill the SilcTime structure */
172 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
174 SILC_LOG_DEBUG(("Incorrect values in UTC time string"));
179 if (z == '-' || z == '+') {
180 ret = sscanf(universal_time + (ret * 2) + 1, "%02u%02u", &hour, &minute);
182 SILC_LOG_DEBUG(("Malformed UTC time string"));
186 if (hour < 0 || hour > 23)
188 if (minute < 0 || minute > 60)
191 ret_time->utc_hour = hour;
192 ret_time->utc_minute = minute;
193 ret_time->utc_east = (z == '-') ? 0 : 1;
194 } else if (z != 'Z') {
195 SILC_LOG_DEBUG(("Invalid timezone"));
199 /* UTC year must be fixed since it's represented only as YY not YYYY. */
200 ret_time->year += 1900;
201 if (ret_time->year < 1950)
202 ret_time->year += 100;
207 /* Encode universal time string. */
209 SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
210 SilcUInt32 ret_string_size)
213 memset(ret_string, 0, ret_string_size);
214 ret = silc_snprintf(ret_string, ret_string_size - 1,
215 "%02u%02u%02u%02u%02u%02u",
216 time_val->year % 100, time_val->month, time_val->day,
217 time_val->hour, time_val->minute, time_val->second);
222 if (!time_val->utc_hour && !time_val->utc_minute) {
223 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
228 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
229 "%c%02u%02u", time_val->utc_east ? '+' : '-',
230 time_val->utc_hour, time_val->utc_minute);
239 /* Returns time from generalized time string into SilcTime */
241 SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
244 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
245 unsigned int msecond = 0;
250 memset(ret_time, 0, sizeof(*ret_time));
252 /* Parse the time string */
253 ret = sscanf(generalized_time, "%04u%02u%02u%02u%02u%02u", &year, &month,
254 &day, &hour, &minute, &second);
256 SILC_LOG_DEBUG(("Invalid generalized time string"));
260 /* Fill the SilcTime structure */
261 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
263 SILC_LOG_DEBUG(("Incorrect values in generalized time string"));
267 /* Check fractions of second and/or timezone */
269 ret = sscanf(generalized_time + i, "%c", &z);
271 SILC_LOG_DEBUG(("Malformed generalized time string"));
276 /* Take fractions of second */
279 ret = sscanf(generalized_time + i, "%u%n", &msecond, &l);
281 SILC_LOG_DEBUG(("Malformed generalized time string"));
288 ret_time->msecond = msecond;
291 /* Read optional timezone */
292 if (strlen(generalized_time) < i)
293 sscanf(generalized_time + i, "%c", &z);
296 /* Check timezone if present */
297 if (z == '-' || z == '+') {
298 ret = sscanf(generalized_time + i + 1, "%02u%02u", &hour, &minute);
300 SILC_LOG_DEBUG(("Malformed UTC time string"));
304 if (hour < 0 || hour > 23)
306 if (minute < 0 || minute > 60)
309 ret_time->utc_hour = hour;
310 ret_time->utc_minute = minute;
311 ret_time->utc_east = (z == '-') ? 0 : 1;
317 /* Encode generalized time string */
319 SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
320 SilcUInt32 ret_string_size)
323 memset(ret_string, 0, ret_string_size);
324 ret = silc_snprintf(ret_string, ret_string_size - 1,
325 "%04u%02u%02u%02u%02u%02u",
326 time_val->year, time_val->month, time_val->day, time_val->hour,
327 time_val->minute, time_val->second);
332 if (time_val->msecond) {
333 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
334 ".%lu", (unsigned long)time_val->msecond);
340 if (!time_val->utc_hour && !time_val->utc_minute) {
341 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
346 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
347 "%c%02u%02u", time_val->utc_east ? '+' : '-',
348 time_val->utc_hour, time_val->utc_minute);
357 /* Return TRUE if `smaller' is smaller than `bigger'. */
359 SilcBool silc_compare_timeval(struct timeval *smaller,
360 struct timeval *bigger)
362 if ((smaller->tv_sec < bigger->tv_sec) ||
363 ((smaller->tv_sec == bigger->tv_sec) &&
364 (smaller->tv_usec < bigger->tv_usec)))