initial load of upstream version 1.06.32
[xmlrpc-c] / lib / abyss / src / date.c
1 #include <ctype.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <time.h>
5 #include <stdlib.h>
6
7
8 #include <inttypes.h>
9 #include "date.h"
10
11 /*********************************************************************
12 ** Date
13 *********************************************************************/
14
15 static char *_DateDay[7]=
16 {
17     "Sun","Mon","Tue","Wed","Thu","Fri","Sat"
18 };
19
20 static char *_DateMonth[12]=
21 {
22     "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
23 };
24
25 static int32_t _DateTimeBias=0;
26 static char _DateTimeBiasStr[6]="";
27
28 abyss_bool DateToString(TDate *tm,char *s)
29 {
30     if (mktime(tm)==(time_t)(-1))
31     {
32         *s='\0';
33         return FALSE;
34     };
35
36     sprintf(s,"%s, %02d %s %04d %02d:%02d:%02d GMT",_DateDay[tm->tm_wday],tm->tm_mday,
37                 _DateMonth[tm->tm_mon],tm->tm_year+1900,tm->tm_hour,tm->tm_min,tm->tm_sec);
38
39     return TRUE;
40 }
41
42
43
44 abyss_bool
45 DateToLogString(TDate * const tmP,
46                 char *  const s) {
47     time_t t;
48
49     t = mktime(tmP);
50     if (t != (time_t)(-1)) {
51         TDate d;
52         abyss_bool success;
53         success = DateFromLocal(&d, t);
54         if (success) {
55             sprintf(s, "%02d/%s/%04d:%02d:%02d:%02d %s",
56                     d.tm_mday, _DateMonth[d.tm_mon],
57                     d.tm_year+1900, d.tm_hour, d.tm_min, d.tm_sec,
58                     _DateTimeBiasStr);
59             return TRUE;
60         }
61     }
62     *s = '\0';
63     return FALSE;
64 }
65
66
67
68 abyss_bool
69 DateDecode(const char *  const dateString,
70            TDate *       const tmP) {
71
72     int rc;
73     const char * s;
74     uint32_t n;
75
76     s = &dateString[0];
77
78     /* Ignore spaces, day name and spaces */
79     while ((*s==' ') || (*s=='\t'))
80         ++s;
81
82     while ((*s!=' ') && (*s!='\t'))
83         ++s;
84
85     while ((*s==' ') || (*s=='\t'))
86         ++s;
87
88     /* try to recognize the date format */
89     rc = sscanf(s, "%*s %d %d:%d:%d %d%*s", &tmP->tm_mday, &tmP->tm_hour,
90                 &tmP->tm_min, &tmP->tm_sec, &tmP->tm_year);
91     if (rc != 5) {
92         int rc;
93         rc = sscanf(s, "%d %n%*s %d %d:%d:%d GMT%*s",
94                     &tmP->tm_mday,&n,&tmP->tm_year,
95                     &tmP->tm_hour, &tmP->tm_min, &tmP->tm_sec);
96         if (rc != 5) {
97             int rc;
98             rc = sscanf(s, "%d-%n%*[A-Za-z]-%d %d:%d:%d GMT%*s",
99                         &tmP->tm_mday, &n, &tmP->tm_year,
100                         &tmP->tm_hour, &tmP->tm_min, &tmP->tm_sec);
101             if (rc != 5)
102                 return FALSE;
103         }
104     }    
105     /* s points now to the month string */
106     s += n;
107     for (n = 0; n < 12; ++n) {
108         char * p;
109
110         p =_DateMonth[n];
111
112         if (tolower(*p++) == tolower(*s))
113             if (*p++ == tolower(s[1]))
114                 if (*p == tolower(s[2]))
115                     break;
116     }
117
118     if (n == 12)
119         return FALSE;
120
121     tmP->tm_mon = n;
122
123     /* finish the work */
124     if (tmP->tm_year > 1900)
125         tmP->tm_year -= 1900;
126     else {
127         if (tmP->tm_year < 70)
128             tmP->tm_year += 100;
129     }
130     tmP->tm_isdst = 0;
131
132     return (mktime(tmP) != (time_t)(-1));
133 }
134
135
136
137 int32_t DateCompare(TDate *d1,TDate *d2)
138 {
139     int32_t x;
140
141     if ((x=d1->tm_year-d2->tm_year)==0)
142         if ((x=d1->tm_mon-d2->tm_mon)==0)
143             if ((x=d1->tm_mday-d2->tm_mday)==0)
144                 if ((x=d1->tm_hour-d2->tm_hour)==0)
145                     if ((x=d1->tm_min-d2->tm_min)==0)
146                         x=d1->tm_sec-d2->tm_sec;
147
148     return x;
149 }
150
151
152
153 abyss_bool
154 DateFromGMT(TDate *d,time_t t) {
155     TDate *dx;
156
157     dx=gmtime(&t);
158     if (dx) {
159         *d=*dx;
160         return TRUE;
161     };
162
163     return FALSE;
164 }
165
166 abyss_bool DateFromLocal(TDate *d,time_t t)
167 {
168     return DateFromGMT(d,t+_DateTimeBias*2);
169 }
170
171
172
173 abyss_bool
174 DateInit() {
175     time_t t;
176     TDate gmt,local,*d;
177
178     time(&t);
179     if (DateFromGMT(&gmt,t)) {
180         d=localtime(&t);
181         if (d) {
182             local=*d;
183             _DateTimeBias =
184                 (local.tm_sec-gmt.tm_sec)+(local.tm_min-gmt.tm_min)*60
185                 +(local.tm_hour-gmt.tm_hour)*3600;
186             sprintf(_DateTimeBiasStr, "%+03d%02d",
187                     _DateTimeBias/3600,(abs(_DateTimeBias) % 3600)/60);
188             return TRUE;
189         };
190     }
191     return FALSE;
192 }