5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2003 - 2005 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.
20 #include "silcincludes.h"
22 /* Return time since Epoch */
24 SilcInt64 silc_time(void)
26 return (SilcInt64)time(NULL);
29 /* Returns time as string */
31 const char *silc_time_string(SilcInt64 timeval)
39 curtime = (time_t)timeval;
40 return_time = ctime(&curtime);
43 return_time[strlen(return_time) - 1] = '\0';
45 return (const char *)return_time;
48 /* Returns time as SilcTime structure */
50 bool silc_time_value(SilcInt64 timeval, SilcTime ret_time)
58 timeval = silc_time();
60 time = localtime((time_t *)&timeval);
64 memset(ret_time, 0, sizeof(*ret_time));
65 ret_time->year = time->tm_year + 1900;
66 ret_time->month = time->tm_mon + 1;
67 ret_time->day = time->tm_mday;
68 ret_time->hour = time->tm_hour;
69 ret_time->minute = time->tm_min;
70 ret_time->second = time->tm_sec;
71 ret_time->dst = time->tm_isdst ? 1 : 0;
74 ret_time->utc_east = _timezone < 0 ? 1 : 0;
75 ret_time->utc_hour = (ret_time->utc_east ? (-(_timezone)) / 3600 :
77 ret_time->utc_minute = (ret_time->utc_east ? (-(_timezone)) % 3600 :
80 #if defined(HAVE_TZSET)
81 ret_time->utc_east = timezone < 0 ? 1 : 0;
82 ret_time->utc_hour = (ret_time->utc_east ? (-(timezone)) / 3600 :
84 ret_time->utc_minute = (ret_time->utc_east ? (-(timezone)) % 3600 :
86 #endif /* HAVE_TZSET */
87 #endif /* SILC_WIN32 */
92 /* Fills the SilcTime structure with correct values */
94 static bool silc_time_fill(SilcTime time,
104 if (month < 1 || month > 12)
106 if (day < 1 || day > 31)
119 time->minute = minute;
120 time->second = second;
125 /* Returns time from universal time string into SilcTime */
127 bool silc_time_universal(const char *universal_time, SilcTime ret_time)
130 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
135 memset(ret_time, 0, sizeof(*ret_time));
137 /* Parse the time string */
138 ret = sscanf(universal_time, "%02u%02u%02u%02u%02u%02u%c", &year, &month,
139 &day, &hour, &minute, &second, &z);
141 SILC_LOG_DEBUG(("Invalid UTC time string"));
145 /* Fill the SilcTime structure */
146 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second);
148 SILC_LOG_DEBUG(("Incorrect values in UTC time string"));
153 if (z == '-' || z == '+') {
154 ret = sscanf(universal_time + (ret * 2) + 1, "%02u%02u", &hour, &minute);
156 SILC_LOG_DEBUG(("Malformed UTC time string"));
160 if (hour < 0 || hour > 23)
162 if (minute < 0 || minute > 60)
165 ret_time->utc_hour = hour;
166 ret_time->utc_minute = minute;
167 ret_time->utc_east = (z == '-') ? 0 : 1;
168 } else if (z != 'Z') {
169 SILC_LOG_DEBUG(("Invalid timezone"));
173 /* UTC year must be fixed since it's represented only as YY not YYYY. */
174 ret_time->year += 1900;
175 if (ret_time->year < 1950)
176 ret_time->year += 100;
181 /* Encode universal time string. */
183 bool silc_time_universal_string(SilcTime timeval, char *ret_string,
184 SilcUInt32 ret_string_size)
187 memset(ret_string, 0, ret_string_size);
188 ret = snprintf(ret_string, ret_string_size - 1,
189 "%02u%02u%02u%02u%02u%02u",
190 timeval->year % 100, timeval->month, timeval->day,
191 timeval->hour, timeval->minute, timeval->second);
196 if (!timeval->utc_hour && !timeval->utc_minute) {
197 ret = snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
202 ret = snprintf(ret_string + len, ret_string_size - 1 - len,
203 "%c%02u%02u", timeval->utc_east ? '+' : '-',
204 timeval->utc_hour, timeval->utc_minute);
213 /* Returns time from generalized time string into SilcTime */
215 bool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
218 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
219 unsigned int msecond = 0;
224 memset(ret_time, 0, sizeof(*ret_time));
226 /* Parse the time string */
227 ret = sscanf(generalized_time, "%04u%02u%02u%02u%02u%02u", &year, &month,
228 &day, &hour, &minute, &second);
230 SILC_LOG_DEBUG(("Invalid generalized time string"));
234 /* Fill the SilcTime structure */
235 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second);
237 SILC_LOG_DEBUG(("Incorrect values in generalized time string"));
241 /* Check fractions of second and/or timezone */
243 ret = sscanf(generalized_time + i, "%c", &z);
245 SILC_LOG_DEBUG(("Malformed generalized time string"));
250 /* Take fractions of second */
253 ret = sscanf(generalized_time + i, "%u%n", &msecond, &l);
255 SILC_LOG_DEBUG(("Malformed generalized time string"));
262 ret_time->msecond = msecond;
265 /* Read optional timezone */
266 if (strlen(generalized_time) < i)
267 sscanf(generalized_time + i, "%c", &z);
270 /* Check timezone if present */
271 if (z == '-' || z == '+') {
272 ret = sscanf(generalized_time + i + 1, "%02u%02u", &hour, &minute);
274 SILC_LOG_DEBUG(("Malformed UTC time string"));
278 if (hour < 0 || hour > 23)
280 if (minute < 0 || minute > 60)
283 ret_time->utc_hour = hour;
284 ret_time->utc_minute = minute;
285 ret_time->utc_east = (z == '-') ? 0 : 1;
291 /* Encode generalized time string */
293 bool silc_time_generalized_string(SilcTime timeval, char *ret_string,
294 SilcUInt32 ret_string_size)
297 memset(ret_string, 0, ret_string_size);
298 ret = snprintf(ret_string, ret_string_size - 1,
299 "%04u%02u%02u%02u%02u%02u",
300 timeval->year, timeval->month, timeval->day, timeval->hour,
301 timeval->minute, timeval->second);
306 if (timeval->msecond) {
307 ret = snprintf(ret_string + len, ret_string_size - 1 - len,
308 ".%lu", (unsigned long)timeval->msecond);
314 if (!timeval->utc_hour && !timeval->utc_minute) {
315 ret = snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
320 ret = snprintf(ret_string + len, ret_string_size - 1 - len,
321 "%c%02u%02u", timeval->utc_east ? '+' : '-',
322 timeval->utc_hour, timeval->utc_minute);