Author: rfm
Date: Fri May  5 14:43:54 2017
New Revision: 40497

URL: http://svn.gna.org/viewcvs/gnustep?rev=40497&view=rev
Log:
timezone and date/time encoding options added

Modified:
    libs/webservices/trunk/GWSCoder.h
    libs/webservices/trunk/GWSJSONCoder.m

Modified: libs/webservices/trunk/GWSCoder.h
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/webservices/trunk/GWSCoder.h?rev=40497&r1=40496&r2=40497&view=diff
==============================================================================
--- libs/webservices/trunk/GWSCoder.h   (original)
+++ libs/webservices/trunk/GWSCoder.h   Fri May  5 14:43:54 2017
@@ -586,15 +586,20 @@
                     order: (NSArray*)order;
 
 /** A helper method which decodes a timestamp from ISO8601 format,
- * YYYY-MM-DDTHH:MM:SS.mmmZ
+ * YYYY-MM-DDTHH:MM:SS.mmmZ<br />
+ * This tolerates omission of the trailing Z (in which case it assumes
+ * the local timezone rather than GMT), the millisecond part,
+ * and (in the case that both milliseconds and 'Z' are omitted) also
+ * tolerates omission of the hyphens and colons (YYYYMMDDTHHMMSS).
  */
 - (NSCalendarDate*) decodeDateTimeFrom: (NSString*)source;
 
 /** Take the supplied date and encode it as a string.<br />
- * This uses the timezone currently set in the receiver to determine
- * the time of day encoded.<br />
  * There is no standard for JSON timestamps, but the recommended value
- * (supported by javascript) is ISO8601 ... YYYY-MM-DDTHH:MM:SS.mmmZ
+ * (supported by javascript) is ISO8601 ... YYYY-MM-DDTHH:MM:SS.mmmZ<br />
+ * If the -setTimeZone: method is used, the format YYYYMMDDTHHMMSS will
+ * be used relative to the time zone value set (unless a nil argument
+ * was supplied).
  */
 - (NSString*) encodeDateTimeFrom: (NSDate*)source;
 
@@ -612,7 +617,10 @@
  */
 - (void) setRPCID: (id)o;
 
-/** Does nothing ... JSON always uses GMT as the time zone.
+/** Sets a timeZone so that encoding of date/time values will be done
+ * relative to that timeZone as YYYMMDDTHHMMSS format rather than the
+ * standard YYYY-MM-DDTHH:MM:SS.mmZ format using GMT.<br />
+ * Setting a nil timeZone reverts to standard behavior.
  */
 - (void) setTimeZone: (NSTimeZone*)timeZone;
 
@@ -625,10 +633,6 @@
  * to the method call overrides any value set in the coder).
  */
 - (void) setVersion: (NSString*)v;
-
-/** Return the time zone for encoding/decoding; always GMT for JSON.
- */
-- (NSTimeZone*) timeZone;
 
 /** Returns the json-rpc version (currently "2.0" or "1.0" or nil).<br />
  * See -setVersion: for details.

Modified: libs/webservices/trunk/GWSJSONCoder.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/webservices/trunk/GWSJSONCoder.m?rev=40497&r1=40496&r2=40497&view=diff
==============================================================================
--- libs/webservices/trunk/GWSJSONCoder.m       (original)
+++ libs/webservices/trunk/GWSJSONCoder.m       Fri May  5 14:43:54 2017
@@ -41,7 +41,8 @@
 static Class    NSNullClass;
 static Class    NSNumberClass;
 static Class    NSStringClass;
-static NSTimeZone       *tz;
+static NSTimeZone       *gmt;
+static BOOL     useTimeZone = NO;
 
 static NSString*
 JSONQuote(NSString *str)
@@ -580,7 +581,7 @@
   boolY = [[NSNumberClass numberWithBool: YES] retain];
   boolN = [[NSNumberClass numberWithBool: NO] retain];
   null = [[NSNullClass null] retain];
-  tz = [[NSTimeZone timeZoneWithName: @"GMT"] retain];
+  gmt = [[NSTimeZone timeZoneWithName: @"GMT"] retain];
 }
 
 - (void) appendObject: (id)o
@@ -960,13 +961,42 @@
   int                  minute;
   int                  second;
   int                   millisecond;
-  NSTimeInterval       ti;
+  int                   l;
+  NSTimeZone            *tz = nil;
   const char            *u;
   NSCalendarDate        *d;
 
   u = [source UTF8String];
-  if (sscanf(u, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
-    &year, &month, &day, &hour, &minute, &second, &millisecond) != 7)
+  l = strlen(u);
+  if (24 == l && sscanf(u, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
+    &year, &month, &day, &hour, &minute, &second, &millisecond) == 7)
+    {
+      tz = gmt;
+    }
+  else if (23 == l && sscanf(u, "%04d-%02d-%02dT%02d:%02d:%02d.%03d",
+    &year, &month, &day, &hour, &minute, &second, &millisecond) == 7)
+    {
+      tz = [self timeZone];
+    }
+  else if (20 == l && sscanf(u, "%04d-%02d-%02dT%02d:%02d:%02dZ",
+    &year, &month, &day, &hour, &minute, &second) == 6)
+    {
+      millisecond = 0;
+      tz = gmt;
+    }
+  else if (19 == l && sscanf(u, "%04d-%02d-%02dT%02d:%02d:%02d",
+    &year, &month, &day, &hour, &minute, &second) == 6)
+    {
+      millisecond = 0;
+      tz = [self timeZone];
+    }
+  else if (15 == l && sscanf(u, "%04d%02d%02dT%02d%02d%02d",
+    &year, &month, &day, &hour, &minute, &second) == 6)
+    {
+      millisecond = 0;
+      tz = [self timeZone];
+    }
+  else
     {
       [NSException raise: NSInvalidArgumentException
                   format: @"bad date/time format '%@'", source];
@@ -979,22 +1009,35 @@
                minute: minute
                second: second
              timeZone: tz];
-
-  ti = millisecond;
-  ti /= 1000.0;
-  ti += [d timeIntervalSinceReferenceDate];
-  d = [d initWithTimeIntervalSinceReferenceDate: ti];
+  if (millisecond != 0)
+    {
+      NSTimeInterval   ti;
+
+      ti = millisecond;
+      ti /= 1000.0;
+      ti += [d timeIntervalSinceReferenceDate];
+      d = [d initWithTimeIntervalSinceReferenceDate: ti];
+    }
   [d setTimeZone: tz];
   return [d autorelease];
 }
 
 - (NSString*) encodeDateTimeFrom: (NSDate*)source
 {
-  NSString     *s;
-
-  s = [source descriptionWithCalendarFormat: @"%Y-%m-%dT%H:%M:%S.%FZ"
-                                   timeZone: tz
-                                     locale: nil];
+  NSString      *s;
+
+  if (YES == useTimeZone)
+    {
+      s = [source descriptionWithCalendarFormat: @"%Y%m%dT%H:%M:%S"
+                                       timeZone: [self timeZone]
+                                         locale: nil];
+    }
+  else
+    {
+      s = [source descriptionWithCalendarFormat: @"%Y-%m-%dT%H:%M:%S.%FZ"
+                                       timeZone: gmt
+                                         locale: nil];
+    }
   return s;
 }
 
@@ -1189,7 +1232,16 @@
 
 - (void) setTimeZone: (NSTimeZone*)timeZone
 {
-  return;
+  if (nil == timeZone)
+    {
+      timeZone = gmt;
+      useTimeZone = NO;         // Use standard date/time format
+    }
+  else
+    {
+      useTimeZone = YES;        // Use timezone relative date/time format
+    }
+  [super setTimeZone: timeZone];
 }
 
 - (void) setVersion: (NSString*)v
@@ -1206,11 +1258,6 @@
     {
       _version = nil;
     }
-}
-
-- (NSTimeZone*) timeZone
-{
-  return tz;
 }
 
 - (NSString*) version


_______________________________________________
Gnustep-cvs mailing list
Gnustep-cvs@gna.org
https://mail.gna.org/listinfo/gnustep-cvs

Reply via email to