Last active
August 29, 2015 14:14
-
-
Save lukewar/07711ac7e0fba0434883 to your computer and use it in GitHub Desktop.
ISO8601 parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <time.h> | |
// Source http://blog.soff.es/how-to-drastically-improve-your-app-with-an-afternoon-and-instruments/ | |
+ (NSDate *)dateFromISO8601String:(NSString *)string { | |
if (!string) { | |
return nil; | |
} | |
struct tm tm; | |
time_t t; | |
strptime([string 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 *)ISO8601String { | |
struct tm *timeinfo; | |
char buffer[80]; | |
time_t rawtime = [self timeIntervalSince1970] - [[NSTimeZone localTimeZone] secondsFromGMT]; | |
timeinfo = localtime(&rawtime); | |
strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S%z", 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