RFC 3339 Date format - Decreasing date for PST timezone

Hi All,


We have received a bug in our application saying that the values were shifted by one day back.


We have developed this application using the Objctive-C and please find the code snippet below.


-(NSString *)getDisplayDateFormat:(NSString *)dateString

{

NSLocale * enUSPOSIXLocale;

NSDateFormatter *inputformatter = [[NSDateFormatter alloc] init];

enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];

[inputformatter setLocale:enUSPOSIXLocale];

inputformatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

[inputformatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];

NSDate *date= [inputformatter dateFromString:dateString];


NSDateFormatter *OutPutformatter = [[NSDateFormatter alloc] init];

[OutPutformatter setDateStyle: NSDateFormatterShortStyle];

NSString *dispalyDateString = [OutPutformatter stringFromDate:date];

return dispalyDateString;


}


dateString = 2017-01-16T00:00:00-06:00

date = 2017-01-16 06:00:00 +0000

displayDateString = 1/15/17


I have given the input a 16th Feb but got the output 15th Feb. Need your inputs to resolve the issue.

dateString = 2017-01-16T00:00:00-06:00
date = 2017-01-16 06:00:00 +0000
displayDateString = 1/15/17

These results make perfect sense to me:

  • dateString
    is the start of (that is, midnight) 16 Jan, UTC-6
  • That translates to 6am UTC, which is what

    date
    shows
  • 8 hours earlier than that is 10pm on 15 Jan, hence

    displayDateString

Thinking about it in terms of the US vernacular, midnight in Central on day N translates to 10pm in Pacific on day N-1.

I think the problem here is one of specification. You’ve taken an absolute position in time and mapped it from one time zone to another and then are weirded out by the fact that the date has changed, but such changes are completely normal. If you want to display the same date everywhere, you have to change your approach, something that’s completely fine to do. If you can describe the approach you want to use, I can point you in the right direction API-wise. However, you’ll have to come up with the basic approach, because that’s something that has to make sense to you and your users.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
RFC 3339 Date format - Decreasing date for PST timezone
 
 
Q