5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2003 - 2006 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 * 1000) + (curtime.tv_usec / 1000);
75 /* Return time since Epoch in microseconds */
77 SilcInt64 silc_time_usec(void)
79 struct timeval curtime;
80 silc_gettimeofday(&curtime);
81 return (curtime.tv_sec * 1000000) + curtime.tv_usec;
84 /* Returns time as string */
86 const char *silc_time_string(SilcInt64 time_val)
92 curtime = silc_time();
94 curtime = (time_t)time_val;
95 return_time = ctime(&curtime);
98 return_time[strlen(return_time) - 1] = '\0';
100 return (const char *)return_time;
103 /* Returns time as SilcTime structure */
105 SilcBool silc_time_value(SilcInt64 time_val, SilcTime ret_time)
108 unsigned int msec = 0;
114 time_val = silc_time_msec();
116 msec = time_val % 1000;
119 time = localtime((time_t *)&time_val);
123 memset(ret_time, 0, sizeof(*ret_time));
124 if (!silc_time_fill(ret_time, time->tm_year + 1900, time->tm_mon + 1,
125 time->tm_mday, time->tm_hour, time->tm_min,
129 ret_time->dst = time->tm_isdst ? 1 : 0;
131 ret_time->utc_east = _timezone < 0 ? 1 : 0;
132 ret_time->utc_hour = (ret_time->utc_east ? (-(_timezone)) / 3600 :
134 ret_time->utc_minute = (ret_time->utc_east ? (-(_timezone)) % 3600 :
137 #if defined(HAVE_TZSET)
138 ret_time->utc_east = timezone < 0 ? 1 : 0;
139 ret_time->utc_hour = (ret_time->utc_east ? (-(timezone)) / 3600 :
141 ret_time->utc_minute = (ret_time->utc_east ? (-(timezone)) % 3600 :
143 #endif /* HAVE_TZSET */
144 #endif /* SILC_WIN32 */
149 /* Returns time from universal time string into SilcTime */
151 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
154 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
159 memset(ret_time, 0, sizeof(*ret_time));
161 /* Parse the time string */
162 ret = sscanf(universal_time, "%02u%02u%02u%02u%02u%02u%c", &year, &month,
163 &day, &hour, &minute, &second, &z);
165 SILC_LOG_DEBUG(("Invalid UTC time string"));
169 /* Fill the SilcTime structure */
170 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
172 SILC_LOG_DEBUG(("Incorrect values in UTC time string"));
177 if (z == '-' || z == '+') {
178 ret = sscanf(universal_time + (ret * 2) + 1, "%02u%02u", &hour, &minute);
180 SILC_LOG_DEBUG(("Malformed UTC time string"));
184 if (hour < 0 || hour > 23)
186 if (minute < 0 || minute > 60)
189 ret_time->utc_hour = hour;
190 ret_time->utc_minute = minute;
191 ret_time->utc_east = (z == '-') ? 0 : 1;
192 } else if (z != 'Z') {
193 SILC_LOG_DEBUG(("Invalid timezone"));
197 /* UTC year must be fixed since it's represented only as YY not YYYY. */
198 ret_time->year += 1900;
199 if (ret_time->year < 1950)
200 ret_time->year += 100;
205 /* Encode universal time string. */
207 SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
208 SilcUInt32 ret_string_size)
211 memset(ret_string, 0, ret_string_size);
212 ret = silc_silc_snprintf(ret_string, ret_string_size - 1,
213 "%02u%02u%02u%02u%02u%02u",
214 time_val->year % 100, time_val->month, time_val->day,
215 time_val->hour, time_val->minute, time_val->second);
220 if (!time_val->utc_hour && !time_val->utc_minute) {
221 ret = silc_silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
226 ret = silc_silc_snprintf(ret_string + len, ret_string_size - 1 - len,
227 "%c%02u%02u", time_val->utc_east ? '+' : '-',
228 time_val->utc_hour, time_val->utc_minute);
237 /* Returns time from generalized time string into SilcTime */
239 SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
242 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
243 unsigned int msecond = 0;
248 memset(ret_time, 0, sizeof(*ret_time));
250 /* Parse the time string */
251 ret = sscanf(generalized_time, "%04u%02u%02u%02u%02u%02u", &year, &month,
252 &day, &hour, &minute, &second);
254 SILC_LOG_DEBUG(("Invalid generalized time string"));
258 /* Fill the SilcTime structure */
259 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
261 SILC_LOG_DEBUG(("Incorrect values in generalized time string"));
265 /* Check fractions of second and/or timezone */
267 ret = sscanf(generalized_time + i, "%c", &z);
269 SILC_LOG_DEBUG(("Malformed generalized time string"));
274 /* Take fractions of second */
277 ret = sscanf(generalized_time + i, "%u%n", &msecond, &l);
279 SILC_LOG_DEBUG(("Malformed generalized time string"));
286 ret_time->msecond = msecond;
289 /* Read optional timezone */
290 if (strlen(generalized_time) < i)
291 sscanf(generalized_time + i, "%c", &z);
294 /* Check timezone if present */
295 if (z == '-' || z == '+') {
296 ret = sscanf(generalized_time + i + 1, "%02u%02u", &hour, &minute);
298 SILC_LOG_DEBUG(("Malformed UTC time string"));
302 if (hour < 0 || hour > 23)
304 if (minute < 0 || minute > 60)
307 ret_time->utc_hour = hour;
308 ret_time->utc_minute = minute;
309 ret_time->utc_east = (z == '-') ? 0 : 1;
315 /* Encode generalized time string */
317 SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
318 SilcUInt32 ret_string_size)
321 memset(ret_string, 0, ret_string_size);
322 ret = silc_silc_snprintf(ret_string, ret_string_size - 1,
323 "%04u%02u%02u%02u%02u%02u",
324 time_val->year, time_val->month, time_val->day, time_val->hour,
325 time_val->minute, time_val->second);
330 if (time_val->msecond) {
331 ret = silc_silc_snprintf(ret_string + len, ret_string_size - 1 - len,
332 ".%lu", (unsigned long)time_val->msecond);
338 if (!time_val->utc_hour && !time_val->utc_minute) {
339 ret = silc_silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
344 ret = silc_silc_snprintf(ret_string + len, ret_string_size - 1 - len,
345 "%c%02u%02u", time_val->utc_east ? '+' : '-',
346 time_val->utc_hour, time_val->utc_minute);
355 /* Return TRUE if `smaller' is smaller than `bigger'. */
357 SilcBool silc_compare_timeval(struct timeval *smaller,
358 struct timeval *bigger)
360 if ((smaller->tv_sec < bigger->tv_sec) ||
361 ((smaller->tv_sec == bigger->tv_sec) &&
362 (smaller->tv_usec < bigger->tv_usec)))