comparison mcabber/src/utils.c @ 802:dd860da62002

Enable use on systems that do not have tm_gmtoff including Cygwin
author Soren Andersen <somian@pobox.com>
date Mon, 10 Apr 2006 23:25:32 +0200
parents 2f027806cd48
children 8f8d8f8157a2
comparison
equal deleted inserted replaced
801:d8e0a1ce3e8a 802:dd860da62002
23 23
24 #include <stdio.h> 24 #include <stdio.h>
25 #include <stdlib.h> 25 #include <stdlib.h>
26 #include <string.h> 26 #include <string.h>
27 #include <stdarg.h> 27 #include <stdarg.h>
28
29 /* For Cygwin (thanks go to Yitzchak Scott-Thoennes) */
30 #ifdef __CYGWIN__
31 # define timezonevar
32 extern long timezone;
33 #endif
28 #include <time.h> 34 #include <time.h>
29 #include <unistd.h> 35 #include <unistd.h>
30 #include <sys/types.h> 36 #include <sys/types.h>
31 #include <sys/stat.h> 37 #include <sys/stat.h>
32 #include <ctype.h> 38 #include <ctype.h>
185 191
186 return ((ret == -1) ? -1 : 0); 192 return ((ret == -1) ? -1 : 0);
187 } 193 }
188 194
189 // from_iso8601(timestamp, utc) 195 // from_iso8601(timestamp, utc)
190 // This function comes from the Gaim project, gaim_str_to_time(). 196 // This function came from the Gaim project, gaim_str_to_time().
191 // (Actually date may not be pure iso-8601) 197 // (Actually date may not be pure iso-8601)
192 // Thanks, guys! 198 // Thanks, guys!
199 // ** Modified by somian 10 Apr 2006 with advice from ysth.
193 time_t from_iso8601(const char *timestamp, int utc) 200 time_t from_iso8601(const char *timestamp, int utc)
194 { 201 {
195 struct tm t; 202 struct tm t;
196 time_t retval = 0; 203 time_t retval = 0;
197 char buf[32]; 204 char buf[32];
198 char *c; 205 char *c;
199 int tzoff = 0; 206 int tzoff = 0;
207 int hms_succ = 0;
200 208
201 time(&retval); 209 time(&retval);
202 localtime_r(&retval, &t); 210 localtime_r(&retval, &t);
203 211
204 /* Reset time to midnight (00:00:00) */ 212 /* Reset time to midnight (00:00:00) */
228 c+=2; 236 c+=2;
229 if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */ 237 if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */
230 c++; /* skip the "T" */ 238 c++; /* skip the "T" */
231 239
232 /* 2 digit hour */ 240 /* 2 digit hour */
233 if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 || 241 if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3)
234 sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) { 242 {
243 hms_succ = 1;
244 c += 8;
245 }
246 else if (sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3)
247 {
248 hms_succ = 1;
249 c += 6;
250 }
251
252 if (hms_succ) {
235 int tzhrs, tzmins; 253 int tzhrs, tzmins;
236 c+=8; 254
237 if (*c == '.') /* dealing with precision we don't care about */ 255 if (*c == '.') /* dealing with precision we don't care about */
238 c += 4; 256 c += 4;
239 257
240 if ((*c == '+' || *c == '-') && 258 if ((*c == '+' || *c == '-') &&
241 sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) { 259 sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) {
243 if (*c == '+') 261 if (*c == '+')
244 tzoff *= -1; 262 tzoff *= -1;
245 } 263 }
246 264
247 if (tzoff || utc) { 265 if (tzoff || utc) {
248 266 #ifdef HAVE_TM_GMTOFF
249 //#ifdef HAVE_TM_GMTOFF
250 tzoff += t.tm_gmtoff; 267 tzoff += t.tm_gmtoff;
251 //#else 268 #else
252 //# ifdef HAVE_TIMEZONE 269 # ifdef HAVE_TIMEZONE
253 // tzset(); /* making sure */ 270 tzset(); /* making sure */
254 // tzoff -= timezone; 271 tzoff -= timezone;
255 //# endif 272 # endif
256 //#endif 273 #endif
257 } 274 }
258 } 275 }
259 } 276 }
260 277
261 t.tm_isdst = -1; 278 t.tm_isdst = -1;