Tag: utc

NSDate dateWithTimeIntervalSince1970不返回GMT / UTC时间

我尝试了一个实验,因为我需要能够在我正在工作的应用程序中生成一个unix时间戳(自1970年以来)。 NSLog(@"Getting timeIntervalSince1970"); double theLoggedInTokenTimestampDateEpochSeconds = [[NSDate date] timeIntervalSince1970]; 这应该已经在格林尼治标准时间(1970年1月1日以来的秒)内返回了纪元秒(自1970年以来)。 但是,在2011年8月15日星期一09:54:30进行实验时,返回1313427270.504315 在Mac OSterminal上用一个简单的perl单行testing,得到: perl -e 'print scalar(localtime(1313427270))' ,它返回星期一8月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]; 而且这个工程很漂亮 – 在用户的时区和区域显示更正的时间…所以我知道正在完成传入的时间戳。 […]

UTCdate和UTC时间计算

我有一个string,我知道将引用UTC时间戳 @"2016-01-20T17:00:00" 现在当它转换为一个实际的NSDate它打印时返回这个: NSLog("%@", [TimeHelper stringToDateInUtc:@"2016-01-20T17:00:00"]); -> 2016-01-20 18:00:00 +0000 与UTCdate相同的东西: NSLog(@"Time in UTC Now: %@", [TimeHelper getNowUtcAsDate]); 我也有这种方法获取UTC的当前时间,并将string转换为UTC: + (NSDate*) stringToDateInUtc:(NSString*)str { NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; return [formatter dateFromString:str]; } + (NSString*) getNowUtc { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; NSDate *now […]

NSDateFormatter麻烦?

我试图parsing一个RSS提要,但date很难parsing。 datestring是: publishedDate = "2013-01-08T20:09:02.000Z" 这是我的代码: NSDateFormatter *utc = [[NSDateFormatter alloc] init]; [utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; [utc setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; NSDate *dtUTC = [utc dateFromString: publishedDate]; NSDateFormatter *dfLocal = [[NSDateFormatter alloc] init]; [dfLocal setDateFormat:@"yyyy-MM-dd"]; [dfLocal setTimeZone:[NSTimeZone localTimeZone]]; NSString *time =[dfLocal stringFromDate:dtUTC]; NSLog(@"original UTC %@ now local: %@", dtUTC, time); 为什么date返回零? 此外,我试图将UTC时间转换为CST时间。