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 * 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;
115 time_val = silc_time_msec();
117 msec = (SilcUInt64)time_val % (SilcUInt64)1000;
118 timeval = (time_t)((SilcUInt64)time_val / (SilcUInt64)1000);
120 time = localtime(&timeval);
124 memset(ret_time, 0, sizeof(*ret_time));
125 if (!silc_time_fill(ret_time, time->tm_year + 1900, time->tm_mon + 1,
126 time->tm_mday, time->tm_hour, time->tm_min,
130 ret_time->dst = time->tm_isdst ? 1 : 0;
132 ret_time->utc_east = _timezone < 0 ? 1 : 0;
133 ret_time->utc_hour = (ret_time->utc_east ? (-(_timezone)) / 3600 :
135 ret_time->utc_minute = (ret_time->utc_east ? (-(_timezone)) % 3600 :
138 #if defined(HAVE_TZSET)
139 ret_time->utc_east = timezone < 0 ? 1 : 0;
140 ret_time->utc_hour = (ret_time->utc_east ? (-(timezone)) / 3600 :
142 ret_time->utc_minute = (ret_time->utc_east ? (-(timezone)) % 3600 :
144 #endif /* HAVE_TZSET */
145 #endif /* SILC_WIN32 */
150 /* Returns timezone */
152 SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size)
154 SilcTimeStruct curtime;
156 if (timezone_size < 6)
159 if (!silc_time_value(0, &curtime))
162 if (!curtime.utc_hour && curtime.utc_minute)
163 silc_snprintf(timezone, timezone_size, "Z");
164 else if (curtime.utc_minute)
165 silc_snprintf(timezone, timezone_size, "%c%02d:%02d",
166 curtime.utc_east ? '+' : '-', curtime.utc_hour,
169 silc_snprintf(timezone, timezone_size, "%c%02d",
170 curtime.utc_east ? '+' : '-', curtime.utc_hour);
175 /* Returns time from universal time string into SilcTime */
177 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
180 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
185 memset(ret_time, 0, sizeof(*ret_time));
187 /* Parse the time string */
188 ret = sscanf(universal_time, "%02u%02u%02u%02u%02u%02u%c", &year, &month,
189 &day, &hour, &minute, &second, &z);
191 SILC_LOG_DEBUG(("Invalid UTC time string"));
195 /* Fill the SilcTime structure */
196 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
198 SILC_LOG_DEBUG(("Incorrect values in UTC time string"));
203 if (z == '-' || z == '+') {
204 ret = sscanf(universal_time + (ret * 2) + 1, "%02u%02u", &hour, &minute);
206 SILC_LOG_DEBUG(("Malformed UTC time string"));
210 if (hour < 0 || hour > 23)
212 if (minute < 0 || minute > 60)
215 ret_time->utc_hour = hour;
216 ret_time->utc_minute = minute;
217 ret_time->utc_east = (z == '-') ? 0 : 1;
218 } else if (z != 'Z') {
219 SILC_LOG_DEBUG(("Invalid timezone"));
223 /* UTC year must be fixed since it's represented only as YY not YYYY. */
224 ret_time->year += 1900;
225 if (ret_time->year < 1950)
226 ret_time->year += 100;
231 /* Encode universal time string. */
233 SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
234 SilcUInt32 ret_string_size)
237 memset(ret_string, 0, ret_string_size);
238 ret = silc_snprintf(ret_string, ret_string_size - 1,
239 "%02u%02u%02u%02u%02u%02u",
240 time_val->year % 100, time_val->month, time_val->day,
241 time_val->hour, time_val->minute, time_val->second);
246 if (!time_val->utc_hour && !time_val->utc_minute) {
247 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
252 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
253 "%c%02u%02u", time_val->utc_east ? '+' : '-',
254 time_val->utc_hour, time_val->utc_minute);
263 /* Returns time from generalized time string into SilcTime */
265 SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
268 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
269 unsigned int msecond = 0;
274 memset(ret_time, 0, sizeof(*ret_time));
276 /* Parse the time string */
277 ret = sscanf(generalized_time, "%04u%02u%02u%02u%02u%02u", &year, &month,
278 &day, &hour, &minute, &second);
280 SILC_LOG_DEBUG(("Invalid generalized time string"));
284 /* Fill the SilcTime structure */
285 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
287 SILC_LOG_DEBUG(("Incorrect values in generalized time string"));
291 /* Check fractions of second and/or timezone */
293 ret = sscanf(generalized_time + i, "%c", &z);
295 SILC_LOG_DEBUG(("Malformed generalized time string"));
300 /* Take fractions of second */
303 ret = sscanf(generalized_time + i, "%u%n", &msecond, &l);
305 SILC_LOG_DEBUG(("Malformed generalized time string"));
312 ret_time->msecond = msecond;
315 /* Read optional timezone */
316 if (strlen(generalized_time) < i)
317 sscanf(generalized_time + i, "%c", &z);
320 /* Check timezone if present */
321 if (z == '-' || z == '+') {
322 ret = sscanf(generalized_time + i + 1, "%02u%02u", &hour, &minute);
324 SILC_LOG_DEBUG(("Malformed UTC time string"));
328 if (hour < 0 || hour > 23)
330 if (minute < 0 || minute > 60)
333 ret_time->utc_hour = hour;
334 ret_time->utc_minute = minute;
335 ret_time->utc_east = (z == '-') ? 0 : 1;
341 /* Encode generalized time string */
343 SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
344 SilcUInt32 ret_string_size)
347 memset(ret_string, 0, ret_string_size);
348 ret = silc_snprintf(ret_string, ret_string_size - 1,
349 "%04u%02u%02u%02u%02u%02u",
350 time_val->year, time_val->month, time_val->day, time_val->hour,
351 time_val->minute, time_val->second);
356 if (time_val->msecond) {
357 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
358 ".%lu", (unsigned long)time_val->msecond);
364 if (!time_val->utc_hour && !time_val->utc_minute) {
365 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
370 ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
371 "%c%02u%02u", time_val->utc_east ? '+' : '-',
372 time_val->utc_hour, time_val->utc_minute);
381 /* Return TRUE if `smaller' is smaller than `bigger'. */
383 int silc_compare_timeval(struct timeval *t1, struct timeval *t2)
385 SilcInt32 s = t1->tv_sec - t2->tv_sec;
387 return t1->tv_usec - t2->tv_usec;