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 if (ret_time->utc_minute)
160 ret_time->utc_minute /= 60;
161 #endif /* SILC_WIN32 */
166 /* Returns timezone */
168 SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size)
170 SilcTimeStruct curtime;
172 if (timezone_size < 6)
175 if (!silc_time_value(0, &curtime))
178 if (!curtime.utc_hour && curtime.utc_minute)
179 silc_snprintf(timezone, timezone_size, "Z");
180 else if (curtime.utc_minute)
181 silc_snprintf(timezone, timezone_size, "%c%02d:%02d",
182 curtime.utc_east ? '+' : '-', curtime.utc_hour,
185 silc_snprintf(timezone, timezone_size, "%c%02d",
186 curtime.utc_east ? '+' : '-', curtime.utc_hour);
191 /* Returns time from universal time string into SilcTime */
193 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
196 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
201 memset(ret_time, 0, sizeof(*ret_time));
203 /* Parse the time string */
204 ret = sscanf(universal_time, "%02u%02u%02u%02u%02u%02u%c", &year, &month,
205 &day, &hour, &minute, &second, &z);
207 SILC_LOG_DEBUG(("Invalid UTC time string"));
211 /* Fill the SilcTime structure */
212 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
214 SILC_LOG_DEBUG(("Incorrect values in UTC time string"));
219 if (z == '-' || z == '+') {
220 ret = sscanf(universal_time + (ret * 2) + 1, "%02u%02u", &hour, &minute);
222 SILC_LOG_DEBUG(("Malformed UTC time string"));
226 if (hour < 0 || hour > 23)
228 if (minute < 0 || minute > 60)
231 ret_time->utc_hour = hour;
232 ret_time->utc_minute = minute;
233 ret_time->utc_east = (z == '-') ? 0 : 1;
234 } else if (z != 'Z') {
235 SILC_LOG_DEBUG(("Invalid timezone"));
239 /* UTC year must be fixed since it's represented only as YY not YYYY. */
240 ret_time->year += 1900;
241 if (ret_time->year < 1950)
242 ret_time->year += 100;
247 /* Encode universal time string. */
249 SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
250 SilcUInt32 ret_string_size)
253 memset(ret_string, 0, ret_string_size);
254 ret = silc_snprintf(ret_string, ret_string_size - 1,
255 "%02u%02u%02u%02u%02u%02u",
256 time_val->year % 100, time_val->month, time_val->day,
257 time_val->hour, time_val->minute, time_val->second);
262 if (!time_val->utc_hour && !time_val->utc_minute) {
263 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
268 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
269 "%c%02u%02u", time_val->utc_east ? '+' : '-',
270 time_val->utc_hour, time_val->utc_minute);
279 /* Returns time from generalized time string into SilcTime */
281 SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
284 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
285 unsigned int msecond = 0;
290 memset(ret_time, 0, sizeof(*ret_time));
292 /* Parse the time string */
293 ret = sscanf(generalized_time, "%04u%02u%02u%02u%02u%02u", &year, &month,
294 &day, &hour, &minute, &second);
296 SILC_LOG_DEBUG(("Invalid generalized time string"));
300 /* Fill the SilcTime structure */
301 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
303 SILC_LOG_DEBUG(("Incorrect values in generalized time string"));
307 /* Check fractions of second and/or timezone */
309 ret = sscanf(generalized_time + i, "%c", &z);
311 SILC_LOG_DEBUG(("Malformed generalized time string"));
316 /* Take fractions of second */
319 ret = sscanf(generalized_time + i, "%u%n", &msecond, &l);
321 SILC_LOG_DEBUG(("Malformed generalized time string"));
328 ret_time->msecond = msecond;
331 /* Read optional timezone */
332 if (strlen(generalized_time) < i)
333 sscanf(generalized_time + i, "%c", &z);
336 /* Check timezone if present */
337 if (z == '-' || z == '+') {
338 ret = sscanf(generalized_time + i + 1, "%02u%02u", &hour, &minute);
340 SILC_LOG_DEBUG(("Malformed UTC time string"));
344 if (hour < 0 || hour > 23)
346 if (minute < 0 || minute > 60)
349 ret_time->utc_hour = hour;
350 ret_time->utc_minute = minute;
351 ret_time->utc_east = (z == '-') ? 0 : 1;
357 /* Encode generalized time string */
359 SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
360 SilcUInt32 ret_string_size)
363 memset(ret_string, 0, ret_string_size);
364 ret = silc_snprintf(ret_string, ret_string_size - 1,
365 "%04u%02u%02u%02u%02u%02u",
366 time_val->year, time_val->month, time_val->day, time_val->hour,
367 time_val->minute, time_val->second);
372 if (time_val->msecond) {
373 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
374 ".%lu", (unsigned long)time_val->msecond);
380 if (!time_val->utc_hour && !time_val->utc_minute) {
381 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
386 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
387 "%c%02u%02u", time_val->utc_east ? '+' : '-',
388 time_val->utc_hour, time_val->utc_minute);
397 /* Return TRUE if `smaller' is smaller than `bigger'. */
399 int silc_compare_timeval(struct timeval *t1, struct timeval *t2)
401 SilcInt32 s = t1->tv_sec - t2->tv_sec;
403 return t1->tv_usec - t2->tv_usec;