NSCalendarDate+PGVal.m
Copyright (C) 1999 MDlink online service center GmbH and Helge Hess
- Copyright (C) 2000-2005 SKYRIX Software AG and Helge Hess
+ Copyright (C) 2000-2008 SKYRIX Software AG and Helge Hess
Author: Helge Hess (helge@opengroupware.org)
/*
Format: '2001-07-26 14:00:00+02' (len 22)
'2001-07-26 14:00:00+09:30' (len 25)
+ '2008-01-31 14:00:57.249+01' (len 26)
0123456789012345678901234
Matthew: "07/25/2003 06:00:00 CDT".
attribute:(EOAttribute *)_attribute
adaptorChannel:(PostgreSQL72Channel *)_channel
{
- static char buf[28]; // reused buffer THREAD
+ char buf[28];
char *p;
NSTimeZone *attrTZ;
NSCalendarDate *date;
int year, month, day, hour, min, sec, tzOffset = 0;
+ char *tok;
if (_length == 0)
return nil;
- if (_length != 22 && _length != 25) {
+ if (_length != 22 && _length != 25 && _length != 26) {
// TODO: add support for "2001-07-26 14:00:00" (len=19)
+ // TBD: add support for "2008-01-31 14:00:57.249+01" (len=26)
NSLog(@"ERROR(%s): unexpected string '%s' for date type '%@', returning "
@"now (expected format: '2001-07-26 14:00:00+02')",
__PRETTY_FUNCTION__,
_cstr, _type);
return [NSCalendarDate date];
}
- strncpy(buf, _cstr, 25);
- buf[25] = '\0';
+ strncpy(buf, _cstr, 26);
+ buf[26] = '\0';
- /* perform on reverse, so that we don't overwrite with null-terminators */
+ tok = buf;
+ year = atoi(strsep(&tok, "-"));
+ month = atoi(strsep(&tok, "-"));
+ day = atoi(strsep(&tok, " "));
+ hour = atoi(strsep(&tok, ":"));
+ min = atoi(strsep(&tok, ":"));
- if (_length == 22) {
- p = &(buf[19]);
- tzOffset = atoi(p) * 60;
- }
- else if (_length >= 25) {
- int mins;
- p = &(buf[23]);
- mins = atoi(p);
- buf[22] = '\0'; // the ':'
- p = &(buf[19]);
- tzOffset = atoi(p) * 60;
- tzOffset = tzOffset > 0 ? (tzOffset + mins) : (tzOffset - mins);
+ tzOffset = 0;
+ if (tok != NULL && (p = strchr(tok, '+')) != NULL)
+ tzOffset = +1;
+ else if (tok != NULL && (p = strchr(tok, '-')) != NULL)
+ tzOffset = -1;
+ else
+ tzOffset = 0; // TBD: warn?
+ if (tzOffset != 0) {
+ int tzHours, tzMins = 0;
+
+ p++; // skip +/-
+ tzHours = atoi(strsep(&p, ":"));
+ if (p != NULL) tzMins = atoi(p);
+
+ tzMins = tzHours * 60 + tzMins;
+ tzOffset = tzOffset < 0 ? -tzMins : tzMins;
}
- p = &(buf[17]); buf[19] = '\0'; sec = atoi(p);
- p = &(buf[14]); buf[16] = '\0'; min = atoi(p);
- p = &(buf[11]); buf[13] = '\0'; hour = atoi(p);
- p = &(buf[8]); buf[10] = '\0'; day = atoi(p);
- p = &(buf[5]); buf[7] = '\0'; month = atoi(p);
- p = &(buf[0]); buf[4] = '\0'; year = atoi(p);
+ /* extract seconds */
+ sec = atoi(strsep(&tok, ":+."));
+
+#if HEAVY_DEBUG
+ NSLog(@"DATE: %s => %04i-%02i-%02i %02i:%02i:%02i",
+ buf, year, month, day, hour, min, sec);
+#endif
+#if HEAVY_DEBUG
+ NSLog(@"DATE: %s => OFFSET %i", _cstr, tzOffset);
+#endif
/* TODO: cache all timezones (just 26 ;-) */
switch (tzOffset) {