views:

4249

answers:

3

I have a sqlite3 table that I'm trying to map to an object in objective-C. One attribute of the table is 'completed_at' which is stored as a DATETIME.

I want to create a property on my objective-C class (which inherits from NSObject) that will map well to the 'completed_at' attribute.

Objective-C has an NSDate type but I'm not sure if that will map directly?

+3  A: 

I have zero experience with Objective-C, but I found Apple's NSDate Class Reference with a google search. With the information provided on the linked page you should be able to figure out how to manipulate 32-bit epoch times in Objective-C, and this would work well in SQLite. I would probably create the completed_at column as type INTEGER for 32-bit times.

SQLite really prefers Julian dates, which are floats. I haven't found any documentation explaining how one might coerce the NSDate class into working with Julians.

timeIntervalSince1970 looks very interesting.

converter42
timeIntervalSince1970 works for me
amrox
Indeed, this is the correct mapping but be aware that SQLite does not impose a restriction on the types that it will hold but uses types merely as suggestions.
wisequark
+2  A: 

This came up a couple of weeks ago:

Persisting Dates to SQLite3 in an iPhone Application

Stephen Darlington
A: 

The formatter is important if you are trying to effect the presentation but if you use if for internal storage, you are defining a string which can defeat the DB-engine's ability to use the value for computation, comparison, sorting, etc. Also, if you are going to have different clients inserting the date value into the DB you would have to write conversion functions everywhere. I used the following and it worked as expected (schema's column defined as DATETIME):

dateExpires = [NSDate dateWithTimeIntervalSinceNow: sqlite3_column_double(queryStmt, 5)];

I inserted into the SQLITE3 db with the Firefox add-on as "4/12/2010" here in Central time zone. Viewing the value of 'dateExpires' in XCode-debugger displayed as:

2010-04-12 23:19:48 -0500

Sure enough, that is the correct time.

Also, to insert into the SQLITE DB you will put the value [NSDate date]

mobibob