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 * (SilcUInt64)1000) +
73 (curtime.tv_usec / (SilcUInt64)1000);
76 /* Return time since Epoch in microseconds */
78 SilcInt64 silc_time_usec(void)
80 struct timeval curtime;
81 silc_gettimeofday(&curtime);
82 return (curtime.tv_sec * (SilcUInt64)1000000) + curtime.tv_usec;
85 /* Returns time as string */
87 const char *silc_time_string(SilcInt64 time_val)
93 curtime = silc_time();
95 curtime = (time_t)time_val;
96 return_time = ctime(&curtime);
99 return_time[strlen(return_time) - 1] = '\0';
101 return (const char *)return_time;
104 /* Returns time as SilcTime structure */
106 SilcBool silc_time_value(SilcInt64 time_val, SilcTime ret_time)
109 unsigned int msec = 0;
117 time_val = silc_time_msec();
119 msec = (SilcUInt64)time_val % (SilcUInt64)1000;
120 timeval = (time_t)((SilcUInt64)time_val / (SilcUInt64)1000);
122 t = localtime(&timeval);
126 memset(ret_time, 0, sizeof(*ret_time));
127 if (!silc_time_fill(ret_time, t->tm_year + 1900, t->tm_mon + 1,
128 t->tm_mday, t->tm_hour, t->tm_min,
132 ret_time->dst = t->tm_isdst ? 1 : 0;
135 ret_time->utc_east = _timezone < 0 ? 1 : 0;
136 ret_time->utc_hour = (ret_time->utc_east ? (-(_timezone)) / 3600 :
138 ret_time->utc_minute = (ret_time->utc_east ? (-(_timezone)) % 3600 :
141 #if defined(HAVE_TIMEZONE)
142 ret_time->utc_east = timezone < 0 ? 1 : 0;
146 #elif defined(HAVE_TM_GMTOFF)
147 ret_time->utc_east = t->tm_gmtoff > 0 ? 1 : 0;
149 #elif defined(HAVE___TM_GMTOFF)
150 ret_time->utc_east = t->__tm_gmtoff > 0 ? 1 : 0;
151 ctz = -t->__tm_gmtoff;
152 #elif defined(HAVE___TM_GMTOFF__)
153 ret_time->utc_east = t->__tm_gmtoff__ > 0 ? 1 : 0;
154 ctz = -t->__tm_gmtoff__;
155 #endif /* HAVE_TIMEZONE */
157 ret_time->utc_hour = (ret_time->utc_east ? (-(ctz)) / 3600 : ctz / 3600);
158 ret_time->utc_minute = (ret_time->utc_east ? (-(ctz)) % 3600 : ctz % 3600);
159 #endif /* SILC_WIN32 */
161 if (ret_time->utc_minute)
162 ret_time->utc_minute /= 60;
167 /* Returns timezone */
169 SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size)
171 SilcTimeStruct curtime;
173 if (timezone_size < 6)
176 if (!silc_time_value(0, &curtime))
179 if (!curtime.utc_hour && curtime.utc_minute)
180 silc_snprintf(timezone, timezone_size, "Z");
181 else if (curtime.utc_minute)
182 silc_snprintf(timezone, timezone_size, "%c%02d:%02d",
183 curtime.utc_east ? '+' : '-', curtime.utc_hour,
186 silc_snprintf(timezone, timezone_size, "%c%02d",
187 curtime.utc_east ? '+' : '-', curtime.utc_hour);
192 /* Returns time from universal time string into SilcTime */
194 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
197 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
202 memset(ret_time, 0, sizeof(*ret_time));
204 /* Parse the time string */
205 ret = sscanf(universal_time, "%02u%02u%02u%02u%02u%02u%c", &year, &month,
206 &day, &hour, &minute, &second, &z);
208 SILC_LOG_DEBUG(("Invalid UTC time string"));
212 /* Fill the SilcTime structure */
213 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
215 SILC_LOG_DEBUG(("Incorrect values in UTC time string"));
220 if (z == '-' || z == '+') {
221 ret = sscanf(universal_time + (ret * 2) + 1, "%02u%02u", &hour, &minute);
223 SILC_LOG_DEBUG(("Malformed UTC time string"));
227 if (hour < 0 || hour > 23)
229 if (minute < 0 || minute > 60)
232 ret_time->utc_hour = hour;
233 ret_time->utc_minute = minute;
234 ret_time->utc_east = (z == '-') ? 0 : 1;
235 } else if (z != 'Z') {
236 SILC_LOG_DEBUG(("Invalid timezone"));
240 /* UTC year must be fixed since it's represented only as YY not YYYY. */
241 ret_time->year += 1900;
242 if (ret_time->year < 1950)
243 ret_time->year += 100;
248 /* Encode universal time string. */
250 SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
251 SilcUInt32 ret_string_size)
254 memset(ret_string, 0, ret_string_size);
255 ret = silc_snprintf(ret_string, ret_string_size - 1,
256 "%02u%02u%02u%02u%02u%02u",
257 time_val->year % 100, time_val->month, time_val->day,
258 time_val->hour, time_val->minute, time_val->second);
263 if (!time_val->utc_hour && !time_val->utc_minute) {
264 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
269 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
270 "%c%02u%02u", time_val->utc_east ? '+' : '-',
271 time_val->utc_hour, time_val->utc_minute);
280 /* Returns time from generalized time string into SilcTime */
282 SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
285 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
286 unsigned int msecond = 0;
291 memset(ret_time, 0, sizeof(*ret_time));
293 /* Parse the time string */
294 ret = sscanf(generalized_time, "%04u%02u%02u%02u%02u%02u", &year, &month,
295 &day, &hour, &minute, &second);
297 SILC_LOG_DEBUG(("Invalid generalized time string"));
301 /* Fill the SilcTime structure */
302 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
304 SILC_LOG_DEBUG(("Incorrect values in generalized time string"));
308 /* Check fractions of second and/or timezone */
310 ret = sscanf(generalized_time + i, "%c", &z);
312 SILC_LOG_DEBUG(("Malformed generalized time string"));
317 /* Take fractions of second */
320 ret = sscanf(generalized_time + i, "%u%n", &msecond, &l);
322 SILC_LOG_DEBUG(("Malformed generalized time string"));
329 ret_time->msecond = msecond;
332 /* Read optional timezone */
333 if (strlen(generalized_time) < i)
334 sscanf(generalized_time + i, "%c", &z);
337 /* Check timezone if present */
338 if (z == '-' || z == '+') {
339 ret = sscanf(generalized_time + i + 1, "%02u%02u", &hour, &minute);
341 SILC_LOG_DEBUG(("Malformed UTC time string"));
345 if (hour < 0 || hour > 23)
347 if (minute < 0 || minute > 60)
350 ret_time->utc_hour = hour;
351 ret_time->utc_minute = minute;
352 ret_time->utc_east = (z == '-') ? 0 : 1;
358 /* Encode generalized time string */
360 SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
361 SilcUInt32 ret_string_size)
364 memset(ret_string, 0, ret_string_size);
365 ret = silc_snprintf(ret_string, ret_string_size - 1,
366 "%04u%02u%02u%02u%02u%02u",
367 time_val->year, time_val->month, time_val->day, time_val->hour,
368 time_val->minute, time_val->second);
373 if (time_val->msecond) {
374 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
375 ".%lu", (unsigned long)time_val->msecond);
381 if (!time_val->utc_hour && !time_val->utc_minute) {
382 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
387 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
388 "%c%02u%02u", time_val->utc_east ? '+' : '-',
389 time_val->utc_hour, time_val->utc_minute);
398 /* Return TRUE if `smaller' is smaller than `bigger'. */
400 int silc_compare_timeval(struct timeval *t1, struct timeval *t2)
402 SilcInt32 s = t1->tv_sec - t2->tv_sec;
404 return t1->tv_usec - t2->tv_usec;