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 #include "silcruntime.h"
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 silc_set_errno(SILC_ERR_BAD_TIME);
63 /* Return time since Epoch */
65 SilcInt64 silc_time(void)
67 return (SilcInt64)time(NULL);
70 /* Return time since Epoch in milliseconds */
72 SilcInt64 silc_time_msec(void)
74 struct timeval curtime;
75 silc_gettimeofday(&curtime);
76 return (curtime.tv_sec * (SilcUInt64)1000) +
77 (curtime.tv_usec / (SilcUInt64)1000);
80 /* Return time since Epoch in microseconds */
82 SilcInt64 silc_time_usec(void)
84 struct timeval curtime;
85 if (silc_gettimeofday(&curtime))
86 silc_set_errno_posix(errno);
87 return (curtime.tv_sec * (SilcUInt64)1000000) + curtime.tv_usec;
90 /* Returns time as string */
92 const char *silc_time_string(SilcInt64 time_val)
98 curtime = silc_time();
100 curtime = (time_t)time_val;
101 return_time = ctime(&curtime);
103 silc_set_errno(SILC_ERR_BAD_TIME);
106 return_time[strlen(return_time) - 1] = '\0';
108 return (const char *)return_time;
111 /* Returns time as SilcTime structure */
113 SilcBool silc_time_value(SilcInt64 time_val, SilcTime ret_time)
116 unsigned int msec = 0;
124 time_val = silc_time_msec();
126 msec = (SilcUInt64)time_val % (SilcUInt64)1000;
127 timeval = (time_t)((SilcUInt64)time_val / (SilcUInt64)1000);
129 t = localtime(&timeval);
131 silc_set_errno(SILC_ERR_BAD_TIME);
135 memset(ret_time, 0, sizeof(*ret_time));
136 if (!silc_time_fill(ret_time, t->tm_year + 1900, t->tm_mon + 1,
137 t->tm_mday, t->tm_hour, t->tm_min,
141 ret_time->dst = t->tm_isdst ? 1 : 0;
144 ret_time->utc_east = _timezone < 0 ? 1 : 0;
145 ret_time->utc_hour = (ret_time->utc_east ? (-(_timezone)) / 3600 :
147 ret_time->utc_minute = (ret_time->utc_east ? (-(_timezone)) % 3600 :
150 #if defined(HAVE_TIMEZONE)
151 ret_time->utc_east = timezone < 0 ? 1 : 0;
155 #elif defined(HAVE_TM_GMTOFF)
156 ret_time->utc_east = t->tm_gmtoff > 0 ? 1 : 0;
158 #elif defined(HAVE___TM_GMTOFF)
159 ret_time->utc_east = t->__tm_gmtoff > 0 ? 1 : 0;
160 ctz = -t->__tm_gmtoff;
161 #elif defined(HAVE___TM_GMTOFF__)
162 ret_time->utc_east = t->__tm_gmtoff__ > 0 ? 1 : 0;
163 ctz = -t->__tm_gmtoff__;
164 #endif /* HAVE_TIMEZONE */
166 ret_time->utc_hour = (ret_time->utc_east ? (-(ctz)) / 3600 : ctz / 3600);
167 ret_time->utc_minute = (ret_time->utc_east ? (-(ctz)) % 3600 : ctz % 3600);
168 #endif /* SILC_WIN32 */
170 if (ret_time->utc_minute)
171 ret_time->utc_minute /= 60;
176 /* SilcTime to epoch */
178 SilcUInt64 silc_time_epoch(SilcTime timeval)
185 val.tm_sec = timeval->second;
186 val.tm_min = timeval->minute;
187 val.tm_hour = timeval->hour;
188 val.tm_mday = timeval->day;
189 val.tm_mon = timeval->month - 1;
190 val.tm_year = timeval->year - 1900;
191 val.tm_isdst = timeval->dst;
193 return (SilcUInt64)mktime(&val);
196 /* Returns timezone */
198 SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size)
200 SilcTimeStruct curtime;
202 if (timezone_size < 6) {
203 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
207 if (!silc_time_value(0, &curtime))
210 if (!curtime.utc_hour && curtime.utc_minute)
211 silc_snprintf(timezone, timezone_size, "Z");
212 else if (curtime.utc_minute)
213 silc_snprintf(timezone, timezone_size, "%c%02d:%02d",
214 curtime.utc_east ? '+' : '-', curtime.utc_hour,
217 silc_snprintf(timezone, timezone_size, "%c%02d",
218 curtime.utc_east ? '+' : '-', curtime.utc_hour);
223 /* Returns time from universal time string into SilcTime */
225 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
228 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
233 memset(ret_time, 0, sizeof(*ret_time));
235 /* Parse the time string */
236 ret = sscanf(universal_time, "%02u%02u%02u%02u%02u%02u%c", &year, &month,
237 &day, &hour, &minute, &second, &z);
239 SILC_LOG_DEBUG(("Invalid UTC time string"));
240 silc_set_errno_reason(SILC_ERR_BAD_TIME, "Invalid UTC time string");
244 /* Fill the SilcTime structure */
245 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
247 SILC_LOG_DEBUG(("Incorrect values in UTC time string"));
252 if (z == '-' || z == '+') {
253 ret = sscanf(universal_time + (ret * 2) + 1, "%02u%02u", &hour, &minute);
255 SILC_LOG_DEBUG(("Malformed UTC time string"));
256 silc_set_errno_reason(SILC_ERR_BAD_TIME, "Malformed UTC time string");
260 if (hour > 23 || minute > 60) {
261 silc_set_errno(SILC_ERR_BAD_TIME);
265 ret_time->utc_hour = hour;
266 ret_time->utc_minute = minute;
267 ret_time->utc_east = (z == '-') ? 0 : 1;
268 } else if (z != 'Z') {
269 SILC_LOG_DEBUG(("Invalid timezone"));
270 silc_set_errno_reason(SILC_ERR_BAD_TIME, "Invalid timezone");
274 /* UTC year must be fixed since it's represented only as YY not YYYY. */
275 ret_time->year += 1900;
276 if (ret_time->year < 1950)
277 ret_time->year += 100;
282 /* Encode universal time string. */
284 SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
285 SilcUInt32 ret_string_size)
289 memset(ret_string, 0, ret_string_size);
290 ret = silc_snprintf(ret_string, ret_string_size - 1,
291 "%02u%02u%02u%02u%02u%02u",
292 time_val->year % 100, time_val->month, time_val->day,
293 time_val->hour, time_val->minute, time_val->second);
298 if (!time_val->utc_hour && !time_val->utc_minute) {
299 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
304 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
305 "%c%02u%02u", time_val->utc_east ? '+' : '-',
306 time_val->utc_hour, time_val->utc_minute);
315 /* Returns time from generalized time string into SilcTime */
317 SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
320 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
321 unsigned int msecond = 0;
326 memset(ret_time, 0, sizeof(*ret_time));
328 /* Parse the time string */
329 ret = sscanf(generalized_time, "%04u%02u%02u%02u%02u%02u", &year, &month,
330 &day, &hour, &minute, &second);
332 SILC_LOG_DEBUG(("Invalid generalized time string"));
333 silc_set_errno_reason(SILC_ERR_BAD_TIME, "Invalid generalized time string");
337 /* Fill the SilcTime structure */
338 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
340 SILC_LOG_DEBUG(("Incorrect values in generalized time string"));
344 /* Check fractions of second and/or timezone */
346 ret = sscanf(generalized_time + i, "%c", &z);
348 SILC_LOG_DEBUG(("Malformed generalized time string"));
349 silc_set_errno_reason(SILC_ERR_BAD_TIME,
350 "Malformed generalized time string");
355 /* Take fractions of second */
358 ret = sscanf(generalized_time + i, "%u%n", &msecond, &l);
360 SILC_LOG_DEBUG(("Malformed generalized time string"));
361 silc_set_errno_reason(SILC_ERR_BAD_TIME,
362 "Malformed generalized time string");
369 ret_time->msecond = msecond;
372 /* Read optional timezone */
373 if (strlen(generalized_time) < i)
374 sscanf(generalized_time + i, "%c", &z);
377 /* Check timezone if present */
378 if (z == '-' || z == '+') {
379 ret = sscanf(generalized_time + i + 1, "%02u%02u", &hour, &minute);
381 SILC_LOG_DEBUG(("Malformed generalized time string"));
382 silc_set_errno_reason(SILC_ERR_BAD_TIME,
383 "Malformed generalized time string");
387 if (hour > 23 || minute > 60) {
388 silc_set_errno(SILC_ERR_BAD_TIME);
392 ret_time->utc_hour = hour;
393 ret_time->utc_minute = minute;
394 ret_time->utc_east = (z == '-') ? 0 : 1;
400 /* Encode generalized time string */
402 SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
403 SilcUInt32 ret_string_size)
406 memset(ret_string, 0, ret_string_size);
407 ret = silc_snprintf(ret_string, ret_string_size - 1,
408 "%04u%02u%02u%02u%02u%02u",
409 time_val->year, time_val->month,
410 time_val->day, time_val->hour,
411 time_val->minute, time_val->second);
416 if (time_val->msecond) {
417 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
418 ".%lu", (unsigned long)time_val->msecond);
424 if (!time_val->utc_hour && !time_val->utc_minute) {
425 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
430 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
431 "%c%02u%02u", time_val->utc_east ? '+' : '-',
432 time_val->utc_hour, time_val->utc_minute);
441 /* Return TRUE if `smaller' is smaller than `bigger'. */
443 int silc_compare_timeval(struct timeval *t1, struct timeval *t2)
445 SilcInt32 s = t1->tv_sec - t2->tv_sec;
447 return t1->tv_usec - t2->tv_usec;