I have a MySQL DB and I need to be able to store dates earlier then 1970 so I need a custom way to store dates. In my case, I need to be able to store dates up to the present and as far back as history goes. Obviously earlier events in history require less accuracy – days, months, even years become less important, as we do not have this historical information. The main query involving dates will be to select records between two given dates.
I have thought to use this format:
Year - int(6) | Month - tinyint(2) | day - tinyint (2) | time - time | AD tinyint (1) | mya - int (11)
But when it comes to actually using data in this format it becomes difficult. For example, if I want to get all records between two dates it would be like (pseudocode not SQL):
get all where
year between minYear and maxYear
if year == minYear, month >= minMonth
if year == maxYear, month <= maxMonth
if month == minMonth, day >= minDay
if month == maxMonth, day <= maxDay
if day == minDay, time >= minTime
if day == maxDay, time <= maxTime
or something, which seems like a right pain. I could store seconds before/after 0 AD, but that would take up way too much data! 2011 = 6.4 billion seconds since 0 AD. Does anybody have any ideas for this problem?
You need at least one date to create an interval. I use one date field for start_date and an interval which can be a number of seconds or a fixed interval like ENUM(day,month,year). This depends on the data that you are working with.
For DATE the supported range is ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’, but this means that although earlier values might work, there is no guarantee. I see you can use 0001-01-01 with no problems.
FOR BC values.
I dont know if there is a standard but I’ve seen systems that state their own starting point. Lets say your data will be pre AD but not longer that 1000 BC. You can state your 0 year as -2000, you apply (year- 2000) difference for all dates and maybe even build a wrapper Date class which applies only to your project. You can assign the time as an integer an do all operations with it with no problem, especially if you dont work with days or months granularities. Current calendars are not reliable to work in the past as the accuracy of date was wrongfully calculated for centuries, and repaired by some hotfixes that can do calculations pretty difficult.
I would recommend you to keep the date format as it will allow you to do fast DATE operation directly in mySql.
Update for calculating the leap years in your class: