Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting does not fully respect time zone #3

Closed
carllindberg opened this issue Sep 25, 2012 · 1 comment
Closed

Formatting does not fully respect time zone #3

carllindberg opened this issue Sep 25, 2012 · 1 comment
Labels

Comments

@carllindberg
Copy link
Contributor

Unless I'm missing something, it appears that while the time zone is being set in the associated NSCalendar instance, it is the timeZone set in the NSDateFormatter instance which is actually being used to format the time. Since the passed-in time zone is used to append the "Z" or the numeric time zone offset, the actual time seen can be a mismatch with the time zone. It appears as though the systemTimeZone or localTimeZone is being used by default.

The following program will print out different time values, even though they both appear to be using a UTC time zone, and both end up with a "Z" time zone in the string. The NSDateFormatter result is correct; ISO8601DateFormatter prints out an incorrect time (unless actually being run in a zero-offset time zone).

#import <Foundation/Foundation.h>
#import "ISO8601DateFormatter.h"

int main()
{
    id pool = [[NSAutoreleasePool alloc] init];

    ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc] init];
    [formatter setIncludeTime:YES];
    [formatter setDefaultTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; // UTC

    NSDateFormatter *nsformatter = [[NSDateFormatter alloc] init];
    [nsformatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    [nsformatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; // UTC

    NSDate *date = [NSDate date];
    NSString *isoFormatted = [formatter stringFromDate:date];
    NSString *nsFormatted = [nsformatter stringFromDate:date];

    NSLog(@"\nISO:    %@\nNSDate: %@", isoFormatted, nsFormatted);

    [pool release];
    exit(0);
    return 0;
}

The problem is fixed if you set unparsingFormatter.timeZone to the same value as unparsingCalendar.timeZone in the code.

This is running on MacOS 10.7; problem also observed using iOS 5.x.

boredzo added a commit that referenced this issue Aug 21, 2013
Fixes issue #3: Set the timeZone of the unparsingFormatter. Fixes a test failure.
boredzo added a commit that referenced this issue Sep 4, 2013
…ls (i.e., the change that fixes #5 is commented out).
@boredzo
Copy link
Owner

boredzo commented Sep 4, 2013

Well, considering I merged the fix and the test passes, I might as well close the ticket.

@boredzo boredzo closed this as completed Sep 4, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants