One Quickie


Converting xsd:date to NSDate (NSDate->General)
xsd:date has a format that's a subset of the full ISO8601 format. Here's a quick way to convert an xsd:date to an NSDate. Based on a forum posting by Jens Alfke. For a full ISO 8601 parser, check out the one by Peter Hosey.

Note that thiscode

NSDate *xsdDateTimeToNSDate (NSString *dateTime) {
    static NSDateFormatter *xsdDateTimeFormatter;
    if (!xsdDateTimeFormatter) {
        xsdDateTimeFormatter = [[NSDateFormatter alloc] init];  // Keep around forever
        xsdDateTimeFormatter.timeStyle = NSDateFormatterFullStyle;
        xsdDateTimeFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:sszzz";
    }

    // Date formatters don't grok a single trailing Z, so make it "GMT".
    if ([dateTime hasSuffix: @"Z"]) {
        dateTime = [[dateTime substringToIndex: dateTime.length - 1]
                       stringByAppendingString: @"GMT"];
    }

    NSDate *date = [xsdDateTimeFormatter dateFromString: dateTime];
    if (!date) NSLog(@"could not parse date '%@'", dateTime);

    return (date);

} // xsdDateTimeToNSDate



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com