NSDate dateFromString,如何解析'围绕'UTC,GMT和用户区域设置? [英] NSDate dateFromString, how to parse 'around' UTC, GMT and User locale?

查看:102
本文介绍了NSDate dateFromString,如何解析'围绕'UTC,GMT和用户区域设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从xml文件解析一些值。
有一个 @25-12-2010'T'23:40:00字符串中包含时间和日期,并且有一个GMT字符串这样偏移@+ 0200。
所以上面的时间是12月23日40:00的25.在时间区+ 0200 GMT。 (或21:40 UTC)
我有很多这些日期与不同的GMT抵消。我必须将这些日期显示为
,即不得更改它们以适合用户的区域设置。因此,如果时间1:是22:45 +0500那么这就是我必须向用户显示的内容,即使用户位于不同的时区。



我有各种各样的显示,计算和解析这些字符串的麻烦。



如果我使用dateFormatter和 dateFromString 用户特定的GMT信息将包含在结果 NSDate 中,意味着上述内容将保存为格林威治标准时间23:40:00 +0100因为这是我的手机设置,可能是23:40:00 - 0400来自纽约新手机的用户。



当我随后在这些日期之间进行减法,加法和比较时,我必须保持GMT偏移,一切都会变得更糟手机切换区域设置,从解析日期到显示日期...



我有没有办法从字符串中提取此日期UTC,然后将其保存为间隔而不是实际(时区相关)日期。我知道日期总是在内部保存。但我无法弄清楚如何使用单独的GMT字符串并考虑用户区域设置。



干杯

setTimeZone:。您还需要将时区偏移量与NSDate分开存储,以便稍后再次显示这些时区中的时间。


I parse some values from an xml file. There is a @"25-12-2010'T'23:40:00"string with the time and date and there is a string with the GMT offset like this @"+0200". So the above time is the 25. of December 23:40:00 in timeZone +0200 GMT. (or 21:40 UTC) I have lots of these dates with different GMT offsets. I have to display these dates as they are, i.e. They must not be changed to fit the locale of the user. So if time 1: is 22:45 +0500 then that is what I must show the user, even if the user is in a different timezone.

I have all sorts of trouble with displaying, calculating and parsing these strings.

If I use a dateFormatter and dateFromString the user specific GMT info will be included in the resulting NSDate meaning the above will be saved as 23:40:00 +0100 GMT because that is my phones setting and maybe 23:40:00 −0400 on a user from new New York's phone.

When I subsequently do subtraction, addition and comparisons between these dates I have to keep the GMT offset around and everything gets worse if the phone switches locale settings, from when the date was parsed to when the date is displayed...

Is there a way for me to extract this date from the string as UTC, then save it as an interval instead of an actual (timezone dependent) date. I know that is how dates are always saved internally. But I can't figure out how to do it with the separate GMT string and taking into account the users locale.

Cheers

解决方案

Use NSDateFormatter's setTimeZone:. You will also need to store the time zone offsets separately from the NSDate to later display the times in these time zones again.

这篇关于NSDate dateFromString,如何解析'围绕'UTC,GMT和用户区域设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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