【问题标题】:What is the best way of getting the CFAbsoluteTime of midnight of today?获得今天午夜 CFAbsoluteTime 的最佳方法是什么?
【发布时间】:2013-08-25 05:33:44
【问题描述】:

我使用CFAbsoluteTimeGetCurrent() 获取当前时间,作为CFAbsoluteTime 结构体。

获取今天午夜的CFAbsoluteTime 的最佳方式是什么?

Ive considered getting midnight using a similar method to the one shown here,然后将结果转换为CFAbsoluteTime,但是做一件简单的事情似乎太复杂了。

有没有更好的办法?

【问题讨论】:

    标签: objective-c time core-foundation


    【解决方案1】:

    计算“当天的午夜”并非微不足道,因为事情如 必须考虑时区和夏令时转换。 我的首选方式是

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *midnight;
    [calendar rangeOfUnit:NSDayCalendarUnit
                startDate:&midnight
                 interval:NULL
                  forDate:[NSDate date]];
    

    然后您可以使用

    进行转换
    CFAbsoluteTime cfMidnight = [midnight timeIntervalSinceReferenceDate];
    

    【讨论】:

    • 举个具体的例子:巴西在午夜进行 DST 转换。这意味着,在那个国家(部分地区),不存在 10 月第三个星期日的午夜(因为时钟可能会跳到 01:00,就像我们在美国大部分地区从 02:00 跳到 03 :00 在我们的 DST 开始日期,所以 02:00 不存在)。 en.wikipedia.org/wiki/Daylight_saving_time_in_Brazil(而且,就像在美国一样,这一切是否会发生取决于您所在的州。)一个更容易回答的问题是“日历中的这一天从什么时候开始?”,以及这就是你的代码所做的。
    猜你喜欢
    • 1970-01-01
    • 2020-09-30
    • 2012-02-20
    • 1970-01-01
    • 2021-09-23
    • 2012-10-19
    • 1970-01-01
    • 2011-04-23
    相关资源
    最近更新 更多