Skip to content

Instantly share code, notes, and snippets.

@chbeer
Created June 10, 2013 09:33
Show Gist options
  • Save chbeer/5747548 to your computer and use it in GitHub Desktop.
Save chbeer/5747548 to your computer and use it in GitHub Desktop.
Methods to create and parse ISO date strings without NSDateFormatter.
NSDate *CBCBDateFromISOString(NSString *iso)
{
struct tm tm;
time_t t;
if (iso.length == 10) {
strptime([iso cStringUsingEncoding:NSUTF8StringEncoding], "%Y-%m-%d", &tm);
tm.tm_isdst = -1;
tm.tm_sec = 0;
tm.tm_min = 0;
tm.tm_hour = 0;
t = mktime(&tm);
} else {
strptime([iso cStringUsingEncoding:NSUTF8StringEncoding], "%Y-%m-%dT%H:%M:%S%z", &tm);
tm.tm_isdst = -1;
t = mktime(&tm);
}
return [NSDate dateWithTimeIntervalSince1970:t + [[NSTimeZone localTimeZone] secondsFromGMT]];
}
NSString *CBCBISOStringFromDate(NSDate *date)
{
struct tm *timeinfo;
char buffer[80];
time_t rawtime = [date timeIntervalSince1970] - [[NSTimeZone localTimeZone] secondsFromGMT];
timeinfo = localtime(&rawtime);
strftime(buffer, 80, "%Y-%m-%dT%H:%M:%SZ", timeinfo);
return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment