Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
date.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 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 
144  return date;
145 }
146 
148 {
149  long pos;
150  struct universaltime t;
151  Octstr *monthstr = NULL;
152 
153  /* First, skip the leading day-of-week string. */
154  pos = octstr_search_char(date, ' ', 0);
155  if (pos < 0 || pos == octstr_len(date) - 1)
156  return -1;
157  pos++; /* Skip the space */
158 
159  /* Distinguish between the three acceptable formats */
160  if (isdigit(octstr_get_char(date, pos)) &&
161  octstr_get_char(date, pos + 2) == ' ') {
162  if (octstr_len(date) - pos < (long)strlen("06 Nov 1994 08:49:37 GMT"))
163  goto error;
164  if (octstr_parse_long(&t.day, date, pos, 10) != pos + 2)
165  goto error;
166  monthstr = octstr_copy(date, pos + 3, 3);
167  if (octstr_parse_long(&t.year, date, pos + 7, 10) != pos + 11)
168  goto error;
169  if (octstr_parse_long(&t.hour, date, pos + 12, 10) != pos + 14)
170  goto error;
171  if (octstr_parse_long(&t.minute, date, pos + 15, 10) != pos + 17)
172  goto error;
173  if (octstr_parse_long(&t.second, date, pos + 18, 10) != pos + 20)
174  goto error;
175  /* Take the GMT part on faith. */
176  } else if (isdigit(octstr_get_char(date, pos)) &&
177  octstr_get_char(date, pos + 2) == '-') {
178  if (octstr_len(date) - pos < (long)strlen("06-Nov-94 08:49:37 GMT"))
179  goto error;
180  if (octstr_parse_long(&t.day, date, pos, 10) != pos + 2)
181  goto error;
182  monthstr = octstr_copy(date, pos + 3, 3);
183  if (octstr_parse_long(&t.year, date, pos + 7, 10) != pos + 9)
184  goto error;
185  if (t.year > 60)
186  t.year += 1900;
187  else
188  t.year += 2000;
189  if (octstr_parse_long(&t.hour, date, pos + 10, 10) != pos + 12)
190  goto error;
191  if (octstr_parse_long(&t.minute, date, pos + 13, 10) != pos + 15)
192  goto error;
193  if (octstr_parse_long(&t.second, date, pos + 16, 10) != pos + 18)
194  goto error;
195  /* Take the GMT part on faith. */
196  } else {
197  if (octstr_len(date) - pos < (long)strlen(" 6 08:49:37 1994"))
198  goto error;
199  monthstr = octstr_copy(date, pos, 3);
200  if (octstr_parse_long(&t.day, date, pos + 4, 10) != pos + 6)
201  goto error;
202  if (octstr_parse_long(&t.hour, date, pos + 7, 10) != pos + 9)
203  goto error;
204  if (octstr_parse_long(&t.minute, date, pos + 10, 10) != pos + 12)
205  goto error;
206  if (octstr_parse_long(&t.second, date, pos + 13, 10) != pos + 15)
207  goto error;
208  if (octstr_parse_long(&t.year, date, pos + 16, 10) != pos + 20)
209  goto error;
210  }
211 
212  for (t.month = 0; t.month < 12; t.month++) {
213  if (octstr_str_compare(monthstr, monthname[t.month]) == 0)
214  break;
215  }
216  if (t.month == 12)
217  goto error;
218 
219  octstr_destroy(monthstr);
220  return date_convert_universal(&t);
221 
222 error:
223  octstr_destroy(monthstr);
224  return -1;
225 }
226 
227 int date_parse_iso (struct universaltime *ut, Octstr *os)
228 {
229  int n = 0;
230  char *p, *q;
231 
232  /* assign defaults */
233  ut->month = 0;
234  ut->day = 1;
235  ut->hour = 0;
236  ut->minute = 0;
237  ut->second = 0;
238 
239  p = octstr_get_cstr(os);
240  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 */
241  if (sscanf(p, "%4ld%n", &ut->year, &n) < 1)
242  return -1;
243  p += n;
244 
245  if (ut->year < 70)
246  ut->year += 2000;
247  else if (ut->year < 100)
248  ut->year += 1900;
249 
250  while (p < q && !gw_isdigit(*p))
251  p++;
252  if (sscanf(p, "%2ld%n", &ut->month, &n) < 1)
253  return 0;
254  p += n;
255 
256  /* 0-based months */
257  if (ut->month > 0)
258  ut->month--;
259 
260  while (p < q && !gw_isdigit(*p))
261  p++;
262  if (sscanf(p, "%2ld%n", &ut->day, &n) < 1)
263  return 0;
264  p += n;
265 
266  if (*q == 'T')
267  p = q+1;
268  else
269  return 0;
270 
271  while (*p && !gw_isdigit(*p))
272  p++;
273  if (sscanf(p, "%2ld%n", &ut->hour, &n) < 1)
274  return 0;
275  p += n;
276 
277  while (*p && !gw_isdigit(*p))
278  p++;
279  if (sscanf(p, "%2ld%n", &ut->minute, &n) < 1)
280  return 0;
281  p += n;
282 
283  while (*p && !gw_isdigit(*p))
284  p++;
285  if (sscanf(p, "%2ld%n", &ut->second, &n) < 1)
286  return 0;
287  p += n;
288 
289  return 0;
290 }
291 
292 Octstr* date_create_iso(time_t unixtime)
293 {
294  struct tm tm;
295 
296  tm = gw_gmtime(unixtime);
297 
298  return octstr_format("%d-%02d-%02dT%02d:%02d:%02dZ",
299  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
300 }
301 
302 
303 /* Note that this implementation makes unportable assumptions about time_t. */
305 {
306  return (long) time(NULL);
307 }
void error(int err, const char *fmt,...)
Definition: log.c:612
long year
Definition: date.h:72
long date_parse_http(Octstr *date)
Definition: date.c:147
#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:292
#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:1010
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:624
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#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:227
long date_universal_now(void)
Definition: date.c:304
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
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:971
long octstr_parse_long(long *nump, Octstr *ostr, long pos, int base)
Definition: octstr.c:747
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:404
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.