iOS UTC秒数时间戳和日期的互相转换[转载]

来源:互联网 发布:日军退子弹 知乎 编辑:程序博客网 时间:2024/06/06 20:05

参考:

iOS UTC秒数时间戳和日期的相互转换【原创】


什么是UTC?
协调世界时,又称世界统一时间,世界标准时间,国际协调时间,简称UTC。不属于任意时区

简单介绍了一下,想详细了解的自己百度了。

UTC秒数和日期相互转换,现在直接上代码

NSDate *date = [NSDate date];NSLog(@"当前日期为:%@",date);NSTimeInterval timeStamp= [date timeIntervalSince1970];NSLog(@"日期转换为时间戳 %@ = %f", date, timeStamp);
时间戳转日期 (秒数转日期)
NSString *timeStamp2 = @"1414956901";long long int date1 = (long long int)[timeStamp2 intValue];NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:date1];NSLog(@"时间戳转日期 %@  = %@", timeStamp2, date2);


补充:(该内容为原创,参考苹果官方API)

THREAD SAFETY

On iOS 7 and later NSDateFormatter is thread safe.

On OS X v10.9 and later NSDateFormatter is thread safe so long as you are using the modern behavior in a 64-bit app.

On earlier versions of the operating system, or when using the legacy formatter behavior or running in 32-bit on OS X, NSDateFormatter is not thread safe and you therefore must not mutate a date formatter simultaneously from multiple threads.

翻译为中文为:
线程安全性:
自iOS 7及以后,NSDateFormatter是线程安全的。
自OSX v10.9及以后,NSDateFormatter是线程安全的,只要你所开发的事64位app。
在这些版本之前以及32位OS X上,NSDateFormatter是非线程安全的。因此,你不能同时使formatter在多个线程中进行变化。

关于线程安全的知识,想要了解的请自己百度了。个人认为苹果官方API解释得已经很清楚了,如果有不明白的,可以留言,本人看见留言后,会尽快回复。

1 0
原创粉丝点击