I am working with a legacy OS X application to fix a bug with timestamps that are 1 hour off. I am mainly a C# developer and have no previous experience with Objective-C, so I don't really know what I am doing. :)
This is the helper function that creates an NSString with the current time:
+ (NSString*)formattedDate {
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss."];
dateString = [formatter stringFromDate:[NSDate date]];
dateString = [dateString stringByReplacingOccurrencesOfString:@" " withString:@"T"];
dateString = [dateString stringByAppendingString:@"0000000+01:00"];
return dateString;
}
I would guess it's the line that appends "0000000+01:00" that sets the timezone to GMT+1? During summer the time zone is GMT+2 here, so that would at least explain it.
Would the right fix be something like this:
+ (NSString*)formattedDate {
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss."];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:[[NSTimeZone localTimeZone] abbreviation]]];
dateString = [formatter stringFromDate:[NSDate date]];
dateString = [dateString stringByReplacingOccurrencesOfString:@" " withString:@"T"];
return dateString;
}
Or will that do something different than the append?
The date string is sent from the client application to a server application as part of an XML message.
I updated the code to the following, and it worked:
+ (NSString*)formattedDate {
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ"];
dateString = [formatter stringFromDate:[NSDate date]];
return dateString;
}