0

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.

Moshe
  • 57,511
  • 78
  • 272
  • 425

2 Answers2

1

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.

Community
  • 1
  • 1
Ben Trengrove
  • 8,191
  • 3
  • 40
  • 58
  • Yes, I added it to the answer above. They are different. – Ben Trengrove Sep 27 '12 at 04:13
  • This answer has some pretty good explanation on the timezone types. http://stackoverflow.com/questions/1526990/nstimezone-what-is-the-difference-between-localtimezone-and-systemtimezone – Ben Trengrove Sep 27 '12 at 04:14
  • 1
    It is stated in the [Data Formatting Guide](http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1), though not quite explicitly: «In all cases, you should consider that formatters default to using the user’s locale (`currentLocale`) superimposed with the user’s preference settings.» The locale includes a time zone. – jscs Sep 27 '12 at 17:30
0

It will take a localTimeZone by default.

Gerstmann
  • 5,368
  • 6
  • 37
  • 57
Kasaname
  • 1,491
  • 11
  • 15