这种方法是否总是在 ios 中为我提供当天的 12:00:00? [英] Is this method going to always give me 12:00:00 of the current day in ios?

查看:45
本文介绍了这种方法是否总是在 ios 中为我提供当天的 12:00:00?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,这类似于 NSDate 开始日期和结束日期天,但该主题并未讨论它是否适用于我在这里问的所有情况.这个问题涉及没有取回正确的值,因为他们忘记了 NSDayCalendarUnit.这个问题是关于值的准确性,事实证明它不会像我怀疑的那样与 DST 一起工作.

Yes, this is similar to NSDate beginning of day and end of day, but that topic does not discuss whether it works for all situations which is what I am asking here. That question dealt with not getting back the correct value because they forgot NSDayCalendarUnit. This question is dealing with the accuracy of the value which as it turns out is not going to work with DST as I had suspected.

在我的应用程序的几个部分中,我需要指定一天中的某个时间而不关心实际的一天,例如,每天下午 4:00 执行此活动.我看过一些帖子讨论如何做到这一点,但希望对我的方法提供反馈.下面的方法旨在返回任何特定日期的午夜,并将与表示特定时间的秒数的偏移量结合使用(例如,1:00:00 am 为 3600).

In several parts of my app, I have the need to specify a time of day without caring about the actual day, e.g., do this activity at 4:00pm every day. I have seen a few posts that talk about how to do this, but would like feedback on my approach. The method below is intended to return midnight of any particular day and will be used in conjunction with an offset that represents the number of seconds to a particular time (for instance, 1:00:00 am as 3600).

我相信它应该可以工作,但很好奇它是否可以处理夏令时等边缘情况.对于任何反馈,我们都表示感谢.谢谢!

I believe it should work, but am curious if it will handle edge cases such as daylight savings time. Any feedback would be appreciated. Thanks!

// We want to return 12:00:00AM of the day represented by the time interval
NSTimeInterval getStartOfDay(NSTimeInterval t)
{
   // first convert the time interval to an NSDate
   NSDate *ourStartingDate = [NSDate dateWithTimeIntervalSinceReferenceDate:t] ;

   // get just the year, month, and day of our date
   unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
   NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
   NSDateComponents *comps = [gregorian components:unitFlags fromDate:ourStartingDate];
   NSDate *ourConvertedDate = [gregorian dateFromComponents:comps] ;

   // put that date into a time interval
   NSTimeInterval convertedInterval = [ourConvertedDate timeIntervalSinceReferenceDate] ;

   // because the time interval is in GMT and we may not be, we need to get an offset
   NSInteger timeZoneOffset = [[NSTimeZone localTimeZone ] secondsFromGMT] ;

   // account for the time zone difference
   convertedInterval = convertedInterval - timeZoneOffset ;

   // still might have a Daylight Savings Time issue?
   return convertedInterval ;
}

推荐答案

这不会处理边缘情况.在巴西,夏令时会消除或复制午夜到凌晨一点.因此,在某些日子里,没有午夜,而在其他日子里,有两个午夜.

This will not handle edge cases. In Brazil, DST either eliminates or duplicates the midnight-to-one-AM hour. So on some days, there is no midnight, and on others there are two midnights.

处理这个问题的正确方法是让你自己的数据类型代表一天中的时间(独立于日期),或者使用 NSDateComponents,只设置时间组件.

The correct way to handle this is to either make your own data type representing a time of day (independent of date), or to use NSDateComponents, setting only the time-of-day components.

如果您想表示一个独立于一天中的时间的日期,最好使用当天的中午而不是使用午夜.或者你可以创建你自己的数据类型,或者使用 NSDateComponents,只存储日/月/年/纪元.

If you want to represent a date, independent of a time-of-day, you're better off using noon of that day than using midnight. Or you can create your own data type, or use NSDateComponents, storing only day/month/year/era.

强烈建议您尽快观看这些 WWDC 视频:

I highly recommend you watch these WWDC videos ASAP:

这篇关于这种方法是否总是在 ios 中为我提供当天的 12:00:00?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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