Question

What NSTimeZone value does an NSDateFormatter use if you don't pass it in? I'd like to either use the default timeZone value, or pass in a given one. I'd like to do something like this:

- (NSString *) announcementTime{
    NSTimeZone *defaultTimeZone;  // What does this become?
    [self announcementTimeInTimeZone:defaultTimeZone];
}

- (NSString *) announcementTimeInTimeZone:(NSTimeZone *)timeZone{

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

  [formatter setDateStyle:NSDateFormatterNoStyle];
  [formatter setTimeStyle:NSDateFormatterShortStyle];
  [formatter setTimeZone:timeZone];

  return time;
}

What time zone do I want to pass in to my announcementTimeInTimeZone: method? Perhaps [NSTimeZone defaultTimeZone] or [NSTimeZone localTimeZone]. I'm just not sure what the default is and don't see it documented anywhere.

Was it helpful?

Solution

I could not find any documentation either but I had a play in the debugger and it looks like NSDateFormatter uses defaultTimeZone.

(lldb) p (NSTimeZone*)[f timeZone] (NSTimeZone *) $1 = 0x0a926fa0 @"Australia/Sydney"

(lldb) p (NSTimeZone*)[NSTimeZone defaultTimeZone] (NSTimeZone *) $2 = 0x0a926fa0 @"Australia/Sydney"

(lldb) po [NSTimeZone localTimeZone] (id) $2 = 0x0ab3bb10 Local Time Zone (Australia/Sydney (GMT+10:00) offset 36000)

"f" is an NSDateFormatter created just with alloc - init. As you can see the two time zones are exactly the same, stored at the same address.

OTHER TIPS

It will take a localTimeZone by default.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top