处理可可中的时区

处理可可中的时区

本文介绍了处理可可中的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想澄清一下我是否了解日期&

I just want to clarify if I am understanding how dates & time zones work.

基本上,我有一个日期字符串@ 2008-07-06 12:08:49,我想将其转换为NSDate。我希望此日期和时间在当前用户的时区中设置。因此,如果他们使用的是格林尼治标准时间(GMT)或HST,则仍为12:08:49。

Basically, I have a date string @"2008-07-06 12:08:49" that I want to convert to an NSDate. I want this date and time to be in whatever the current user's time zone is set in. So if they are in GMT or HST, it's still 12:08:49.

如果我的日期格式为unix 1215382129(UTC),并且我的时区设置为伦敦(GMT),则NSLog()的输出日期为:

If I have date in unix form 1215382129 (UTC) and my time zone is set to London (GMT), the outputted date from NSLog() is:

2008- 07-06 12:08:49 +0100

2008-07-06 12:08:49 +0100

如果我将时区更改为夏威夷(HST)并输出相同的日期,则会得到:

If I then change my time zone to Hawaii (HST) and output the same date, I get:

2008-07-06 12:08:49 -1000

2008-07-06 12:08:49 -1000

这似乎很好,但我印象深刻要在夏威夷语中获取时间,我必须将时差(-10小时)添加到Unix时间戳中。

This seems to work fine, but I was under the impression to get the time in Hawaiian, I'd have to physically add the time difference (-10hrs) to the unix time stamp. Is this not required then?

这是否意味着,无论unix时间指向什么日期和时间,它始终指向同一时区的相同日期和时间。用户在吗?

Does that mean, whatever date and time a unix time is pointing to, it always points to the same date and time in whatever time zone a user is in?

希望这很有意义!

编辑

我刚刚意识到(感谢Kevin Conner!),实际上NSDateFormatter会根据当前时区为该日期字符串创建不同的unix时间戳!所以我完全错了! :-)

I've just realised (thanks to Kevin Conner!) that in fact NSDateFormatter is creating different unix timestamps for that date string depending on the current timezone! So I was totally wrong!! :-)

推荐答案

免责声明,我主要是Java人。但是在这方面,Cocoa似乎像Java库一样工作:日期是无区域的时间戳。时区位于格式化日期的域中以进行显示。换句话说,内部格式不考虑时区,而全部采用UTC。时区对人类来说是一个相对方便的地方,因此它们位于显示/解析方面。

Disclaimer, I'm mostly a Java guy. But Cocoa seems to work like the Java library in this regard: Dates are zoneless timestamps. Time zones are in the domain of formatting dates for display. In other words, the internal format doesn't consider time zones, it's all in UTC. Time zones are relatively a convenience for humans, so they are in the display/parsing side.

我注意到有一个。在执行格式化之前,请尝试在格式化程序上调用它。

I noticed there is a setTimeZone: method on NSDateFormatter. Try calling that on your formatter before performing the format.

这篇关于处理可可中的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:36