如何将UTC时代1352716800解析为NSDate? [英] How to parse a UTC epoch 1352716800 to NSDate?

查看:63
本文介绍了如何将UTC时代1352716800解析为NSDate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从XML文件读取数据,该文件的UTC日期类似于"2011-05-04T00:00:00",而UTC时期类似于1352716800.

I'm reading data from a XML file which has a UTC date looking like "2011-05-04T00:00:00", and a UTC epoch looking like 1352716800.

将UTC时代解析为NSDate可能比弄乱复杂的日期格式要安全得多.如何将UTC时代解析为NSDate?使用NSDateFormatter和"UTC时代"的特殊格式吗?

Parsing the UTC epoch to NSDate would probably be much safer than messing around with a complex date format. How would I parse the UTC epoch to NSDate? With NSDateFormatter and a special format for "UTC Epoch"?

我认为这是 [[NSDate alloc] initWithTimeIntervalSince1970:epoch] ,并且似乎可以进行测试.但是我不确定这是否是偶然的正确,还是"UTC时代"是自1970年以来".苹果文档没有提到UTC时代.

I think that it is [[NSDate alloc] initWithTimeIntervalSince1970:epoch] and a test seemed to work. But I am not sure if that's just correct by accident or if the "UTC epoch" is "Since 1970". The Apple Docs don't mention UTC Epoch.

推荐答案

是的,您是正确的,它是UTC时代.作为参考,如果"Epoch时间是UTC",请结帐

YES, you are correct it is UTC Epoch. For Reference if "Epoch time is UTC" checkout this

NSString *epochTime = @"1352716800";

// (Step 1) Convert epoch time to SECONDS since 1970
NSTimeInterval seconds = [epochTime doubleValue];
NSLog (@"Epoch time %@ equates to %qi seconds since 1970", epochTime, (long long) seconds);

// (Step 2) Create NSDate object
NSDate *epochNSDate = [[NSDate alloc] initWithTimeIntervalSince1970:seconds];
NSLog (@"Epoch time %@ equates to UTC %@", epochTime, epochNSDate);

这篇关于如何将UTC时代1352716800解析为NSDate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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