Kannel: Open Source WAP and SMS gateway  svn-r5335
date.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /* date.c - utility functions for handling times and dates
58  *
59  * Richard Braakman
60  */
61 
62 #include <unistd.h>
63 #include <ctype.h>
64 #include <string.h>
65 
66 #include "gwlib.h"
67 
68 static char const *wkday[7] = {
69  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
70 };
71 
72 static char const *monthname[12] = {
73  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
74  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
75 };
76 
77 /* The starting day of each month, if there's not a leap year.
78  * January 1 is day 0, December 31 is day 355. */
79 static int monthstart[12] = {
80  0, 31, 59, 90, 120, 151,
81  181, 212, 243, 273, 304, 334
82 };
83 
84 /* Value in seconds */
85 #define MINUTE 60
86 #define HOUR (60 * MINUTE)
87 #define DAY (24 * HOUR)
88 
89 Octstr *date_format_http(unsigned long unixtime)
90 {
91  struct tm tm;
92  char buffer[30];
93 
94  tm = gw_gmtime((time_t) unixtime);
95 
96  /* Make sure gmtime gave us a good date. We check this to
97  * protect the sprintf call below, which might overflow its
98  * buffer if the field values are bad. */
99  if (tm.tm_wday < 0 || tm.tm_wday > 6 ||
100  tm.tm_mday < 0 || tm.tm_mday > 31 ||
101  tm.tm_mon < 0 || tm.tm_mon > 11 ||
102  tm.tm_year < 0 ||
103  tm.tm_hour < 0 || tm.tm_hour > 23 ||
104  tm.tm_min < 0 || tm.tm_min > 59 ||
105  tm.tm_sec < 0 || tm.tm_sec > 61) {
106  warning(0, "Bad date for timestamp %lu, cannot format.",
107  unixtime);
108  return NULL;
109  }
110 
111  sprintf(buffer, "%s, %02d %s %04d %02d:%02d:%02d GMT",
112  wkday[tm.tm_wday], tm.tm_mday, monthname[tm.tm_mon],
113  tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec);
114 
115  return octstr_create(buffer);
116 }
117 
119 {
120  long date;
121  int leapyears;
122  long year;
123 
124  date = (t->year - 1970) * (365 * DAY);
125 
126  /* If we haven't had this year's leap day yet, pretend it's
127  * the previous year. */
128  year = t->year;
129  if (t->month <= 1)
130  year--;
131 
132  /* Add leap years since 1970. The magic number 477 is the value
133  * this formula would give for 1970 itself. Notice the extra
134  * effort we make to keep it correct for the year 2100. */
135  leapyears = (year / 4) - (year / 100) + (year / 400) - 477;
136  date += leapyears * DAY;
137 
138  date += monthstart[t->month] * DAY;
139  date += (t->day - 1) * DAY;
140  date += t->hour * HOUR;
141  date += t->minute * MINUTE;
142  date += t->second;
143  date -= t->offset_sec;
144 
145  return date;
146 }
147 
149 {
150  long pos;
151  struct universaltime t;
152  Octstr *monthstr = NULL;
153 
154  /* First, skip the leading day-of-week string. */
155  pos = octstr_search_char(date, ' ', 0);
156  if (pos < 0 || pos == octstr_len(date) - 1)
157  return -1;
158  pos++; /* Skip the space */
159 
160  /* Distinguish between the three acceptable formats */
161  if (isdigit(octstr_get_char(date, pos)) &&
162  octstr_get_char(date, pos + 2) == ' ') {
163  if (octstr_len(date) - pos < (long)strlen("06 Nov 1994 08:49:37 GMT"))
164  goto error;
165  if (octstr_parse_long(&t.day, date, pos, 10) != pos + 2)
166  goto error;
167  monthstr = octstr_copy(date, pos + 3, 3);
168  if (octstr_parse_long(&t.year, date, pos + 7, 10) != pos + 11)
169  goto error;
170  if (octstr_parse_long(&t.hour, date, pos + 12, 10) != pos + 14)
171  goto error;
172  if (octstr_parse_long(&t.minute, date, pos + 15, 10) != pos + 17)
173  goto error;
174  if (octstr_parse_long(&t.second, date, pos + 18, 10) != pos + 20)
175  goto error;
176  /* Take the GMT part on faith. */
177  } else if (isdigit(octstr_get_char(date, pos)) &&
178  octstr_get_char(date, pos + 2) == '-') {
179  if (octstr_len(date) - pos < (long)strlen("06-Nov-94 08:49:37 GMT"))
180  goto error;
181  if (octstr_parse_long(&t.day, date, pos, 10) != pos + 2)
182  goto error;
183  monthstr = octstr_copy(date, pos + 3, 3);
184  if (octstr_parse_long(&t.year, date, pos + 7, 10) != pos + 9)
185  goto error;
186  if (t.year > 60)
187  t.year += 1900;
188  else
189  t.year += 2000;
190  if (octstr_parse_long(&t.hour, date, pos + 10, 10) != pos + 12)
191  goto error;
192  if (octstr_parse_long(&t.minute, date, pos + 13, 10) != pos + 15)
193  goto error;
194  if (octstr_parse_long(&t.second, date, pos + 16, 10) != pos + 18)
195  goto error;
196  /* Take the GMT part on faith. */
197  } else {
198  if (octstr_len(date) - pos < (long)strlen(" 6 08:49:37 1994"))
199  goto error;
200  monthstr = octstr_copy(date, pos, 3);
201  if (octstr_parse_long(&t.day, date, pos + 4, 10) != pos + 6)
202  goto error;
203  if (octstr_parse_long(&t.hour, date, pos + 7, 10) != pos + 9)
204  goto error;
205  if (octstr_parse_long(&t.minute, date, pos + 10, 10) != pos + 12)
206  goto error;
207  if (octstr_parse_long(&t.second, date, pos + 13, 10) != pos + 15)
208  goto error;
209  if (octstr_parse_long(&t.year, date, pos + 16, 10) != pos + 20)
210  goto error;
211  }
212 
213  for (t.month = 0; t.month < 12; t.month++) {
214  if (octstr_str_compare(monthstr, monthname[t.month]) == 0)
215  break;
216  }
217  if (t.month == 12)
218  goto error;
219 
220  octstr_destroy(monthstr);
221  /* GTM time means offset 0 */
222  t.offset_sec = 0;
223  return date_convert_universal(&t);
224 
225 error:
226  octstr_destroy(monthstr);
227  return -1;
228 }
229 
230 int date_parse_iso (struct universaltime *ut, Octstr *os)
231 {
232  int n = 0;
233  char *p, *q;
234 
235  /* assign defaults */
236  ut->month = 0;
237  ut->day = 1;
238  ut->hour = 0;
239  ut->minute = 0;
240  ut->second = 0;
241  ut->offset_sec = 0;
242 
243  p = octstr_get_cstr(os);
244  q = p + ((n = octstr_search_char(os, 'T', 0)) >= 0 ? n : octstr_len(os)); /* stop at the end of string or at the time separator */
245  if (sscanf(p, "%4ld%n", &ut->year, &n) < 1)
246  return -1;
247  p += n;
248 
249  if (ut->year < 70)
250  ut->year += 2000;
251  else if (ut->year < 100)
252  ut->year += 1900;
253 
254  while (p < q && !gw_isdigit(*p))
255  p++;
256  if (sscanf(p, "%2ld%n", &ut->month, &n) < 1)
257  return 0;
258  p += n;
259 
260  /* 0-based months */
261  if (ut->month > 0)
262  ut->month--;
263 
264  while (p < q && !gw_isdigit(*p))
265  p++;
266  if (sscanf(p, "%2ld%n", &ut->day, &n) < 1)
267  return 0;
268  p += n;
269 
270  if (*q == 'T')
271  p = q+1;
272  else
273  return 0;
274 
275  while (*p && !gw_isdigit(*p))
276  p++;
277  if (sscanf(p, "%2ld%n", &ut->hour, &n) < 1)
278  return 0;
279  p += n;
280 
281  while (*p && !gw_isdigit(*p))
282  p++;
283  if (sscanf(p, "%2ld%n", &ut->minute, &n) < 1)
284  return 0;
285  p += n;
286 
287  while (*p && !gw_isdigit(*p))
288  p++;
289  if (sscanf(p, "%2ld%n", &ut->second, &n) < 1)
290  return 0;
291  p += n;
292 
293  if (*p == '.') {
294  long fract;
295  /* fraction */
296  p++;
297  if (sscanf(p, "%ld%n", &fract, &n) < 1)
298  return 0;
299  p += n;
300  }
301 
302  /* check timezone */
303  if (*p) {
304  long hh = 0, mi = 0;
305  char plus = *p;
306  p++;
307 
308  if (plus == 'Z')
309  return 0; /* we are done */
310 
311  if (sscanf(p, "%2ld%n", &hh, &n) < 1)
312  return -1;
313  p += n;
314 
315  while (*p && !gw_isdigit(*p))
316  p++;
317  if (sscanf(p, "%2ld%n", &mi, &n) > 1)
318  p += n;
319 
320  ut->offset_sec = hh * HOUR + mi * MINUTE;
321  if (plus == '-')
322  ut->offset_sec = -ut->offset_sec;
323  }
324 
325  return 0;
326 }
327 
328 Octstr* date_create_iso(time_t unixtime)
329 {
330  struct tm tm;
331 
332  tm = gw_gmtime(unixtime);
333 
334  return octstr_format("%d-%02d-%02dT%02d:%02d:%02dZ",
335  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
336 }
337 
338 
339 /* Note that this implementation makes unportable assumptions about time_t. */
341 {
342  return (long) time(NULL);
343 }
void error(int err, const char *fmt,...)
Definition: log.c:648
long year
Definition: date.h:72
long date_parse_http(Octstr *date)
Definition: date.c:148
#define DAY
Definition: date.c:87
struct tm gw_gmtime(time_t t)
Definition: protected.c:137
#define MINUTE
Definition: date.c:85
Octstr * date_create_iso(time_t unixtime)
Definition: date.c:328
#define HOUR
Definition: date.c:86
static char const * wkday[7]
Definition: date.c:68
long minute
Definition: date.h:74
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1012
long day
Definition: date.h:70
int gw_isdigit(int c)
Definition: utils.c:988
void warning(int err, const char *fmt,...)
Definition: log.c:660
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
long second
Definition: date.h:75
int date_parse_iso(struct universaltime *ut, Octstr *os)
Definition: date.c:230
long date_universal_now(void)
Definition: date.c:340
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
Definition: octstr.c:118
long offset_sec
Definition: date.h:76
long hour
Definition: date.h:73
long month
Definition: date.h:71
int octstr_str_compare(const Octstr *ostr, const char *str)
Definition: octstr.c:973
long octstr_parse_long(long *nump, Octstr *ostr, long pos, int base)
Definition: octstr.c:749
Octstr * date_format_http(unsigned long unixtime)
Definition: date.c:89
static int date(int hex)
static char const * monthname[12]
Definition: date.c:72
long date_convert_universal(struct universaltime *t)
Definition: date.c:118
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:406
static int monthstart[12]
Definition: date.c:79
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.