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.
22 /* Fills the SilcTime structure with correct values */
24 static SilcBool silc_time_fill(SilcTime time,
34 if (month < 1 || month > 12)
36 if (day < 1 || day > 31)
49 time->minute = minute;
50 time->second = second;
55 /* Return time since Epoch */
57 SilcInt64 silc_time(void)
59 return (SilcInt64)time(NULL);
62 /* Returns time as string */
64 const char *silc_time_string(SilcInt64 timeval)
72 curtime = (time_t)timeval;
73 return_time = ctime(&curtime);
76 return_time[strlen(return_time) - 1] = '\0';
78 return (const char *)return_time;
81 /* Returns time as SilcTime structure */
83 SilcBool silc_time_value(SilcInt64 timeval, SilcTime ret_time)
91 timeval = silc_time();
93 time = localtime((time_t *)&timeval);
97 memset(ret_time, 0, sizeof(*ret_time));
98 if (!silc_time_fill(ret_time, time->tm_year + 1900, time->tm_mon + 1,
99 time->tm_mday, time->tm_hour, time->tm_min,
103 ret_time->dst = time->tm_isdst ? 1 : 0;
105 ret_time->utc_east = _timezone < 0 ? 1 : 0;
106 ret_time->utc_hour = (ret_time->utc_east ? (-(_timezone)) / 3600 :
108 ret_time->utc_minute = (ret_time->utc_east ? (-(_timezone)) % 3600 :
111 #if defined(HAVE_TZSET)
112 ret_time->utc_east = timezone < 0 ? 1 : 0;
113 ret_time->utc_hour = (ret_time->utc_east ? (-(timezone)) / 3600 :
115 ret_time->utc_minute = (ret_time->utc_east ? (-(timezone)) % 3600 :
117 #endif /* HAVE_TZSET */
118 #endif /* SILC_WIN32 */
123 /* Returns time from universal time string into SilcTime */
125 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
128 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
133 memset(ret_time, 0, sizeof(*ret_time));
135 /* Parse the time string */
136 ret = sscanf(universal_time, "%02u%02u%02u%02u%02u%02u%c", &year, &month,
137 &day, &hour, &minute, &second, &z);
139 SILC_LOG_DEBUG(("Invalid UTC time string"));
143 /* Fill the SilcTime structure */
144 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second);
146 SILC_LOG_DEBUG(("Incorrect values in UTC time string"));
151 if (z == '-' || z == '+') {
152 ret = sscanf(universal_time + (ret * 2) + 1, "%02u%02u", &hour, &minute);
154 SILC_LOG_DEBUG(("Malformed UTC time string"));
158 if (hour < 0 || hour > 23)
160 if (minute < 0 || minute > 60)
163 ret_time->utc_hour = hour;
164 ret_time->utc_minute = minute;
165 ret_time->utc_east = (z == '-') ? 0 : 1;
166 } else if (z != 'Z') {
167 SILC_LOG_DEBUG(("Invalid timezone"));
171 /* UTC year must be fixed since it's represented only as YY not YYYY. */
172 ret_time->year += 1900;
173 if (ret_time->year < 1950)
174 ret_time->year += 100;
179 /* Encode universal time string. */
181 SilcBool silc_time_universal_string(SilcTime timeval, char *ret_string,
182 SilcUInt32 ret_string_size)
185 memset(ret_string, 0, ret_string_size);
186 ret = snprintf(ret_string, ret_string_size - 1,
187 "%02u%02u%02u%02u%02u%02u",
188 timeval->year % 100, timeval->month, timeval->day,
189 timeval->hour, timeval->minute, timeval->second);
194 if (!timeval->utc_hour && !timeval->utc_minute) {
195 ret = snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
200 ret = snprintf(ret_string + len, ret_string_size - 1 - len,
201 "%c%02u%02u", timeval->utc_east ? '+' : '-',
202 timeval->utc_hour, timeval->utc_minute);
211 /* Returns time from generalized time string into SilcTime */
213 SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
216 unsigned int year, month, day, hour = 0, minute = 0, second = 0;
217 unsigned int msecond = 0;
222 memset(ret_time, 0, sizeof(*ret_time));
224 /* Parse the time string */
225 ret = sscanf(generalized_time, "%04u%02u%02u%02u%02u%02u", &year, &month,
226 &day, &hour, &minute, &second);
228 SILC_LOG_DEBUG(("Invalid generalized time string"));
232 /* Fill the SilcTime structure */
233 ret = silc_time_fill(ret_time, year, month, day, hour, minute, second);
235 SILC_LOG_DEBUG(("Incorrect values in generalized time string"));
239 /* Check fractions of second and/or timezone */
241 ret = sscanf(generalized_time + i, "%c", &z);
243 SILC_LOG_DEBUG(("Malformed generalized time string"));
248 /* Take fractions of second */
251 ret = sscanf(generalized_time + i, "%u%n", &msecond, &l);
253 SILC_LOG_DEBUG(("Malformed generalized time string"));
260 ret_time->msecond = msecond;
263 /* Read optional timezone */
264 if (strlen(generalized_time) < i)
265 sscanf(generalized_time + i, "%c", &z);
268 /* Check timezone if present */
269 if (z == '-' || z == '+') {
270 ret = sscanf(generalized_time + i + 1, "%02u%02u", &hour, &minute);
272 SILC_LOG_DEBUG(("Malformed UTC time string"));
276 if (hour < 0 || hour > 23)
278 if (minute < 0 || minute > 60)
281 ret_time->utc_hour = hour;
282 ret_time->utc_minute = minute;
283 ret_time->utc_east = (z == '-') ? 0 : 1;
289 /* Encode generalized time string */
291 SilcBool silc_time_generalized_string(SilcTime timeval, char *ret_string,
292 SilcUInt32 ret_string_size)
295 memset(ret_string, 0, ret_string_size);
296 ret = snprintf(ret_string, ret_string_size - 1,
297 "%04u%02u%02u%02u%02u%02u",
298 timeval->year, timeval->month, timeval->day, timeval->hour,
299 timeval->minute, timeval->second);
304 if (timeval->msecond) {
305 ret = snprintf(ret_string + len, ret_string_size - 1 - len,
306 ".%lu", (unsigned long)timeval->msecond);
312 if (!timeval->utc_hour && !timeval->utc_minute) {
313 ret = snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
318 ret = snprintf(ret_string + len, ret_string_size - 1 - len,
319 "%c%02u%02u", timeval->utc_east ? '+' : '-',
320 timeval->utc_hour, timeval->utc_minute);