Author: rfm
Date: Wed Apr  5 12:54:03 2017
New Revision: 40448

URL: http://svn.gna.org/viewcvs/gnustep?rev=40448&view=rev
Log:
fixups for json timezone/timestamp encoding

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=40448&r1=40447&r2=40448&view=diff
==============================================================================
--- libs/webservices/trunk/GWSCoder.h   (original)
+++ libs/webservices/trunk/GWSCoder.h   Wed Apr  5 12:54:03 2017
@@ -478,7 +478,7 @@
 
 /** Take the supplied date and encode it as an XMLRPC timestamp.<br />
  * This uses the timezone currently set in the receiver to determine
- * the time of day encoded.
+ * the time of day encoded.  The format is YYYYMMDDTHH:MM:SS
  */
 - (NSString*) encodeDateTimeFrom: (NSDate*)source;
 
@@ -585,10 +585,16 @@
                parameters: (NSDictionary*)parameters
                     order: (NSArray*)order;
 
+/** A helper method which decodes a timestamp from ISO8601 format,
+ * YYYY-MM-DDTHH:MM:SS.mmmZ
+ */
+- (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.
+ * There is no standard for JSON timestamps, but the recommended value
+ * (supported by javascript) is ISO8601 ... YYYY-MM-DDTHH:MM:SS.mmmZ
  */
 - (NSString*) encodeDateTimeFrom: (NSDate*)source;
 
@@ -606,6 +612,10 @@
  */
 - (void) setRPCID: (id)o;
 
+/** Does nothing ... JSON always uses GMT as the time zone.
+ */
+- (void) setTimeZone: (NSTimeZone*)timeZone;
+
 /** Sets the json-rpc version.<br />
  * May be "2.0" or "1.0" (any other non-nil value is currently considered
  * to be version 1.0) or nil if JSON-RPC is not to be used.<br />
@@ -615,6 +625,10 @@
  * 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=40448&r1=40447&r2=40448&view=diff
==============================================================================
--- libs/webservices/trunk/GWSJSONCoder.m       (original)
+++ libs/webservices/trunk/GWSJSONCoder.m       Wed Apr  5 12:54:03 2017
@@ -41,6 +41,7 @@
 static Class    NSNullClass;
 static Class    NSNumberClass;
 static Class    NSStringClass;
+static NSTimeZone       *tz;
 
 static NSString*
 JSONQuote(NSString *str)
@@ -579,6 +580,7 @@
   boolY = [[NSNumberClass numberWithBool: YES] retain];
   boolN = [[NSNumberClass numberWithBool: NO] retain];
   null = [[NSNullClass null] retain];
+  tz = [[NSTimeZone timeZoneWithName: @"GMT"] retain];
 }
 
 - (void) appendObject: (id)o
@@ -949,12 +951,49 @@
   [super dealloc];
 }
 
+- (NSCalendarDate*) decodeDateTimeFrom: (NSString*)source
+{
+  int                  year;
+  int                  month;
+  int                  day;
+  int                  hour;
+  int                  minute;
+  int                  second;
+  int                   millisecond;
+  NSTimeInterval       ti;
+  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)
+    {
+      [NSException raise: NSInvalidArgumentException
+                  format: @"bad date/time format '%@'", source];
+    }
+  d = [NSCalendarDate alloc];
+  d = [d initWithYear: year
+                month: month
+                  day: day
+                 hour: hour
+               minute: minute
+               second: second
+             timeZone: tz];
+
+  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"
-                                   timeZone: [self timeZone]
+  s = [source descriptionWithCalendarFormat: @"%Y-%m-%dT%H:%M:%S.%FZ"
+                                   timeZone: tz
                                      locale: nil];
   return s;
 }
@@ -1148,6 +1187,11 @@
     }
 }
 
+- (void) setTimeZone: (NSTimeZone*)timeZone
+{
+  return;
+}
+
 - (void) setVersion: (NSString*)v
 {
   if (YES == [ver2 isEqual: v])
@@ -1162,6 +1206,11 @@
     {
       _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