NSDate最佳实践创建一个没有时间元素的日期 [英] NSDate best practice in creating a date with no time element

查看:122
本文介绍了NSDate最佳实践创建一个没有时间元素的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个使用核心数据存储我的数据的应用程序。包括在这是一个日期字段,其中我只对日期而不是时间感兴趣。
我需要基于日期(而不是时间)选择记录,所以我在NSDate上创建了一个类别来返回一个日期,标准化为一个设定的时间如下:

I am writing an app which uses core-data to store my data. Included in this is a date field of which I am only interested in the date not the time. I need to select records based on the date (not time) and so I have created a category on NSDate to return a date, normalised to a set time as follows:

+ (NSDate *)dateWithNoTime:(NSDate *)dateTime {
if( dateTime == nil ) {
    dateTime = [NSDate date];
}
NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:dateTime];
NSDate *dateOnly = [[NSCalendar currentCalendar] dateFromComponents:comps];
[dateOnly dateByAddingTimeInterval:(60.0 * 60.0 * 12.0)];           // Push to Middle of day.
return dateOnly;

}

我将数据添加到核心数据存储(我有一个设置器,使用此方法设置原始日期值),然后我使用此方法创建一个日期,我用来比较执行获取请求的日期。所以在理论上,这应该总是工作 - 即挑出我期待的日期。

I then use this when I add data to the core-data store (I have a setter which uses this method to set the primitive date value) and then I use this method to create a date that I use to compare the dates when performing a fetch request. So in theory this should always work - ie pick out the dates I'm looking for.

我有点紧张,因为我不完全确定什么效果改变时区或区域设置。它仍然可以工作吗?

I'm slightly nervous though as I'm not totally sure what effect changing time-zone or locale will have. Will it still work ?

只有当你对时间不感兴趣时​​,在存储和搜索日期时才考虑最佳做法。

What is considered best practice when storing and searching on a date only when you aren't interested in the time.

干杯。

EDIT

讨论推荐我认为我应该修改我的代码如下。想法是,如果我确保我推到一个特定的日历系统和特定的时区(UTC),那么日期应该始终是相同的,无论你在何时设置日期和当你阅读日期。对此新代码的任何意见赞赏。

After reading the discussion recommended I think that I should modify my code to be as follows. The thinking being that if I ensure that I push it to a specific calendar system and a specific timezone (UTC) then the dates should always be the same regardless of where you are when you set the date and when you read the date. Any comments on this new code appreciated.

+ (NSDate *)dateWithNoTime:(NSDate *)dateTime {
if( dateTime == nil ) {
    dateTime = [NSDate date];
}

NSCalendar       *calendar   = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
                         fromDate:dateTime];

NSDate *dateOnly = [calendar dateFromComponents:components];

[dateOnly dateByAddingTimeInterval:(60.0 * 60.0 * 12.0)];           // Push to Middle of day.
return dateOnly;

}

推荐答案

您有几个问题需要处理。首先,正如你所说,时区。您还需要担心夏时制,这改变了中午的概念。

You have a few issues to deal with here. First, as you noted, timezones. You also need to worry about daylight savings, which change the concept of "midday."

请参阅关于CocoaDev的讨论,其中Apple工程师提供一些答案并讨论一些最佳做法。

Take a look at this discussion on CocoaDev where an Apple Engineer gives some answers and discusses some best practices.

这篇关于NSDate最佳实践创建一个没有时间元素的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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