我试过一个实验,因为我需要能够在我正在处理的应用程序中生成unix时间戳(自1970年以来)。NSDate dateWithTimeIntervalSince1970不返回GMT/UTC时间
NSLog(@"Getting timeIntervalSince1970");
double theLoggedInTokenTimestampDateEpochSeconds = [[NSDate date] timeIntervalSince1970];
这应(自1970年1月1日秒)格林尼治标准已经返回划时代秒(自1970年)。然而,在周一在8月15日九点54分30秒2011在进行实验时,它返回1313427270.504315
测试这与我的Mac OS终端一个简单的Perl的一行,我得到: perl -e 'print scalar(localtime(1313427270))'
返回周一八月15 09:54:30 2011 ...
这显然不是格林威治标准时间,当我在SF湾区,我的当地时区设置为“库比蒂诺”。怎么回事,我该如何解决它?我需要让我的应用程序在通信时向服务器发送UTC时间,以便用户在任何时间时间戳发送到一个平等有效的时区。 -
在我的应用程序的另一部分,当从服务器用户请求数据时,它得到它UTC发送将其转换成显示如下:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:nil];
[dateFormatter setDateFormat:@"yyyyMMdd"];
NSDate *conversationDate = [NSDate dateWithTimeIntervalSince1970:[theConversationTimeStampString intValue]];
NSString *conversationDateString = [dateFormatter stringFromDate:conversationDate];
[dateFormatter release];
这精美的作品 - 显示在用户的时区和区域设置中更正的时间...所以我知道正在完成传入的时间戳。那么,我在做什么错误的第一个函数(timeIntervalSince190),阻止它在GMT?
THX
'1313427270.504315'是自1970年1月1日以来的秒数,格林威治标准时间,这是一个不依赖于你是哪个时区的绝对度量。它对应于格林尼治标准时间的“2011-08-15 16:54:30”和加州的“2011-08-15 09:54:30”。两者都是*同一时刻*的不同表示。 – albertamg