jabberd2  2.3.6
datetime.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002-2003 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19  */
20 
21 #include "util.h"
22 
23 /* ISO 8601 / JEP-0082 date/time manipulation */
24 
25 /* formats */
26 #define DT_DATETIME_P "%04d-%02d-%02dT%02d:%02d:%lf+%02d:%02d"
27 #define DT_DATETIME_M "%04d-%02d-%02dT%02d:%02d:%lf-%02d:%02d"
28 #define DT_DATETIME_Z "%04d-%02d-%02dT%02d:%02d:%lfZ"
29 #define DT_TIME_P "%02d:%02d:%lf+%02d:%02d"
30 #define DT_TIME_M "%02d:%02d:%lf-%02d:%02d"
31 #define DT_TIME_Z "%02d:%02d:%lfZ"
32 #define DT_LEGACY "%04d%02d%02dT%02d:%02d:%lf"
33 
34 time_t datetime_in(char *date) {
35  struct tm gmt, off;
36  double sec;
37  off_t fix = 0;
38  struct timeval tv;
39  struct timezone tz;
40 
41  assert((int) (date != NULL));
42 
43  /* !!! sucks having to call this each time */
44  tzset();
45 
46  memset(&gmt, 0, sizeof(struct tm));
47  memset(&off, 0, sizeof(struct tm));
48 
49  if(sscanf(date, DT_DATETIME_P,
50  &gmt.tm_year, &gmt.tm_mon, &gmt.tm_mday,
51  &gmt.tm_hour, &gmt.tm_min, &sec,
52  &off.tm_hour, &off.tm_min) == 8) {
53  gmt.tm_sec = (int) sec;
54  gmt.tm_year -= 1900;
55  gmt.tm_mon--;
56  fix = off.tm_hour * 3600 + off.tm_min * 60;
57  }
58 
59  else if(sscanf(date, DT_DATETIME_M,
60  &gmt.tm_year, &gmt.tm_mon, &gmt.tm_mday,
61  &gmt.tm_hour, &gmt.tm_min, &sec,
62  &off.tm_hour, &off.tm_min) == 8) {
63  gmt.tm_sec = (int) sec;
64  gmt.tm_year -= 1900;
65  gmt.tm_mon--;
66  fix = - off.tm_hour * 3600 - off.tm_min * 60;
67  }
68 
69  else if(sscanf(date, DT_DATETIME_Z,
70  &gmt.tm_year, &gmt.tm_mon, &gmt.tm_mday,
71  &gmt.tm_hour, &gmt.tm_min, &sec) == 6) {
72  gmt.tm_sec = (int) sec;
73  gmt.tm_year -= 1900;
74  gmt.tm_mon--;
75  fix = 0;
76  }
77 
78  else if(sscanf(date, DT_TIME_P,
79  &gmt.tm_hour, &gmt.tm_min, &sec,
80  &off.tm_hour, &off.tm_min) == 5) {
81  gmt.tm_sec = (int) sec;
82  fix = off.tm_hour * 3600 + off.tm_min * 60;
83  }
84 
85  else if(sscanf(date, DT_TIME_M,
86  &gmt.tm_hour, &gmt.tm_min, &sec,
87  &off.tm_hour, &off.tm_min) == 5) {
88  gmt.tm_sec = (int) sec;
89  fix = - off.tm_hour * 3600 - off.tm_min * 60;
90  }
91 
92  else if(sscanf(date, DT_TIME_Z,
93  &gmt.tm_hour, &gmt.tm_min, &sec) == 3) {
94  gmt.tm_sec = (int) sec;
95  fix = - off.tm_hour * 3600 - off.tm_min * 60;
96  }
97 
98  else if(sscanf(date, DT_LEGACY,
99  &gmt.tm_year, &gmt.tm_mon, &gmt.tm_mday,
100  &gmt.tm_hour, &gmt.tm_min, &sec) == 6) {
101  gmt.tm_sec = (int) sec;
102  gmt.tm_year -= 1900;
103  gmt.tm_mon--;
104  fix = 0;
105  }
106 
107  gmt.tm_isdst = -1;
108 
109  gettimeofday(&tv, &tz);
110 
111  return mktime(&gmt) + fix - (tz.tz_minuteswest * 60);
112 }
113 
114 void datetime_out(time_t t, datetime_t type, char *date, int datelen) {
115  struct tm *gmt;
116 
117  assert((int) type);
118  assert((int) (date != NULL));
119  assert((int) datelen);
120 
121  gmt = gmtime(&t);
122 
123  switch(type) {
124  case dt_DATE:
125  snprintf(date, datelen, "%04d-%02d-%02d", gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday);
126  break;
127 
128  case dt_TIME:
129  snprintf(date, datelen, "%02d:%02d:%02dZ", gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
130  break;
131 
132  case dt_DATETIME:
133  snprintf(date, datelen, "%04d-%02d-%02dT%02d:%02d:%02dZ", gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
134  break;
135 
136  case dt_LEGACY:
137  snprintf(date, datelen, "%04d%02d%02dT%02d:%02d:%02d", gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
138  break;
139  }
140 }
datetime_t
Definition: datetime.h:50
#define DT_DATETIME_M
Definition: datetime.c:27
#define DT_LEGACY
Definition: datetime.c:32
#define DT_DATETIME_Z
Definition: datetime.c:28
#define DT_TIME_Z
Definition: datetime.c:31
#define DT_TIME_M
Definition: datetime.c:30
void datetime_out(time_t t, datetime_t type, char *date, int datelen)
Definition: datetime.c:114
#define DT_TIME_P
Definition: datetime.c:29
#define DT_DATETIME_P
Definition: datetime.c:26
time_t datetime_in(char *date)
Definition: datetime.c:34