Iphone 当我们更改时区时,需要以秒为单位显示时间而不随时间变化

Iphone 当我们更改时区时,需要以秒为单位显示时间而不随时间变化,iphone,ios7,nsdate,nstimer,nstimeinterval,Iphone,Ios7,Nsdate,Nstimer,Nstimeinterval,我目前正在进行投标申请,投标申请的工作主要是基于时间,我用来计算时间使用 NSLog(@"date %d",(int)[[NSDate date] timeIntervalSince1970]); output:1409031539 我计算了服务器时间,比较了设备时间和计算结果,但是在更改时区时,这个非工作时间会有所不同,因为我使用了这段代码 NSLocale* currentLocale = [NSLocale currentLocale]; NSLog(@"date: %d",(int

我目前正在进行投标申请,投标申请的工作主要是基于时间,我用来计算时间使用

NSLog(@"date %d",(int)[[NSDate date] timeIntervalSince1970]);
output:1409031539
我计算了服务器时间,比较了设备时间和计算结果,但是在更改时区时,这个非工作时间会有所不同,因为我使用了这段代码

 NSLocale* currentLocale = [NSLocale currentLocale];
 NSLog(@"date: %d",(int)[[NSDate date] descriptionWithLocale:currentLocale]);
产出:196208512

但这并不能帮助我计算运行代码的时间,我看到了不同的输出。

这一行:

 NSLog(@"date: %d",(int)[[NSDate date] descriptionWithLocale:currentLocale]);
是不是错了,我的头受伤了。
descriptionWithLocale
返回的数据是
NSString*
,而不是
NSTimeInterval
,将其打印为
int
只是将指针转换为
int
,这在最好的情况下是个坏主意

我假设您的服务器返回的时间戳也是1970年后的#秒,因此我在这段代码中硬编码了一个示例:

NSLocale* currentLocale = [NSLocale currentLocale];
NSTimeInterval nsti = [[NSDate date] timeIntervalSince1970];
NSLog(@"date (as seconds from 1970): %ld", (long)nsti);
NSLog(@"date (as locale-relative string): %@", [[NSDate dateWithTimeIntervalSince1970:nsti] descriptionWithLocale:currentLocale]);
 // This is Mon 25 Aug 2014, 20:17:51 BST (UTC +0100) obtained by date +%s, subtracting 60,000
NSTimeInterval time_from_server = 1408994271;
NSLog(@"date (from server as string): %@", [[NSDate dateWithTimeIntervalSince1970:time_from_server] descriptionWithLocale:currentLocale]);
但是,请注意,
NSDate
NSTimeInterval
都没有时区的概念,因此没有与GMT的偏移量的概念。您需要的是
NSTimeZone
。请记住,GMT的偏移量取决于当前日期/时间(例如夏季时间、冬季时间),您必须使用如下代码:

NSTimeZone *localZone = [NSTimeZone localTimeZone];
NSDate *date111 = [NSDate date];
NSInteger delta = [localZone secondsFromGMTForDate:date111];
NSLog(@"date= %ld", (long)delta);
获取时区,获取日期,获取日期相对于GMT的偏移量,记录它

将时间显示为特定时区的本地时间:

NSTimeZone *phoenix = [NSTimeZone timeZoneWithName:@"America/Phoenix"];
NSTimeZone *dublin = [NSTimeZone timeZoneWithName:@"Europe/Dublin"];
NSTimeZone *tokyo = [NSTimeZone timeZoneWithName:@"Asia/Tokyo"];
NSDate *date111 = [NSDate date];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateStyle:NSDateFormatterFullStyle];
[fmt setTimeStyle:NSDateFormatterFullStyle];
[fmt setLocale:[NSLocale currentLocale]];
[fmt setTimeZone:phoenix];
NSLog(@"phoenix %@", [fmt stringFromDate:date111]);
[fmt setTimeZone:dublin];
NSLog(@"dublin %@", [fmt stringFromDate:date111]);
[fmt setTimeZone:tokyo];
NSLog(@"tokyo %@", [fmt stringFromDate:date111]);
给我(截至目前):

这里的基本概念是:

  • NSDate没有时区的概念
  • NSTimeInterval没有时区的概念
  • 只有格式化显示时间的行为需要时区

因此,记住这一点,当你获取设备时间时,它只是一个NSDate或一个NSTIMEIVAL,这意味着它不将时区编码到其中。

你需要获取两个日期之间的时间距离?不,我需要以秒为单位获取设备时间,而不是[NSDate date]我需要时间111秒。。。。NSLocale*currentLocale=[NSLocale currentLocale];NSLog(@“日期:%@,[[NSDate Date]descriptionWithLocale:currentLocale]);NSDate date111=(NSDate)[[NSDate date]descriptionWithLocale:currentLocale];NSTimeInterval interval=[date111 timeIntervalSince1970];NSLog(@“日期=%f”,间隔);我尝试了这段代码,但应用程序在中崩溃NSTimeinterval不需要语言环境时间吗?您不能将
NSString*
转换为
NSDate
-这根本不起作用(这是
date111=…
行)。这听起来像是一个错误。这个间隔应该代表什么?UTC时间和本地时间之间的秒差?我已经更新了答案,使用本地时区获取NSDate的GMT秒偏移量。但是如果打印的nslog与American/Phoneix时区核对,它显示在1970年。我使用此Url检查时间戳。
phoenix Tuesday, August 26, 2014 at 9:33:12 AM Mountain Standard Time
dublin Tuesday, August 26, 2014 at 5:33:12 PM Irish Summer Time
tokyo Wednesday, August 27, 2014 at 1:33:12 AM Japan Standard Time