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;
116 time_val = silc_time_msec();
118 msec = (SilcUInt64)time_val % (SilcUInt64)1000;
119 timeval = (time_t)((SilcUInt64)time_val / (SilcUInt64)1000);
121 t = localtime(&timeval);
125 memset(ret_time, 0, sizeof(*ret_time));
126 if (!silc_time_fill(ret_time, t->tm_year + 1900, t->tm_mon + 1,
127 t->tm_mday, t->tm_hour, t->tm_min,
131 ret_time->dst = t->tm_isdst ? 1 : 0;
134 ret_time->utc_east = _timezone < 0 ? 1 : 0;
135 ret_time->utc_hour = (ret_time->utc_east ? (-(_timezone)) / 3600 :
137 ret_time->utc_minute = (ret_time->utc_east ? (-(_timezone)) % 3600 :
140 #if defined(HAVE_TIMEZONE)
141 ret_time->utc_east = timezone < 0 ? 1 : 0;
142 #elif defined(HAVE_TM_GMTOFF)
143 ret_time->utc_east = t->tm_gmtoff > 0 ? 1 : 0;
144 #elif defined(HAVE___TM_GMTOFF)
145 ret_time->utc_east = t->__tm_gmtoff > 0 ? 1 : 0;
146 #elif defined(HAVE___TM_GMTOFF__)
147 ret_time->utc_east = t->__tm_gmtoff__ > 0 ? 1 : 0;
148 #endif /* HAVE_TIMEZONE */
150 #if defined(HAVE_TIMEZONE)
151 ret_time->utc_hour = (ret_time->utc_east ? (-(timezone)) / 3600 :
154 ret_time->utc_hour++;
155 ret_time->utc_minute = (ret_time->utc_east ? (-(timezone)) % 3600 :
157 #elif defined(HAVE_GMTIME)
158 t = gmtime(&timeval);
160 ret_time->utc_hour = (ret_time->utc_east
161 ? ret_time->hour - t->tm_hour
162 : ret_time->hour + t->tm_hour);
163 ret_time->utc_minute = (ret_time->utc_east
164 ? ret_time->minute - t->tm_min
165 : ret_time->minute + t->tm_min);
167 #endif /* HAVE_TIMEZONE */
168 #endif /* SILC_WIN32 */
173 /* Returns timezone */
175 SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size)
177 SilcTimeStruct curtime;
179 if (timezone_size < 6)
182 if (!silc_time_value(0, &curtime))
185 if (!curtime.utc_hour && curtime.utc_minute)
186 silc_snprintf(timezone, timezone_size, "Z");
187 else if (curtime.utc_minute)
188 silc_snprintf(timezone, timezone_size, "%c%02d:%02d",
189 curtime.utc_east ? '+' : '-', curtime.utc_hour,
192 silc_snprintf(timezone, timezone_size, "%c%02d",
193 curtime.utc_east ? '+' : '-', curtime.utc_hour);
198 /* Returns time from universal time string into SilcTime */
200 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
203 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
208 memset(ret_time, 0, sizeof(*ret_time));
210 /* Parse the time string */
211 ret = sscanf(universal_time, "%02u%02u%02u%02u%02u%02u%c", &year, &month,
212 &day, &hour, &minute, &second, &z);
214 SILC_LOG_DEBUG(("Invalid UTC time string"));
218 /* Fill the SilcTime structure */
219 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
221 SILC_LOG_DEBUG(("Incorrect values in UTC time string"));
226 if (z == '-' || z == '+') {
227 ret = sscanf(universal_time + (ret * 2) + 1, "%02u%02u", &hour, &minute);
229 SILC_LOG_DEBUG(("Malformed UTC time string"));
233 if (hour < 0 || hour > 23)
235 if (minute < 0 || minute > 60)
238 ret_time->utc_hour = hour;
239 ret_time->utc_minute = minute;
240 ret_time->utc_east = (z == '-') ? 0 : 1;
241 } else if (z != 'Z') {
242 SILC_LOG_DEBUG(("Invalid timezone"));
246 /* UTC year must be fixed since it's represented only as YY not YYYY. */
247 ret_time->year += 1900;
248 if (ret_time->year < 1950)
249 ret_time->year += 100;
254 /* Encode universal time string. */
256 SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
257 SilcUInt32 ret_string_size)
260 memset(ret_string, 0, ret_string_size);
261 ret = silc_snprintf(ret_string, ret_string_size - 1,
262 "%02u%02u%02u%02u%02u%02u",
263 time_val->year % 100, time_val->month, time_val->day,
264 time_val->hour, time_val->minute, time_val->second);
269 if (!time_val->utc_hour && !time_val->utc_minute) {
270 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
275 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
276 "%c%02u%02u", time_val->utc_east ? '+' : '-',
277 time_val->utc_hour, time_val->utc_minute);
286 /* Returns time from generalized time string into SilcTime */
288 SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
291 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
292 unsigned int msecond = 0;
297 memset(ret_time, 0, sizeof(*ret_time));
299 /* Parse the time string */
300 ret = sscanf(generalized_time, "%04u%02u%02u%02u%02u%02u", &year, &month,
301 &day, &hour, &minute, &second);
303 SILC_LOG_DEBUG(("Invalid generalized time string"));
307 /* Fill the SilcTime structure */
308 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
310 SILC_LOG_DEBUG(("Incorrect values in generalized time string"));
314 /* Check fractions of second and/or timezone */
316 ret = sscanf(generalized_time + i, "%c", &z);
318 SILC_LOG_DEBUG(("Malformed generalized time string"));
323 /* Take fractions of second */
326 ret = sscanf(generalized_time + i, "%u%n", &msecond, &l);
328 SILC_LOG_DEBUG(("Malformed generalized time string"));
335 ret_time->msecond = msecond;
338 /* Read optional timezone */
339 if (strlen(generalized_time) < i)
340 sscanf(generalized_time + i, "%c", &z);
343 /* Check timezone if present */
344 if (z == '-' || z == '+') {
345 ret = sscanf(generalized_time + i + 1, "%02u%02u", &hour, &minute);
347 SILC_LOG_DEBUG(("Malformed UTC time string"));
351 if (hour < 0 || hour > 23)
353 if (minute < 0 || minute > 60)
356 ret_time->utc_hour = hour;
357 ret_time->utc_minute = minute;
358 ret_time->utc_east = (z == '-') ? 0 : 1;
364 /* Encode generalized time string */
366 SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
367 SilcUInt32 ret_string_size)
370 memset(ret_string, 0, ret_string_size);
371 ret = silc_snprintf(ret_string, ret_string_size - 1,
372 "%04u%02u%02u%02u%02u%02u",
373 time_val->year, time_val->month, time_val->day, time_val->hour,
374 time_val->minute, time_val->second);
379 if (time_val->msecond) {
380 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
381 ".%lu", (unsigned long)time_val->msecond);
387 if (!time_val->utc_hour && !time_val->utc_minute) {
388 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
393 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
394 "%c%02u%02u", time_val->utc_east ? '+' : '-',
395 time_val->utc_hour, time_val->utc_minute);
404 /* Return TRUE if `smaller' is smaller than `bigger'. */
406 int silc_compare_timeval(struct timeval *t1, struct timeval *t2)
408 SilcInt32 s = t1->tv_sec - t2->tv_sec;
410 return t1->tv_usec - t2->tv_usec;