Kannel: Open Source WAP and SMS gateway  svn-r5285
date.c File Reference
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include "gwlib.h"

Go to the source code of this file.

Macros

#define MINUTE   60
 
#define HOUR   (60 * MINUTE)
 
#define DAY   (24 * HOUR)
 

Functions

Octstrdate_format_http (unsigned long unixtime)
 
long date_convert_universal (struct universaltime *t)
 
long date_parse_http (Octstr *date)
 
int date_parse_iso (struct universaltime *ut, Octstr *os)
 
Octstrdate_create_iso (time_t unixtime)
 
long date_universal_now (void)
 

Variables

static char const * wkday [7]
 
static char const * monthname [12]
 
static int monthstart [12]
 

Macro Definition Documentation

◆ DAY

#define DAY   (24 * HOUR)

Definition at line 87 of file date.c.

Referenced by date_convert_universal().

◆ HOUR

#define HOUR   (60 * MINUTE)

Definition at line 86 of file date.c.

Referenced by date_convert_universal(), and date_parse_iso().

◆ MINUTE

#define MINUTE   60

Definition at line 85 of file date.c.

Referenced by date_convert_universal(), and date_parse_iso().

Function Documentation

◆ date_convert_universal()

long date_convert_universal ( struct universaltime t)

Definition at line 118 of file date.c.

References date(), universaltime::day, DAY, universaltime::hour, HOUR, universaltime::minute, MINUTE, universaltime::month, monthstart, universaltime::offset_sec, universaltime::second, and universaltime::year.

Referenced by at2_pdu_decode_deliver_sm(), date_parse_http(), handle_operation(), main(), soap_parse_mo(), and soap_read_date().

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 }
long year
Definition: date.h:72
#define DAY
Definition: date.c:87
#define MINUTE
Definition: date.c:85
#define HOUR
Definition: date.c:86
long minute
Definition: date.h:74
long day
Definition: date.h:70
long second
Definition: date.h:75
long offset_sec
Definition: date.h:76
long hour
Definition: date.h:73
long month
Definition: date.h:71
static int date(int hex)
static int monthstart[12]
Definition: date.c:79

◆ date_create_iso()

Octstr* date_create_iso ( time_t  unixtime)

Definition at line 328 of file date.c.

References gw_gmtime(), and octstr_format().

Referenced by soap_mobitai_date_attribute(), and soap_mobitai_validity_date_attribute().

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 }
struct tm gw_gmtime(time_t t)
Definition: protected.c:137
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464

◆ date_format_http()

Octstr* date_format_http ( unsigned long  unixtime)

Definition at line 89 of file date.c.

References gw_gmtime(), monthname, octstr_create, warning(), and wkday.

Referenced by add_x_wap_tod(), check_reversible(), http_send_reply(), soap_server(), and wsp_unpack_date_value().

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 }
struct tm gw_gmtime(time_t t)
Definition: protected.c:137
static char const * wkday[7]
Definition: date.c:68
void warning(int err, const char *fmt,...)
Definition: log.c:660
#define octstr_create(cstr)
Definition: octstr.h:125
static char const * monthname[12]
Definition: date.c:72

◆ date_parse_http()

long date_parse_http ( Octstr date)

Definition at line 148 of file date.c.

References date(), date_convert_universal(), universaltime::day, error(), universaltime::hour, universaltime::minute, universaltime::month, monthname, octstr_copy, octstr_destroy(), octstr_get_char(), octstr_len(), octstr_parse_long(), octstr_search_char(), octstr_str_compare(), universaltime::offset_sec, universaltime::second, and universaltime::year.

Referenced by check_reversible(), and wsp_pack_date().

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 }
void error(int err, const char *fmt,...)
Definition: log.c:648
#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
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
Definition: octstr.c:118
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
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

◆ date_parse_iso()

int date_parse_iso ( struct universaltime ut,
Octstr os 
)

Definition at line 230 of file date.c.

References universaltime::day, gw_isdigit(), universaltime::hour, HOUR, universaltime::minute, MINUTE, universaltime::month, octstr_get_cstr, octstr_len(), octstr_search_char(), universaltime::offset_sec, universaltime::second, and universaltime::year.

Referenced by main(), and soap_parse_mo().

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 }
long year
Definition: date.h:72
#define MINUTE
Definition: date.c:85
#define HOUR
Definition: date.c:86
long minute
Definition: date.h:74
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
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
long second
Definition: date.h:75
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
long offset_sec
Definition: date.h:76
long hour
Definition: date.h:73
long month
Definition: date.h:71

◆ date_universal_now()

long date_universal_now ( void  )

Definition at line 340 of file date.c.

Referenced by eq_create_event(), eq_round_trip_time(), send_enquire_link(), smasi_thread(), and smpp_emu_writer().

341 {
342  return (long) time(NULL);
343 }

Variable Documentation

◆ monthname

char const* monthname[12]
static
Initial value:
= {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
}

Definition at line 72 of file date.c.

Referenced by date_format_http(), and date_parse_http().

◆ monthstart

int monthstart[12]
static
Initial value:
= {
0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334
}

Definition at line 79 of file date.c.

Referenced by date_convert_universal().

◆ wkday

char const* wkday[7]
static
Initial value:
= {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
}

Definition at line 68 of file date.c.

Referenced by date_format_http().

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.