Persisting Dates to SQLite3 in an iPhone Application

后端 未结 5 1767
忘掉有多难
忘掉有多难 2020-12-05 03:31

I am developing an iPhone application that persists data to a SQLite3 database.

For each row I persist I wish to include a \'created date\' and a \'last modified da

相关标签:
5条回答
  • 2020-12-05 03:42

    How get a datetime column in SQLite with Objective C

    0 讨论(0)
  • 2020-12-05 03:43

    I convert to/from ISO8601 strings: http://en.wikipedia.org/wiki/ISO_8601

    Far more readible/hackable than time intervals.

    0 讨论(0)
  • 2020-12-05 03:45

    I typically use a double, something like:

    sqlite3_bind_double(statement, index, [dateObject timeIntervalSince1970]);
    

    where dateObject is an NSDate*. Then, when getting the data out of the DB, use

    [NSDate dateWithTimeIntervalSince1970:doubleValueFromDatabase];
    
    0 讨论(0)
  • 2020-12-05 03:52

    Store as a typical C-time (e.g. the time_t/int value returned by time()) value in an UNSIGNED INT column. Convert like drewh said with NSDate:

    - timeIntervalSince1970
    - dateWithTimeIntervalSince1970:(double)value
    

    Except store as an integer, unless you need sub-second granularity (which, generally, for created-on/last-modified, is overkill). Unsigned ints are a lot smaller and easier to process than doubles. While this may not matter in most desktop and web applications anymore, it certainly helps on the iPhone. Every little bit helps.

    One side benefit of C-times that most people don't realize is that they're timezone-agnostic. If you ever need to support multiple timezones, this comes in handy.

    0 讨论(0)
  • 2020-12-05 03:55

    According to the docs there's no DATETIME data type! I use a real data type and convert an NSDate to a real using the timeIntervalSinceReferenceDate method.

    0 讨论(0)
提交回复
热议问题