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

查看:125
本文介绍了iPhone SDK Core Data系统如何将日期类型存储到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];

其中,create_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

之前的九位数字。然后一些分数。

Nine digits before the . and then some fraction.

这种格式是什么?这不是时代,而不是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这样的数字转换成时代吗?谢谢!

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 指定单个原始方法 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 Core Data系统如何将日期类型存储到sqlite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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