开发者

Storing date and time as epoch vs native datetime format in the database

For most of my tasks I find it much easier to work with date and time in th开发者_如何学JAVAe epoch format:

  • it's trivial to calculate timespan
  • or determine if some event happened before or after another,
  • I don't have to deal with time-zone issues if the data comes from different geographical sources,
  • in case of scripting languages what I usually get from database when I request a datetime-typed column is a string that I need to parse in order to work with it.
  • This list can go on, but for me in order to keep my code portable that's enough to ditch database's native datetime format and store date and time as integer. What do you guys think?


    You should be able to solve the time zone issue by using UTC datetimes. I know C has a function for converting epoch times to UTC. I couldn't find one to go the other way, which confuses me.


    I think I'm so use to proper Model objects and methods in MVC these dates and times functionality becomes trivial. I depend on the date & time library to the calculations. There's very few times where I have to write my own and and actually those times I overlooked the fact that the functionality was in the library already. If you are using PHP grab the date and time library from the Zend Framework. Once you have developed your model reuse it. Then you don't care how the date and time is stored in the DB.


    The primary reason for using the database's DATETIME type is that the POSIX time_t has a very limited range; you cant store dates beyond the infamous Y2038, or before something around 1904. Since many databases need to store historical or future dates, there is a full range and usually portable DATETIME; if in fact POSIX epoch times are more useful to you then go ahead and use them.

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜