NSDate:处理一天中时间的正确方法?
我正在制定指定一天中不同时间的时间表,例如上午 10:30。不过,我不知道具体日期。我将把它们作为值存储在 NSDictionary
中,并希望以简单的方式处理它们。
我无法使用 NSDate
,因为我没有日期。至少,不是以一种直接的方式。
另一种看起来很明显的方法是 NSTimeInterval,但这看起来可能是非常微妙的错误的来源。我特别想到夏令时(出于某种原因,这周我一直在想夏令时!)。
除此之外,唯一真正浮现在脑海中的是将其保存在格式化字符串中或将其编码为 NSNumber
(例如 小时 * 60 + 分钟
)。当然,这两种方法都可以,但看起来我正在发明一个方轮,而我确信某处已经有一个圆轮了。
使用可可处理原始时间的最不违背谷物的方式是什么?
I am working with a schedule that specifies times of day, such as 10:30 AM. I do not know the dates, however. I'm going to store these as values in a NSDictionary
and would like to deal with them in a straightforward way.
I can't use NSDate
, since I don't have a date. At least, not in a straightforward way.
The other way that seems obvious is NSTimeInterval
, but that looks like it's probably a source of very subtle errors. I'm thinking in particular of daylight savings time (which is on my mind this week for some reason!).
Other than that, the only things that really spring to mind are keeping it in a formatted string or encoding it in a NSNumber
(like hours * 60 + minutes
). Both of which would work out fine, of course, but seem like I'm inventing a square wheel where I'm sure there's already a round one somewhere.
What's the least against the grain way of dealing with raw times using Cocoa?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简而言之,没有用于存储时间的内置机制,因此只需使用对您来说最方便的方式即可。我过去使用过自己的编码和解析代码(例如“22:00”),因为它们很容易阅读和调试,但是按照您的建议存储午夜过后的秒数或分钟数没有任何问题。请记住,您必须自己进行数学计算。
无论你如何做,你都需要单独的年、月、日、小时、分钟和秒值,以便你可以使用 NSCalendar 的
-dateFromComponents:
方法从 NSDateComponents 构造 NSDate。正如其他人所说,您不能通过向现有的 NSDate 添加小时和分钟来设置时间,因为如果您跨越 DST 边界,您将无法获得您期望的值。 (但是,我假设您仍然可以添加日期和月份组件,而不必担心 DST)
The short answer is that there's no built-in mechanism for storing time-of-day, so just use whatever is most convenient to you. I've used strings in the past using my own encoding and parsing code, (e.g., "22:00") because they're easy to read and debug, but there's nothing wrong with storing seconds or minutes past midnight as you suggest. Just remember that you'll have to do the math yourself.
How ever you do it, you will need separate year, month, day, hour, minute, and second values so that you can construct an NSDate from NSDateComponents, using NSCalendar's
-dateFromComponents:
method.And as others have said, you cannot set the time-of-day by adding hours and minutes to an existing
NSDate
because if you cross a DST boundary you won't get the value you expect. (However, I assume you can still add day and month components without worrying about DST)所以,我想没有简单的内置方法可以做到这一点。看起来让 Cocoa 在 DST 边界上构建我期望的时间的唯一方法是使用字符串。
所以对于后代来说,看起来我会使用这样的东西。
测试工具:
实用程序标头:
实用程序实现:
为什么要为此使用单独的单元?好吧,我已经编写了足够多的日期格式化代码,知道动态构建
NSCalendar
和NSDateFormatter
会彻底降低性能。So, I guess there's no simple inbuilt way of doing this. It also looks like the only way to get Cocoa to build the time I expect on DST boundaries is with strings.
So for posterity, it looks like I'll be using something like this.
Test harness:
Utility header:
Utility implementation:
Why bother with a separate unit for this? Well, I've written enough date formatting code to know that constructing
NSCalendar
andNSDateFormatter
on the fly utterly kills performance.您可以使用
NSDateComponents
仅存储您需要的组件(例如小时和分钟),然后使用NSCalendar
<当您需要使用序列化组件和基准日期时,code>dateByAddingComponents:toDate:options: 创建绝对日期引用。You can use
NSDateComponents
to store only the components you need (hour and minutes for example), and then useNSCalendar
dateByAddingComponents:toDate:options:
to create an absolute date reference, when you need using the serialized components and a base date.