Skip to content

Instantly share code, notes, and snippets.

@nataliepo
Created February 15, 2013 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nataliepo/4961073 to your computer and use it in GitHub Desktop.
Save nataliepo/4961073 to your computer and use it in GitHub Desktop.
NSDateFormatter bug
// dateString expected format: "2012-08-08 17:31:01 Etc/GMT"
// This value comes from our receipt-verification service,
// so it's independent of any of the user's settings
+(NSDate*)translateExpirePurchaseStringToDate: (NSString*)dateString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// strip the ending
NSString *purchaseDateString = [dateString stringByReplacingOccurrencesOfString:@" Etc/GMT" withString:@""];
// set the gmt timezone just in case
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// -- Here was my bug:
// The purchaseDate value assignment below is nil when the user's
// preferences are: UK Calendar, London timezone with 24-hour time OFF.
//
// Having the user turn 24-hour time on in their phone's preferences
// yielded an NSDate just fine.
//
// It wasn't what I'd expected, since the input value is totally
// independent of the user's settings. :'(
//
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *purchaseDate = [dateFormatter dateFromString:purchaseDateString];
//
// ... do some other unrelated things
//
return purchaseDate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment