NSDate object: local, and UTC 'zulu' time:: seconds are not matching

So today I am dealing with a server outside my timezone and started seeing something I think is weird....


Code:


on the phone...

- (NSString *)getUTCDateString: (NSDate *)theDate

{

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

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:SS.SSS'Z'"];

return [dateFormatter stringFromDate:theDate];

}

- (NSString *)getLocalDateString: (NSDate *)theDate

{

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

[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

return [dateFormatter stringFromDate:theDate];

}


...

NSDate *now = [NSDate date];

NSString *nowLocal = [self getLocalDateString:now];

NSString *nowUTC = [self getUTCDateString:now];


So, was I wrong in the hopes that the nowLocal and & nowUTC would be off by exactly an hour, and not have RANDOM seconds from "somewhere"???

This data is going into JSON, for transmission to the server.

Output I've been getting:


nowLocal = "2015-10-06 01:08:42";

nowUTC = "2015-10-06T19:08:48.487Z";

nowLocal = "2015-10-06 01:09:48";

nowUTC = "2015-10-06T19:09:58.588Z";

nowLocal = "2015-10-06 01:10:18";

nowUTC = "2015-10-06T19:10:15.150Z";


nowLocal = "2015-10-06 01:10:22";

nowUTC = "2015-10-06T19:10:75.754Z";


nowLocal = "2015-10-06 01:10:27";

nowUTC = "2015-10-06T19:10:67.674Z";


nowLocal = "2015-10-06 01:10:35";

nowUTC = "2015-10-06T19:10:74.748Z";


Anyone know what's going on here?


I am converting to "UTC from local" in iOS and then back to local in PHP in another timezone, and I need the seconds to match.


Am I really supposed to believe that everyone just ignores the seconds are off? No, these aren't transmit seconds!

Answered by dcaldwell_payzoom in 70954022

Okay, here is the thing about Stack Overflow, be careful what you use!


My problem was the format specifyer for UTC at the SS.SSS it should have been ss.SSS and only if I wanted the fractional seconds out three places.


The correct UTC Format string for iOS 7.0+ = "yyyy-MM-dd'T'HH:mm:ss'Z'"

big S vs little s...

Thanks Microsoft for raising me 'case unobservant for most things' in the computer industry...


Took me a while to find it,

'ss' is for seconds with the fraction dropped

'SS.SSS' is for seconds with the fraction, out 3 places..


Still not understanding why the seconds are off, and why it swings wildly around....

Accepted Answer

Okay, here is the thing about Stack Overflow, be careful what you use!


My problem was the format specifyer for UTC at the SS.SSS it should have been ss.SSS and only if I wanted the fractional seconds out three places.


The correct UTC Format string for iOS 7.0+ = "yyyy-MM-dd'T'HH:mm:ss'Z'"

NSDate object: local, and UTC 'zulu' time:: seconds are not matching
 
 
Q