I need a universal way to store dates and datetimes in several databases (for now Sqlite, MySql and PostgreSql). I need datetimes to have microseconds precision.
So i thought to keep dates as integers (4 bytes) - days since the unix epoch, and datetimes as integers (8 bytes) - microseconds since the unix epoch.
Questions:
time.time()/86400
(python). time() / 86400 is fine. I've done this before; you should not anticipate any problems. You could also store time() truncated down to the nearest multiple of 86400, which might be a little nicer because you can pass it to various functions that already accept a time_t
and have it come out as midnight UTC on the date in question.
Don't worry about leap seconds. Only specialized software takes them into account. The system date&time on all general purpose operating systems, and all general purpose date&time libraries pretend they don't exist. Your NTP server implements them by just fudging the system clock ahead by one second whenever they happen.
C's gettimeofday
returns the time as two separate 4-byte integer values: seconds since the epoch, and microseconds since the start of the second (struct timeval
). Similarily, C's clock_gettime
returns the time as two separate 4-byte integer values: seconds since the epoch, and nanoseconds since the start of the second (struct timespec
). If you care about compatibility with existing representations of time, you might choose one of those two formats instead of an 8-byte integer counting microseconds since the epoch. On the other hand both of those have the y2.038k bug (unless the first integer is extended to 8 bytes, for a total of 12 bytes) and they are less convenient to use in a database. So I think your idea of microseconds since the epoch is just fine.