Added SILC Server library.
[silc.git] / lib / silcutil / silctime.c
1 /*
2
3   silctime.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2003 - 2005 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 #include "silc.h"
21
22 /* Return time since Epoch */
23
24 SilcInt64 silc_time(void)
25 {
26   return (SilcInt64)time(NULL);
27 }
28
29 /* Returns time as string */
30
31 const char *silc_time_string(SilcInt64 timeval)
32 {
33   time_t curtime;
34   char *return_time;
35
36   if (!timeval)
37     curtime = time(NULL);
38   else
39     curtime = (time_t)timeval;
40   return_time = ctime(&curtime);
41   if (!return_time)
42     return NULL;
43   return_time[strlen(return_time) - 1] = '\0';
44
45   return (const char *)return_time;
46 }
47
48 /* Returns time as SilcTime structure */
49
50 SilcBool silc_time_value(SilcInt64 timeval, SilcTime ret_time)
51 {
52   struct tm *time;
53
54   if (!ret_time)
55     return TRUE;
56
57   if (!timeval)
58     timeval = silc_time();
59
60   time = localtime((time_t *)&timeval);
61   if (!time)
62     return FALSE;
63
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;
72
73 #ifdef SILC_WIN32
74   ret_time->utc_east   = _timezone < 0 ? 1 : 0;
75   ret_time->utc_hour   = (ret_time->utc_east ? (-(_timezone)) / 3600 :
76                           _timezone / 3600);
77   ret_time->utc_minute = (ret_time->utc_east ? (-(_timezone)) % 3600 :
78                           _timezone % 3600);
79 #else
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 :
83                           timezone / 3600);
84   ret_time->utc_minute = (ret_time->utc_east ? (-(timezone)) % 3600 :
85                           timezone % 3600);
86 #endif /* HAVE_TZSET */
87 #endif /* SILC_WIN32 */
88
89   return TRUE;
90 }
91
92 /* Fills the SilcTime structure with correct values */
93
94 static SilcBool silc_time_fill(SilcTime time,
95                                unsigned int year,
96                                unsigned int month,
97                                unsigned int day,
98                                unsigned int hour,
99                                unsigned int minute,
100                                unsigned int second)
101 {
102   if (year > 8191)
103     return FALSE;
104   if (month < 1 || month > 12)
105     return FALSE;
106   if (day < 1 || day > 31)
107     return FALSE;
108   if (hour > 23)
109     return FALSE;
110   if (minute > 60)
111     return FALSE;
112   if (second > 61)
113     return FALSE;
114
115   time->year = year;
116   time->month = month;
117   time->day = day;
118   time->hour = hour;
119   time->minute = minute;
120   time->second = second;
121
122   return TRUE;
123 }
124
125 /* Returns time from universal time string into SilcTime */
126
127 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
128 {
129   int ret;
130   unsigned int year, month, day, hour = 0, minute = 0, second = 0;
131   unsigned char z = 0;
132
133   if (!ret_time)
134     return TRUE;
135   memset(ret_time, 0, sizeof(*ret_time));
136
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);
140   if (ret < 3) {
141     SILC_LOG_DEBUG(("Invalid UTC time string"));
142     return FALSE;
143   }
144
145   /* Fill the SilcTime structure */
146   ret = silc_time_fill(ret_time, year, month, day, hour, minute, second);
147   if (!ret) {
148     SILC_LOG_DEBUG(("Incorrect values in UTC time string"));
149     return FALSE;
150   }
151
152   /* Check timezone */
153   if (z == '-' || z == '+') {
154     ret = sscanf(universal_time + (ret * 2) + 1, "%02u%02u", &hour, &minute);
155     if (ret != 2) {
156       SILC_LOG_DEBUG(("Malformed UTC time string"));
157       return FALSE;
158     }
159
160     if (hour < 0 || hour > 23)
161       return FALSE;
162     if (minute < 0 || minute > 60)
163       return FALSE;
164
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"));
170     return FALSE;
171   }
172
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;
177
178   return TRUE;
179 }
180
181 /* Encode universal time string. */
182
183 SilcBool silc_time_universal_string(SilcTime timeval, char *ret_string,
184                                     SilcUInt32 ret_string_size)
185 {
186   int ret, len = 0;
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);
192   if (ret < 0)
193     return FALSE;
194   len += ret;
195
196   if (!timeval->utc_hour && !timeval->utc_minute) {
197     ret = snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
198     if (ret < 0)
199       return FALSE;
200     len += ret;
201   } else {
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);
205     if (ret < 0)
206       return FALSE;
207     len += ret;
208   }
209
210   return TRUE;
211 }
212
213 /* Returns time from generalized time string into SilcTime */
214
215 SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
216 {
217   int ret, i;
218   unsigned int year, month, day, hour = 0, minute = 0, second = 0;
219   unsigned int msecond = 0;
220   unsigned char z = 0;
221
222   if (!ret_time)
223     return TRUE;
224   memset(ret_time, 0, sizeof(*ret_time));
225
226   /* Parse the time string */
227   ret = sscanf(generalized_time, "%04u%02u%02u%02u%02u%02u", &year, &month,
228                &day, &hour, &minute, &second);
229   if (ret < 3) {
230     SILC_LOG_DEBUG(("Invalid generalized time string"));
231     return FALSE;
232   }
233
234   /* Fill the SilcTime structure */
235   ret = silc_time_fill(ret_time, year, month, day, hour, minute, second);
236   if (!ret) {
237     SILC_LOG_DEBUG(("Incorrect values in generalized time string"));
238     return FALSE;
239   }
240
241   /* Check fractions of second and/or timezone */
242   i = ret * 4;
243   ret = sscanf(generalized_time + i, "%c", &z);
244   if (ret != 1) {
245     SILC_LOG_DEBUG(("Malformed generalized time string"));
246     return FALSE;
247   }
248
249   if (z == '.') {
250     /* Take fractions of second */
251     int l;
252     i++;
253     ret = sscanf(generalized_time + i, "%u%n", &msecond, &l);
254     if (ret != 1) {
255       SILC_LOG_DEBUG(("Malformed generalized time string"));
256       return FALSE;
257     }
258     while (l > 4) {
259       msecond /= 10;
260       l--;
261     }
262     ret_time->msecond = msecond;
263     i += l;
264
265     /* Read optional timezone */
266     if (strlen(generalized_time) < i)
267       sscanf(generalized_time + i, "%c", &z);
268   }
269
270   /* Check timezone if present */
271   if (z == '-' || z == '+') {
272     ret = sscanf(generalized_time + i + 1, "%02u%02u", &hour, &minute);
273     if (ret != 2) {
274       SILC_LOG_DEBUG(("Malformed UTC time string"));
275       return FALSE;
276     }
277
278     if (hour < 0 || hour > 23)
279       return FALSE;
280     if (minute < 0 || minute > 60)
281       return FALSE;
282
283     ret_time->utc_hour   = hour;
284     ret_time->utc_minute = minute;
285     ret_time->utc_east   = (z == '-') ? 0 : 1;
286   }
287
288   return TRUE;
289 }
290
291 /* Encode generalized time string */
292
293 SilcBool silc_time_generalized_string(SilcTime timeval, char *ret_string,
294                                       SilcUInt32 ret_string_size)
295 {
296   int len = 0, ret;
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);
302   if (ret < 0)
303     return FALSE;
304   len += ret;
305
306   if (timeval->msecond) {
307     ret = snprintf(ret_string + len, ret_string_size - 1 - len,
308                    ".%lu", (unsigned long)timeval->msecond);
309     if (ret < 0)
310       return FALSE;
311     len += ret;
312   }
313
314   if (!timeval->utc_hour && !timeval->utc_minute) {
315     ret = snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
316     if (ret < 0)
317       return FALSE;
318     len += ret;
319   } else {
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);
323     if (ret < 0)
324       return FALSE;
325     len += ret;
326   }
327
328   return TRUE;
329 }