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 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 /* Returns timezone */
178 SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size)
180 SilcTimeStruct curtime;
182 if (timezone_size < 6) {
183 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
187 if (!silc_time_value(0, &curtime))
190 if (!curtime.utc_hour && curtime.utc_minute)
191 silc_snprintf(timezone, timezone_size, "Z");
192 else if (curtime.utc_minute)
193 silc_snprintf(timezone, timezone_size, "%c%02d:%02d",
194 curtime.utc_east ? '+' : '-', curtime.utc_hour,
197 silc_snprintf(timezone, timezone_size, "%c%02d",
198 curtime.utc_east ? '+' : '-', curtime.utc_hour);
203 /* Returns time from universal time string into SilcTime */
205 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
208 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
213 memset(ret_time, 0, sizeof(*ret_time));
215 /* Parse the time string */
216 ret = sscanf(universal_time, "%02u%02u%02u%02u%02u%02u%c", &year, &month,
217 &day, &hour, &minute, &second, &z);
219 SILC_LOG_DEBUG(("Invalid UTC time string"));
220 silc_set_errno_reason(SILC_ERR_BAD_TIME, "Invalid UTC time string");
224 /* Fill the SilcTime structure */
225 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
227 SILC_LOG_DEBUG(("Incorrect values in UTC time string"));
232 if (z == '-' || z == '+') {
233 ret = sscanf(universal_time + (ret * 2) + 1, "%02u%02u", &hour, &minute);
235 SILC_LOG_DEBUG(("Malformed UTC time string"));
236 silc_set_errno_reason(SILC_ERR_BAD_TIME, "Malformed UTC time string");
240 if (hour > 23 || minute > 60) {
241 silc_set_errno(SILC_ERR_BAD_TIME);
245 ret_time->utc_hour = hour;
246 ret_time->utc_minute = minute;
247 ret_time->utc_east = (z == '-') ? 0 : 1;
248 } else if (z != 'Z') {
249 SILC_LOG_DEBUG(("Invalid timezone"));
250 silc_set_errno_reason(SILC_ERR_BAD_TIME, "Invalid timezone");
254 /* UTC year must be fixed since it's represented only as YY not YYYY. */
255 ret_time->year += 1900;
256 if (ret_time->year < 1950)
257 ret_time->year += 100;
262 /* Encode universal time string. */
264 SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
265 SilcUInt32 ret_string_size)
269 memset(ret_string, 0, ret_string_size);
270 ret = silc_snprintf(ret_string, ret_string_size - 1,
271 "%02u%02u%02u%02u%02u%02u",
272 time_val->year % 100, time_val->month, time_val->day,
273 time_val->hour, time_val->minute, time_val->second);
278 if (!time_val->utc_hour && !time_val->utc_minute) {
279 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
284 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
285 "%c%02u%02u", time_val->utc_east ? '+' : '-',
286 time_val->utc_hour, time_val->utc_minute);
295 /* Returns time from generalized time string into SilcTime */
297 SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
300 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
301 unsigned int msecond = 0;
306 memset(ret_time, 0, sizeof(*ret_time));
308 /* Parse the time string */
309 ret = sscanf(generalized_time, "%04u%02u%02u%02u%02u%02u", &year, &month,
310 &day, &hour, &minute, &second);
312 SILC_LOG_DEBUG(("Invalid generalized time string"));
313 silc_set_errno_reason(SILC_ERR_BAD_TIME, "Invalid generalized time string");
317 /* Fill the SilcTime structure */
318 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
320 SILC_LOG_DEBUG(("Incorrect values in generalized time string"));
324 /* Check fractions of second and/or timezone */
326 ret = sscanf(generalized_time + i, "%c", &z);
328 SILC_LOG_DEBUG(("Malformed generalized time string"));
329 silc_set_errno_reason(SILC_ERR_BAD_TIME,
330 "Malformed generalized time string");
335 /* Take fractions of second */
338 ret = sscanf(generalized_time + i, "%u%n", &msecond, &l);
340 SILC_LOG_DEBUG(("Malformed generalized time string"));
341 silc_set_errno_reason(SILC_ERR_BAD_TIME,
342 "Malformed generalized time string");
349 ret_time->msecond = msecond;
352 /* Read optional timezone */
353 if (strlen(generalized_time) < i)
354 sscanf(generalized_time + i, "%c", &z);
357 /* Check timezone if present */
358 if (z == '-' || z == '+') {
359 ret = sscanf(generalized_time + i + 1, "%02u%02u", &hour, &minute);
361 SILC_LOG_DEBUG(("Malformed generalized time string"));
362 silc_set_errno_reason(SILC_ERR_BAD_TIME,
363 "Malformed generalized time string");
367 if (hour > 23 || minute > 60) {
368 silc_set_errno(SILC_ERR_BAD_TIME);
372 ret_time->utc_hour = hour;
373 ret_time->utc_minute = minute;
374 ret_time->utc_east = (z == '-') ? 0 : 1;
380 /* Encode generalized time string */
382 SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
383 SilcUInt32 ret_string_size)
386 memset(ret_string, 0, ret_string_size);
387 ret = silc_snprintf(ret_string, ret_string_size - 1,
388 "%04u%02u%02u%02u%02u%02u",
389 time_val->year, time_val->month,
390 time_val->day, time_val->hour,
391 time_val->minute, time_val->second);
396 if (time_val->msecond) {
397 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
398 ".%lu", (unsigned long)time_val->msecond);
404 if (!time_val->utc_hour && !time_val->utc_minute) {
405 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
410 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
411 "%c%02u%02u", time_val->utc_east ? '+' : '-',
412 time_val->utc_hour, time_val->utc_minute);
421 /* Return TRUE if `smaller' is smaller than `bigger'. */
423 int silc_compare_timeval(struct timeval *t1, struct timeval *t2)
425 SilcInt32 s = t1->tv_sec - t2->tv_sec;
427 return t1->tv_usec - t2->tv_usec;