Reputation: 3154
strptime_l
is always returning null. I'm doing this in Objective-C.
#import <time.h>
#import <xlocale.h>
/* Later on... */
const char *str = [dateStr UTF8String];
const char *fmt = [@"EE LLLL d HH:mm:ss Z yyyy" UTF8String];
struct tm timeinfo;
memset(&timeinfo, 0, sizeof(timeinfo));
char *ret = strptime_l(str, fmt, &timeinfo, NULL);
NSDate *date = nil;
if (ret) {
time_t time = mktime(&timeinfo);
date = [NSDate dateWithTimeIntervalSince1970:time];
}
ret
is always null. An example of dateStr
's value is: Sat Sep 15 05:52:10 +0000 2012
and is always in that format.
Any ideas?
Upvotes: 1
Views: 1069
Reputation: 10096
You seem to be using the NSDateFormatter format instead of the strptime one. Take a look here for more info its specific date and time format.
The following code should work:
NSString *dateStr = @"Sat Sep 15 05:52:10 +0000 2012";
const char *str = [dateStr UTF8String];
const char *fmt = [@"%a %b %d %H:%M:%S %z %Y" UTF8String];
struct tm timeinfo;
memset(&timeinfo, 0, sizeof(timeinfo));
char *ret = strptime_l(str, fmt, &timeinfo, NULL);
NSDate *date = nil;
if (ret) {
time_t time = mktime(&timeinfo);
date = [NSDate dateWithTimeIntervalSince1970:time];
}
By the way you could have achieved the same by simply doing:
NSString *dateStr = @"Sat Sep 15 05:52:10 +0000 2012";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE LLLL d HH:mm:ss Z yyyy"];
NSDate *date = [dateFormat dateFromString:dateStr];
Upvotes: 3