LarsonsWorld > Whatever Else > It's About Time 


LarsonsWorld | It's About Time

An Interesting Article Written By: Karen Kenworthy
Of Karen's Powertools http://www.karenware.com/

    I've been thinking a lot about time lately. It's a fascinating subject. What is time? Why does it always seem to move forward, never backwards? Does time have a beginning? Or an end?
    Yes, those are fascinating questions. But being a boring computer programmer, I haven't spent much time on them. Instead, I've been wondering how our computers measure the passage of time, and how they record those measurements.
    And unlike the philosophers who tackle the bigger questions, I've actually found answers to some of my questions ...
    It's About Time
    For example, did you know that Windows keeps track of three different dates and times for each file on a disk? Well, OK, that's an easy one. Most of you know that Windows keeps track of the date and time each file is created. It also remembers the last time a file was changed (its "date of last modification"). And it knows when a file was last accessed, or read.
    But, did you know how Windows measures and records these three dates and times? Probably not, since Microsoft has never completely documented how this is done. But they have left a few clues ...
    My search for answers began with Windows' built-in function called GetFileTime. According to Microsoft, this function returns Windows' three times for any file. And indeed it does. When called, this function returns three 64-bit numbers.
    Each of these numbers is what Microsoft calls a FILETIME structure. Now you and I think of a date as three distinct numbers: the year, month, and day. The same is true of a time. To us, a point in time is a combination of an hour, some number of minutes, and a few seconds. For extra precision, we might add another number, perhaps representing tenths, hundredths, or even thousandths of a second.
    Throw in a few spaces and punctuation marks, to keep the various numbers from running into one another, and the result looks something like this:

3/13/2002 5:36:24.675
or perhaps:

2002.3.13 5:36:24.675

    Yes, that's how humans record dates and times. But Windows' FILETIME structure is a single 64-bit binary number. And, believe it or not, this number is a count -- a count of the 100-nanosecond intervals that have passed since midnight, the morning of January 1st, 1601!
    Now, each second lasts one billion nanoseconds. Since Windows' clock ticks once each 100 nanoseconds, Windows is dividing each second into 10,000,000 parts. And when measuring time, it counts how many of these tiny parts have elapsed over more than 400 years!
    For example, consider the human-readable dates shown above. They show how you and I might represent the date March 13th, 2002 at 5:36:24.675 a.m. But Windows records this date like this:

0000 0001 1100 0001 1100 1010 0101 0001 0000 0011 1011 1001 1011 1011 0011 0000

    Now admittedly, all 64-bit numbers are a "bit" scary, and this is one no exception. But even after converting this number to the base 10, or decimal system, that most human prefer, this number is still pretty daunting. Now it looks like this:

126,604,713,846,750,000

How's that for a date and time? It's a good thing Microsoft doesn't make watches. Can you imagine two programmers, trying to get together for lunch? "How about meeting me at 126,604,944,000,000,000?" says the first programmer. "No," comes the reply, "I have a meeting that might run late. Can we make it at 126,604,962,000,000,000 instead?" :)

Valid HTML 4.01!