Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
date.h File Reference
#include "gwlib.h"

Go to the source code of this file.

Data Structures

struct  universaltime
 

Functions

long date_convert_universal (struct universaltime *t)
 
Octstrdate_format_http (unsigned long unixtime)
 
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)
 

Function Documentation

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::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 
144  return date;
145 }
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 hour
Definition: date.h:73
long month
Definition: date.h:71
static int date(int hex)
static int monthstart[12]
Definition: date.c:79
Octstr* date_create_iso ( time_t  unixtime)

Definition at line 292 of file date.c.

References gw_gmtime(), and octstr_format().

Referenced by soap_mobitai_date_attribute(), and soap_mobitai_validity_date_attribute().

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 }
struct tm gw_gmtime(time_t t)
Definition: protected.c:137
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
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:624
#define octstr_create(cstr)
Definition: octstr.h:125
static char const * monthname[12]
Definition: date.c:72
long date_parse_http ( Octstr date)

Definition at line 147 of file date.c.

References 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::second, and universaltime::year.

Referenced by check_reversible(), and wsp_pack_date().

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 }
void error(int err, const char *fmt,...)
Definition: log.c:612
#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
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
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
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
int date_parse_iso ( struct universaltime ut,
Octstr os 
)

Definition at line 227 of file date.c.

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

Referenced by main(), and soap_parse_mo().

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 }
long year
Definition: date.h:72
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:1010
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:340
long hour
Definition: date.h:73
long month
Definition: date.h:71
long date_universal_now ( void  )

Definition at line 304 of file date.c.

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

305 {
306  return (long) time(NULL);
307 }
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.