3

I noticed a nifty setting in the xcode simulator while running that I can change the current location and hence simulate location based tests

However, when I try to get the date using NSDateFormatter, the local date is still in PST. I am in the Pacific Time Zone

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; // Jan 1, 2010
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];  // 1:43 PM

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];
[usLocale release];

NSDate *testDate = [NSDate date];
NSLog(@"Local date: %@", [dateFormatter stringFromDate:testDate]);

According to the docs, stringFromDate should use the receivers current setting, which should use the timeZone of the Tokyo, Japan.

Is this expected?

Emil
  • 7,220
  • 17
  • 76
  • 135
mbh
  • 3,302
  • 2
  • 22
  • 24

1 Answers1

0

Chech the - timezone-attribute of the NSDateFormatter - I think this is controlled by the device itself (or your computer in this case), not the location. There is a setting in the settings-app however that lets you set the time by location, I'm not sure if that would change anything, but try activating that.

And a general tips: always test it on a device as well - I think the "Simulate Location"-setting works on the phone as well.

Edit: Just saw an answer that could be relevant, try calling [NSTimeZone resetTimeZone]; after setting a new location. That should trigger a reset and hopefully display the correct timezone. Ref: https://stackoverflow.com/a/5987409/267892

Community
  • 1
  • 1
Emil
  • 7,220
  • 17
  • 76
  • 135
  • 1. I tried on device. Simulate Location option not available there. Hence I was trying on simulator; 2. I am aware of timeZone settings on dateFormatter. My question was merely that simulate location should have simulated the time as well. I was not sure if its this way by design (i.e not simulating the time zone part) or I was doing something wrong. – mbh Jan 21 '12 at 23:44
  • See edited answer please, and I'm not sure if it's supposed to be that way, but in theory a change in location should trigger a timezone-update indeed. – Emil Jan 21 '12 at 23:47