Date: | April 15, 2005 / year-entry #94 |
Tags: | code |
Orig Link: | https://blogs.msdn.microsoft.com/oldnewthing/20050415-52/?p=35893 |
Comments: | 6 |
Summary: | We can take our computation of the interval between two moments in time and combine it with the trick we developed for using the powers of mathematics to simplify multi-level comparisons to reduce the amount of work we impose upon the time/date engine. static void PrintAge(DateTime bday, DateTime asof) { // Accumulate years without going... |
We can take our computation of the interval between two moments in time and combine it with the trick we developed for using the powers of mathematics to simplify multi-level comparisons to reduce the amount of work we impose upon the time/date engine. static void PrintAge(DateTime bday, DateTime asof) { // Accumulate years without going over. int years = asof.Year - bday.Year; if (asof.Month*32 + asof.Day < bday.Month*32 + bday.Day) years--; DateTime t = bday.AddYears(years); // Accumulate months without going over. int months = asof.Month - bday.Month; if (asof.Day < bday.Day) months--; months = (months + 12) % 12; t = t.AddMonths(months); // Days are constant-length, woo-hoo! int days = (asof - t).Days; SC.WriteLine("{0} years, {1} months, {2} days", years, months, days); }
Observe that we avoided a call to the |
Comments (6)
Comments are closed. |
Does anything in Windows take account of leap seconds? If I used an embedded version of Windows for my time machine and wanted to go back to 1601, should I pass the time machine the number of nano-seconds in the FILETIME structure for the current time or should I add on a few more for leap seconds? I hope Microsoft isn’t ignoring the market for time machine operating systems.
This has been discussed before. Posix-compatible systems are required to have time-of-day clocks that are already incorrect by around 20 seconds, and Windows systems are compatible with that whether or not their Posix subsystems are being used.
The part I can’t figure out is why, after a Windows system uses NTP to nearly synchronize itself with an atomic clock (after two or three intermediaries), how come it doesn’t adjust itself to 20 seconds in the future. Also what happens to a Posix system if it happens to execute an NTP operation during a leap second.
That’s a neat trick. Took me a few sample cases to see that it really worked, but neat. Could be very useful in environments like SQL Server, where the datediff() methods are way too basic: the year datediff() just subtracts the years, ignoring month and day.
I would, of course, expect a *much* better comment in the source code than the one you provided.
And, err, you didn’t avoid a call to AddYears(). Maybe you meant some other method?
One other question: how does .net handle daylight savings time? I recall having to do brutal hacks to get past the fact that IE6.0 ignored DST, but 6.0a suddenly became the *only* browser to automatically adjust datetimes to reflect DST. (meaning the timeless date arithmetic I was trying to perform lost or gained a day after rounding).
Norman: NTP has a flag that the time server can use to warn that a leap second is coming up. Typical NTP clients on POSIX systems make the clock run a bit slow for a few minutes before and after that time, so the leap second is never visible.
More math?
static void PrintAge(DateTime bday, DateTime asof)
{
int diffSortOf = 32*(asof.Month + 12*asof.Year) + asof.Day
– 32*(bday.Month + 12*bday.Year) – bday.Day;
int years = diffSortOf/32/12;
int months = (diffSortOf % (32*12)) / 32;
DateTime t = bday.AddYears(years).AddMonths(months);
int days = (asof – t).Days;
SC.WriteLine( "{0} years, {1} months, {2} days"
, years, months, days);
}