I need to keep datetime in a C++ structure for an iPhone app. The time will be saved and restored into sqlite db. What is the best data type and corresponding API for this? My candidates are:
TimeValuefrom the QuickTime API
My instinct is to go with the good ole' C/C++ types from
<time.h>. Any drawbacks down the road? Any other time type I miss that is a darling of the iPhone SDK?
NSTimeInterval and its CoreFoundation counterpart
CFAbsoluteTime are the best values to use, as they include sub-millisecond accuracy (they're double-precision floating-point values).
struct tm are only really used in certain BSD APIs (and
struct timeval or
struct timespec are more common there).
TimeValue is only used to represent values and intervals within a media file, and is usually based on a configurable time base.
It depends completely upon what you are using the times for.
- What precision do you need?
- How many will be stored? (On average / worst case)
Store as much precision as you need, but no more. (But also think about future needs). You don't save appointment times to the nearest nanosecond - it doesn't make sense. However if you are recording some type of data where very small intervals are important, then you might save it to the precision of the system clock (often 100 nanosecond chunks), or even a bit finer than that to allow for future versions with better clocks.
If you are only storing small numbers (say less than a few thousand) then storage size and access speed are probably unimportant. If you are storing many records, then the size of the records may become important.
Apart from the above considerations, it doesn't really matter; as long as it is clearly documented.
I'm not familiar with the sqlite db. Does it have any native date/time type? If so, that is probably simplest -- as long as it meets your requirements.