如何在Core Data中存储没有时间的日期 [英] How to store dates without times in Core Data

查看:128
本文介绍了如何在Core Data中存储没有时间的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的应用程式以csv格式接收资料,日期是但没有时间:

 日期,周期
2009-08-01,123
2009-08- 02,234
2009-08-03,345
2009-08-04,456

存储此数据时,每天只能有一个记录。我想最好的事情是创建一个NSDate来存储,但剥离的时间&时区数据。



我可以使用NSDateComponents或NSDateFormatter轻松创建一个NSDate,无需小时,分钟或秒。但是,即使我将时区显式设置为UTC或从GMT开始为零秒,使用NSLog()始终输出创建的日期也有我的本地时区:

  2009-07-29 00:00:00 +0100 

有没有人知道更好的方法使NSDates没有时间组件?

解决方案

一个好的编程规则是总是以UTC为单位存储日期。不管你是否使用核心数据都没关系;你仍然需要做一些工作,因为苹果的日期类很糟糕。



日期在内部表示为从参考日期起的秒数,我相信,2001年1月1日00:00:00(虽然实际参考日期不是很重要)。点是, NSDate 对象总是本机在UTC。如果您在CSV文件中获取的日期是本地的,则需要执行以下操作以获取UTC时间:

  NSDate * UTCDate = [localDate addTimeInterval: -  [[NSTimeZone localTimeZone] secondsFromGMT]]; 

然后,我将时间设置为00:00:00。现在,您正在以UTC格式保存日期,午夜。出于演示目的,您将使用配置为您选择的时区的 NSDateFormatter (如果您不指定系统时区,则系统时区为默认)显示这些日期



当你只是处理日期时,时区并不重要。只要您确保将 NSDateFormatter 的时区设置为UTC,您将始终显示相同的日期,无论用户在她的时区选择了什么设备。



如果您不喜欢此解决方案,您可以始终以另一种格式存储日期。您可以使用 double int 以某种自定义格式存储日期(例如,日期),或者甚至可以滚动您自己的类,以完全按照您想要的方式建模日期,并将其存储为 NSData 对象。只要类实现 NSCoding ,您可以将其序列化为Core Data中的 NSData 对象。您只需要将Core Data中的属性类型设置为Transformable。



这里有很多选项,没有一个涉及编写自己的SQLite查询和数据库。


I've been trying to find a sensible way of storing daily data using Core Data on the iPhone.

My app receives data in csv format, with a date but no time:

date, cycles
2009-08-01, 123
2009-08-02, 234
2009-08-03, 345
2009-08-04, 456

When this data is stored, there should only be one record per day. I figured the best thing to do was create an NSDate to be stored, but strip out the time & time zone data.

I can easily create an NSDate without hours, minutes or seconds using either NSDateComponents or an NSDateFormatter. However even when I set the time zone explicitly to UTC or to zero seconds from GMT, outputting a created date with NSLog() always has my local timezone like:

2009-07-29 00:00:00 +0100

Does anyone know of a better way to make NSDates without time components? Or perhaps a better way of storing the dates?

解决方案

A good programming rule of thumb is to always store dates in UTC. It doesn't matter whether you use Core Data or not; you'll still have to do some work because Apple's date classes pretty much suck.

Dates are represented internally as a number of seconds since a reference date which is, I believe, 1 January 2001 00:00:00 (although the actual reference date isn't very important). Point is, NSDate objects are always natively in UTC. If the dates you're getting in your CSV file are local, you'll need to do something like this to get the UTC time:

NSDate *UTCDate = [localDate addTimeInterval:-[[NSTimeZone localTimeZone] secondsFromGMT]];

Then, I'd set the time to 00:00:00. Now you're saving the date, at midnight, in UTC. For presentation purposes, you will use an NSDateFormatter configured with the time zone of your choice (the system time zone is the default if you don't specify one) to display those dates.

Time zones don't really matter when you're just dealing with dates, though. As long as you make sure to set the time zone on your NSDateFormatter to UTC, you'll always show the same date, no matter what time zone the user has selected on her device.

If you don't like this solution, you can always store your dates in an alternative format. You could use a double or int to store the date in some custom format (e.g. the number of days since some reference date), or you could even roll your own class to model the date exactly the way you want and store it as an NSData object. As long as the class implements NSCoding, you can serialize it to an NSData object in Core Data. You just need to set the attribute type in Core Data to "Transformable".

You have a ton of options here, and none of them involve the effort of writing your own SQLite queries and databases.

这篇关于如何在Core Data中存储没有时间的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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