xcode 4.2 位置模拟器和时区 [英] xcode 4.2 location simulator and Time Zone

查看:57
本文介绍了xcode 4.2 位置模拟器和时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时注意到 xcode 模拟器中有一个漂亮的设置,我可以更改当前位置,从而模拟基于位置的测试

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

但是,当我尝试使用 NSDateFormatter 获取日期时,本地日期 仍在 PST 中.我在太平洋时区

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]);

根据文档,stringFromDate 应使用接收器当前设置,该设置应使用日本东京的时区.

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

这是预期的吗?

推荐答案

检查 -timezone-NSDateFormatter 的属性 - 我认为这是由设备本身控制的(或在这种情况下您的计算机),而不是位置.设置应用程序中有一个设置,可让您按位置设置时间,我不确定这是否会改变任何内容,但请尝试激活它.

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.

刚刚看到一个可能相关的答案,尝试在设置新位置后调用 [NSTimeZone resetTimeZone]; .这应该会触发重置并希望显示正确的时区.参考:https://stackoverflow.com/a/5987409/267892

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

这篇关于xcode 4.2 位置模拟器和时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆