• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDate from xsd:dateTime / ISO 8601
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDate from xsd:dateTime / ISO 8601


  • Subject: Re: NSDate from xsd:dateTime / ISO 8601
  • From: Nathan Vander Wilt <email@hidden>
  • Date: Mon, 17 Mar 2008 16:30:29 -0700

Does -dateWithString: really not support the
"international [standard!] string representation" of
ISO 8601?? What's the right way to convert such an
xsd:dateTime to an NSDate?

+[NSDate dateWithString:] is, I think, configured to recognize one particular date format, which depends on your system locale. It's not very useful. What you want to do is customize an NSDateFormatter object to recognize your format, and then parse using it.


Here's some code I wrote recently. [...]

Thanks, I'd forgotten about NSDateFormatter. I took your code snippets and made the following function:


NSDate* dateFromISO8601(NSString* str) {
static NSDateFormatter* sISO8601 = nil;
if (!sISO8601) {
sISO8601 = [[NSDateFormatter alloc] init];
[sISO8601 setTimeStyle:NSDateFormatterFullStyle];
[sISO8601 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"]; // NOTE: problem!
}
if ([str hasSuffix:@"Z"]) {
str = [[str substringToIndex:(str.length-1)] stringByAppendingString:@"GMT"];
}
return [sISO8601 dateFromString:str];
}


This works, BUT...I had to edit the format a bit because my time strings (in this sample!) did not have the decimal seconds. However, I wouldn't be surprised to see input with sub-second precision. How can I specify optional format parts?

As it is, I might as well get rid of the hasSuffix checking and put a quoted 'Z' in the format, since that is actually far less likely to change than the seconds part in this case. I'd hate to have to go in search of a RegEx library or something similarly beyond the scope of this one line (ideally) of code, just to process a date in an ISO standard date format. Don't property lists store dates in 8601 format?

thanks,
-natevw

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSDate from xsd:dateTime / ISO 8601
      • From: Jens Alfke <email@hidden>
    • Re: NSDate from xsd:dateTime / ISO 8601
      • From: Nathan Vander Wilt <email@hidden>
References: 
 >NSDate from xsd:dateTime / ISO 8601 (From: Nathan Vander Wilt <email@hidden>)
 >Re: NSDate from xsd:dateTime / ISO 8601 (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: reading data from a UDP port - questions
  • Next by Date: Re: Prevent application activation after dragging items to the Dock icon?
  • Previous by thread: Re: NSDate from xsd:dateTime / ISO 8601
  • Next by thread: Re: NSDate from xsd:dateTime / ISO 8601
  • Index(es):
    • Date
    • Thread