C ++ struct member what type to store calendar time on iPhone?
I need to store datetime in a C ++ structure for an iPhone application. The time will be saved and restored to sqlite db. What is the best datatype and corresponding API for this? My candidates:
-
time_t
and structtm
from<ctime>
and<time.h>
-
NSTimeInterval
from<NSDate.h>
-
TimeValue
from QuickTime API
My instinct is to go with good ole 'C / C ++ types from <time.h>
. Any flaws along the way? Any other type of time I've missed is the iPhone SDK darling?
a source to share
NSTimeInterval
and its CoreFoundation equivalent CFAbsoluteTime
are the best values since they include millisecond precision (these are double precision floating point values). time_t
and struct tm
are only used by some BSD APIs (and struct timeval
or struct timespec
more common there). TimeValue
is used only to represent values and intervals in a media file and is usually based on a custom time base.
a source to share
It depends entirely on what you are using the time for.
- How much precision do you need?
- How much will be saved? (Average / Worst Case)
Store as much precision as possible, but no more. (But also think about future needs.) You are not saving the meeting time to the nearest nanosecond - that doesn't make sense. However, if you are recording a particular type of data where very small intervals are important, you can save it on the accuracy of the system clock (often 100 nanosecond chunks), or even slightly better than 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 not important. If you store many records, the size of the records can become important.
Apart from the above considerations, it doesn't really matter; if it is clearly documented.
I am not familiar with sqlite db. Does it have any native date / time type? If so, this is probably the easiest - if it suits your requirements.
a source to share