|
Question : For Experts: Internal representation of pre-20th century dates... what do you think of this one?
|
|
Hello all,
This one is new to me.
I remember having a problem with expressions like CDate(-0.5), but I never explored the matter properly. So here is a table with dates, sorted ascending, with the numerical representation in the second column.
TheDate (sorted) Value ---------------------------------------- 27.12.1899 00:00:00 -3.00 28.12.1899 18:00:00 -2.75 28.12.1899 12:00:00 -2.50 28.12.1899 06:00:00 -2.25 28.12.1899 00:00:00 -2.00 29.12.1899 18:00:00 -1.75 29.12.1899 12:00:00 -1.50 29.12.1899 06:00:00 -1.25 29.12.1899 00:00:00 -1.00 30.12.1899 18:00:00 -0.75 30.12.1899 12:00:00 -0.50 30.12.1899 06:00:00 -0.25 30.12.1899 00:00:00 0.00 30.12.1899 06:00:00 0.25 30.12.1899 12:00:00 0.50 30.12.1899 18:00:00 0.75 31.12.1899 00:00:00 1.00 31.12.1899 06:00:00 1.25 31.12.1899 12:00:00 1.50 31.12.1899 18:00:00 1.75 01.01.1900 00:00:00 2.00 02.01.1900 00:00:00 3.00
Notice anything strange?
Apparently, pre-20th century date/time fields do not sort property, and, perhaps worse, there are two distinct representations for the same date/time on day zero!
? CDate(0.5) = CDate(-0.5) False ? Format(0.5, "ddddd ttttt") = Format(-0.5, "ddddd ttttt") True ? DateAdd("h", 0, -0.5) = DateAdd("h", 0, 0.5) True
Wow!
Let's hope that DateAdd and DateDiff work despite this. I guess Access never uses the range ]-1;0[ because:
? DateDiff("h", -1.25, 0.25) 24 ? DateDiff("h", -1.25, -0.25) 23 ? DateDiff("h", -0.25, 0.25) 1
Ouch!
Of course I know the story behind the choice of day 1 for MS dates. The Excel team liked the Sunday 1st of January 1900 as Day 1, forgetting that 1900 isn't a leap year after all (the 29th of February is still a valid date in Excel!). So naturally Day 1 is formatted in Access as Sunday 31st of December 1899, which is correct.
However, the interpretation of the decimal part as time is just plain @$#§!
Any thoughts, comments or reactions? For serious M$ flames, I should perhaps open a companion Lounge question, but please keep it civil here...
Markus (°v°)
|
|
Answer : For Experts: Internal representation of pre-20th century dates... what do you think of this one?
|
|
This is one of my favorites.
Your findings are correct, and the mess - in addition to the wonderful story behind date value zero - is caused by the fact that the construction of date-time values in VB(A) with and integer and a decimal part leaves "a hole" in a way that can be compared to the missing year 0 in hour calender (remember: the year before year 1 is year -1).
Here are two solutions to the sorting issue:
As you have demonstrated, old dates - those with negative value, prior to 1899-12-30 - do not sort correctly if they contain a time part. You may never need it but if you do, the usual and simple method is to use Format:
SortDate = Format([DateField], "yyyymmddhhnnss")
and sort on that, but it is slow. Another and faster method is this:
SELECT * FROM tblDates ORDER BY Fix([DateField]), Abs([DateField]);
Fix takes care of keeping the dates in correct sequence (Int can not be used!) while Abs for a given date allows to sort the timepart correctly.
/gustav
|
|
|
|