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

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

问题描述

我一直在尝试寻找一种合理的方式来使用 iPhone 上的 Core Data 存储日常数据.

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

我的应用以 csv 格式接收数据,有日期但没有时间:

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

存储此数据时,每天应该只有一条记录.我想最好的办法是创建一个 NSDate 来存储,但去掉时间 &时区数据.

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.

我可以使用 NSDateComponents 或 NSDateFormatter 在没有小时、分钟或秒的情况下轻松创建 NSDate.但是,即使我将时区显式设置为 UTC 或从 GMT 设置为零秒,使用 NSLog() 输出创建的日期 always 具有我的本地时区,例如:

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

有谁知道制作没有时间组件的 NSDates 的更好方法吗?或者也许是存储日期的更好方法?

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

推荐答案

一个好的编程经验法则是始终以 UTC 格式存储日期.是否使用 Core Data 并不重要;你仍然需要做一些工作,因为 Apple 的日期课程非常糟糕.

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.

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

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]];

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

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.

不过,当您只处理日期时,时区并不重要.只要您确保将 NSDateFormatter 上的时区设置为 UTC,您将始终显示相同的日期,无论用户在其设备上选择了哪个时区.

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.

如果您不喜欢这个解决方案,您可以随时以其他格式存储您的日期.您可以使用 doubleint 以某种自定义格式(例如,自某个参考日期以来的天数)存储日期,或者您甚至可以将自己的类滚动到完全按照您想要的方式对日期建模,并将其存储为 NSData 对象.只要该类实现了NSCoding,就可以将其序列化为Core Data 中的NSData 对象.你只需要在 Core Data 中将属性类型设置为Transformable"即可.

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".

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

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

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

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