From 745293aa20f6ff270b7f7fc1a231204707b47def Mon Sep 17 00:00:00 2001 From: helge Date: Sat, 9 Feb 2008 16:16:39 +0000 Subject: [PATCH] some date parsing fix git-svn-id: http://svn.opengroupware.org/SOPE/trunk@1601 e4a50df8-12e2-0310-a44c-efbce7f8a7e3 --- sope-gdl1/PostgreSQL/ChangeLog | 4 ++ sope-gdl1/PostgreSQL/NSCalendarDate+PGVal.m | 64 +++++++++++++-------- sope-gdl1/PostgreSQL/PostgreSQL72Channel.m | 2 +- sope-gdl1/PostgreSQL/Version | 2 +- 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/sope-gdl1/PostgreSQL/ChangeLog b/sope-gdl1/PostgreSQL/ChangeLog index 6ff193d6..56884f26 100644 --- a/sope-gdl1/PostgreSQL/ChangeLog +++ b/sope-gdl1/PostgreSQL/ChangeLog @@ -1,5 +1,9 @@ 2008-02-09 Helge Hess + * NSCalendarDate+PGVal.m: rewrote date parsing to use strsep(), + now works with date strings containing milliseconds (which we ignore) + (v4.7.53) + * v4.7.52 * NSString+PostgreSQL72.m: properly use -length, not -cStringLength diff --git a/sope-gdl1/PostgreSQL/NSCalendarDate+PGVal.m b/sope-gdl1/PostgreSQL/NSCalendarDate+PGVal.m index 9498bbb4..cafc969c 100644 --- a/sope-gdl1/PostgreSQL/NSCalendarDate+PGVal.m +++ b/sope-gdl1/PostgreSQL/NSCalendarDate+PGVal.m @@ -2,7 +2,7 @@ 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) @@ -36,6 +36,7 @@ static NSString *PGSQL_DATETIME_FORMAT = @"%b %d %Y %I:%M:%S:000%p"; /* 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". @@ -52,48 +53,63 @@ static NSTimeZone *gmt02 = nil; 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) { diff --git a/sope-gdl1/PostgreSQL/PostgreSQL72Channel.m b/sope-gdl1/PostgreSQL/PostgreSQL72Channel.m index 340bf51d..52c20c01 100644 --- a/sope-gdl1/PostgreSQL/PostgreSQL72Channel.m +++ b/sope-gdl1/PostgreSQL/PostgreSQL72Channel.m @@ -2,7 +2,7 @@ PostgreSQL72Channel.m Copyright (C) 1999 MDlink online service center GmbH and Helge Hess - Copyright (C) 2000-2004 SKYRIX Software AG and Helge Hess + Copyright (C) 2000-2008 SKYRIX Software AG and Helge Hess Author: Helge Hess (helge.hess@opengroupware.org) diff --git a/sope-gdl1/PostgreSQL/Version b/sope-gdl1/PostgreSQL/Version index 3638a163..5b0a6ce4 100644 --- a/sope-gdl1/PostgreSQL/Version +++ b/sope-gdl1/PostgreSQL/Version @@ -1,5 +1,5 @@ # version file -SUBMINOR_VERSION:=52 +SUBMINOR_VERSION:=53 # v4.5.41 requires libGDLAccess v4.5.50 -- 2.39.5