iPhone SDK核心数据系统如何将日期类型存储到sqlite? [英] How does the iPhone SDK Core Data system store date types to sqlite?

查看:122
本文介绍了iPhone SDK核心数据系统如何将日期类型存储到sqlite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用核心数据来执行此操作:

I used core data to do this:

NSManagedObjectContext *m = [self managedObjectContext];
Foo *f = (Foo *)[NSEntityDescription insertNewObjectForEntityForName:@"Foo" 
                                                        inManagedObjectContext:m];
f.created_at = [NSDate date];
[m insertObject:f];

NSError *error;
[m save:&error];

其中created_at字段在xcdatamodel中定义为类型Date。

Where the created_at field is defined as type "Date" in the xcdatamodel.

当我从它创建的sqlite数据库导出sql时,created_at被定义为类型timestamp,其值如下所示:

When I export the sql from the sqlite database it created, created_at is defined as type "timestamp" and the values look like:

290902422.72624

290902422.72624

之前的九个数字。

这是什么格式?这不是时代的时间,它不是julianday格式。

What is this format? It's not epoch time and it's not julianday format.

时代将是:

1269280338.81213

1269280338.81213

julianday将是:

julianday would be:

2455278.236746875(通知之前只有7位数字,而不是9位数字)

2455278.236746875 (notice only 7 digits before the . not 9 like I have)

我可以把一个像290902422.72624的数字转换为epoch时间吗?感谢!

How can I convert a number like 290902422.72624 to epoch time? Thanks!

推荐答案

首先,请注意Core Data文档说你不应该触摸SQL或它自己生成的值 - 这样做有可能使你的模型无效,如果你对它进行更改,并且很难解析在第一位。

First, note that the Core Data documentation says you should never touch the SQL or values it generates on your own - doing so has the potential to invalidate your model if you make changes to it, and it's difficult to parse in the first place.

也就是说,你可能看到日期相对于2001年1月1日在GMT。 NSDate 的文档a>指定单个基本方法 timeIntervalSinceReferenceDate 使用该时间作为参考。核心数据反过来使用 NSDateAttributeType 来存储日期类型,它被定义为一个NSDate对象。

That said, what you may be seeing is dates relative to January 1, 2001 in GMT. The documentation for NSDate specifies that the single primitive method, timeIntervalSinceReferenceDate, uses that time for its reference. Core Data, in turn, uses the NSDateAttributeType to store date types, which is defined to be an NSDate object.

通过计算器运行您的价值:

Running your value through a calculator produces:

290902422.72624 / 60 / 60 / 24 / 365.25 = 9.21814...

这是关于自该参考日期以来经过的年数。

which is about the number of years that's elapsed since that reference date.

如果您真的需要将该值解析为时代时间,您可以使用方法 initWithTimeIntervalSinceReferenceDate:与您的SQLite存储的数字获得一个NSDate,然后调用 timeIntervalSince1970 epoch秒回(在NSTimeInterval结构中)。

If you really need to parse that value back into an epoch time you can use the method initWithTimeIntervalSinceReferenceDate: with your SQLite-stored number to get an NSDate, then call timeIntervalSince1970 to get epoch seconds back (in an NSTimeInterval struct).

这篇关于iPhone SDK核心数据系统如何将日期类型存储到sqlite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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