iPhone - 考虑夏令时的当前日期 [英] iPhone - current date considering daylight saving

查看:96
本文介绍了iPhone - 考虑夏令时的当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的国家/地区位于GMT + 0。我们现在是+1小时,因为我们正在夏令时。

My country is located in GMT+0. We are +1 hour now, because we are in daylight saving.

当我这样做时

[NSDate date];

应该返回当前日期和时间,但是当我这样做时,我会收到GMT时间不考虑夏令时。

it was supposed to return the current date and time, but when I do that, I receive the GMT time not considering the daylight saving.

这是文档对日期命令的说法

This is what the docs say about the date command

创建并返回一个新的日期集到目前的日期和时间。

为什么Apple会这样做?为什么一切都这么复杂?这是一个错误吗?有没有办法获得设备当前的实时时间?同一时间显示在设备的时钟上?

Why Apple does that to us? Why everything is so complex? Is this a bug? Is there a way to get the current real time the device is on? the same time that is displayed on the device's clock?

我的应用程序取决于日期和时间,并且位于世界各地不同时区的用户的日期错误在夏季或冬季将是一场灾难。

My app depends on dates and times and having a wrong date for an user located in a different timezone around the world that is on summertime or wintertime will be a disaster.

谢谢。

编辑

经过多次回答后,我尝试了以下代码:

After several answers, I have tried this code:

NSDate *wrongToday = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];

[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *currentTime = [dateFormatter wrongToday];

NSDate *today = [dateFormatter dateFromString:currentTime];

猜怎么样,今天=错误今天...
换句话说,没有变化,我没有夏令时,继续错误的日期。
更令人惊奇的是,currentTime在NSString中显示夏令时的日期...

guess what, today = wrongToday... in other words, no change, I continue to have the wrong date without daylight saving. what is more amazing is that currentTime shows in NSString the date with daylight saving...

任何线索?再次感谢。

推荐答案

网站上有几个很好的答案这里 here 此处 NSDate 是自(UTC)参考日期以来的间隔,但您可以使用 NSTimeZone ,详见这些答案。

There are a couple of good answers on the site already here and here and here. An NSDate is an interval since the (UTC) reference date, but you can use NSTimeZone as detailed in those answers.

这篇关于iPhone - 考虑夏令时的当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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