iOS创建日期,忽略夏令时 [英] iOS create date in the future ignoring daylight savings

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

问题描述

我正在尝试使用日期并在将来创建日期,但夏令时不断妨碍我的工作时间。

I'm trying to work with dates and create dates in the future, but daylight savings keeps getting in the way and messing up my times.

这是我的代码将移动到下个月第一天午夜的日期:

Here is my code to move to midnight of the first day of the next month for a date:

+ (NSDate *)firstDayOfNextMonthForDate:(NSDate*)date
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    calendar.timeZone = [NSTimeZone systemTimeZone];
    calendar.locale = [NSLocale currentLocale];

    NSDate *currentDate = [NSDate dateByAddingMonths:1 toDate:date];
    NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                                    fromDate:currentDate];

    [components setDay:1];
    [components setHour:0];
    [components setMinute:0];
    [components setSecond:0];

    return [calendar dateFromComponents:components];
}

+ (NSDate *) dateByAddingMonths: (NSInteger) monthsToAdd toDate:(NSDate*)date
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    calendar.timeZone = [NSTimeZone systemTimeZone];
    calendar.locale = [NSLocale currentLocale];

    NSDateComponents * months = [[NSDateComponents alloc] init];
    [months setMonth: monthsToAdd];

    return [calendar dateByAddingComponents: months toDate: date options: 0];
}

这给出了我在日期迭代运行方法的日期:

Which give the dates when I run the method iteratively on a date:

2013-02-01 00:00:00 +0000
2013-03-01 00:00:00 +0000
2013-03-31 23:00:00 +0000 should be 2013-04-01 00:00:00 +0000
2013-04-30 23:00:00 +0000 should be 2013-05-01 00:00:00 +0000

我最初的想法是不使用 systemTimeZone 但这似乎没有什么区别。关于如何使时间保持不变而不考虑夏令时变化的任何想法?

My initial thought was to not use systemTimeZone but that didn't seem to make a difference. Any ideas for how I can make the time constant and not take into account the change in daylight savings?

推荐答案

对于给定的日历日期/时间,作为一般规则,不可能预测表示的实际时间(自纪元以来的秒数)。时区发生变化,DST规则发生变化。这是生活中的事实。夏令时在澳大利亚历史悠久。 DST规则在以色列非常难以预测。 DST规则最近在美国发生了变化,给微软带来了巨大的麻烦,因为微软存储了秒而不是日历日期。

For a given calendar date/time, it is not possible as a general rule to predict what actual time (seconds since the epoch) that represents. Time zones change and DST rules change. It's a fact of life. DST has a tortured history in Australia. DST rules have been very unpredictable in Israel. DST rules recently changed in the US causing huge headaches for Microsoft who was storing seconds rather than calendar dates.

永不保存 NSDate 当你的意思是 NSDateComponents 。如果您的意思是2013年5月1日在伦敦,那么请在您的数据库中保存2013年5月1日在伦敦。然后计算一个 NSDate ,尽可能接近实际事件。如果您关心日历事物(如月份),请使用 NSDateComponents 完成所有日历数学运算。如果您真的只关心秒数,那么只需要 NSDate 数学。

Never save NSDate when you mean NSDateComponents. If you mean "the first of May 2013 in London," then save "the first of May 2013 in London" in your database. Then calculate an NSDate off of that as close to the actual event as possible. Do all your calendar math using NSDateComponents if you care about calendar things (like months). Only do NSDate math if you really only care about seconds.

编辑:有很多非常有用的背景,请参阅日期和时间编程指南

For lots of very useful background, see the Date and Time Programming Guide.

还有一个关于日历组件的旁注:当我说2013年5月1日在伦敦时,这并不意味着5月1日午夜。不要添加你实际上并不意味着的日历组件。

And one more side note about calendar components: when I say "the first of May 2013 in London," that does not mean "midnight on the first of May." Don't go adding calendar components you don't actually mean.

这篇关于iOS创建日期,忽略夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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