NSString* iso8601TestDate = @"2017-03-30T20:36:46.245Z";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
NSLocale* posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:posix];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate* testDate = [dateFormatter dateFromString:iso8601TestDate];
console.log("Test date", testDate);
Objective-J code results in a testDate value in local time zone:
var iso8601TestDate = @"2017-03-30 20:36:46.245";
var dateFormatter = [[CPDateFormatter alloc] init];
var posix = [[CPLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:posix];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
var testDate = [dateFormatter dateFromString:iso8601TestDate];
console.log("Test date", testDate);
The source code for CPDateFormatter seems to be missing handlers for the 'T', 'SSS' and 'Z' tokens, but I'm still reading it.
We're using Couchdb and relatives for data persistence, which stores data in JSON format - thus needing a standardized textual representation for dates.
We don't particularly need millisecond precision nor do I trust the browsers clock that well, but Javascript Date's toISOString() does support it, so it seems we should too.
Can anyone confirm this problem or point out where I'm mistaken before I fix it?
d.r.
It does, but there appears to be a bug in the parser. There appears to be at two things going wrong: 1) the parser is not handling text elements properly (AFAICT, it is simply ignoring the pattern as it occurs in the string and format, rather than parsing it); and 2) it doesn’t recognize a single quote or period as a delimiter. If you change your date to:
var iso8601TestDate = @"2017-03-30 ’T'20:36:46:245’Z'";
and your format to:
[dateFormatter setDateFormat:@"yyyy-MM-dd ’T'HH:mm:ss:SSS'Z'"];
It will work. Note the space before the ’T’. Without the space the day (I think) gets absorbed and although the string parses it does not give the correct date. I would file a bug report.
Hi guys,,
It has been a long time since I worked on that code but I think we never did work specifically on 8601 parsing for CPDateFormatter
, but rather we focused on getting the main UI based formatting to work.
Instead Cappuccino provides Date.parseISO8601
on the native
Date object in JS. That’s in fact what Ratatosk uses.
There’s no reason for CPDateFormatter
not to support textual tokens the same way Objective-C does. I just don’t think anybody worked on it.
--
You received this message because you are subscribed to the Google Groups "Cappuccino & Objective-J" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectivej+...@googlegroups.com.
To post to this group, send email to objec...@googlegroups.com.
Visit this group at https://groups.google.com/group/objectivej.
For more options, visit https://groups.google.com/d/optout.
Hi guys,,
It has been a long time since I worked on that code but I think we never did work specifically on 8601 parsing for
CPDateFormatter
, but rather we focused on getting the main UI based formatting to work.Instead Cappuccino provides
Date.parseISO8601
on the native Date object in JS. That’s in fact what Ratatosk uses.There’s no reason for
CPDateFormatter
not to support textual tokens the same way Objective-C does. I just don’t think anybody worked on it