July 15, 2003
By Karen Kenworthy
IN THIS ISSUE
I received some sad news yesterday. Many years ago, my father lived a few miles from where the secluded Power Tools workshop now stands. The creek where he, his older brother, and younger sister, swam, still entertains children on long, hot summer afternoons. The bridge he crossed, reluctantly walking to grade school, still spans that creek. The fields, where he ran and played, are still dappled by patches of red earth and green grass,
But the one-room house, with its tar-paper roof, where Grandmother prepared their meals and mended their clothes, where Grandfather relaxed after a hard day's work, where the three children read their schoolbooks, played their games, dreamed their dreams -- that house, is no more.
<sigh> On the other hand, the hospital where I was born is now a veterinary hospital. And the first home I knew has reverted to the two-car garage from whence it came. I guess there are fates worse than losing a confrontation with a bulldozer. :)
It's About Time
Humans measure the passage of time in an amazing variety of ways. Workdays are measured in hours, vacations in weeks. Lifetimes are measured in years. Other important durations are measured in minutes, months, microseconds, seconds, centuries, and more.
We've also invented lots of ways to read and record time. Some folks are partial to pocket-sized round objects, with a pair of slowly moving hands. Others prefer four illuminated digits strapped to their wrists. Other time-telling devices are as tall as a human being, and a few rise even higher, sitting atop tall towers. For every unit of time, from femtoseconds (.000000000000001 seconds) to centuries, there's a device somewhere that measures and reports it!
Computers, however, are far less diverse. And, whenever possible, they shun human ways. As a result, their handling of dates and times seems a bit odd at first. It's also elegant, convenient (for them), and, not surprisingly, logical.
A computer's sense of time comes from special circuits called "oscillators." As the name suggests, these circuits produce a signal that oscillates, or varies, over time.
For example, the output of one common oscillator begins at zero (volts), rapidly increases to five, then returns to zero. Another popular oscillator produces a signal that rises to 60 (volts), drops to minus 60, then returns to its starting point of zero.
Whatever the distance they travel, these electrical round trips are called "cycles".
Some oscillators complete only a few cycles each second. For example, the generator (an especially hefty form of oscillator) that provides electrical power to your house or town probably completes only 50 or 60 cycles each second. But the oscillator that produces the signal of your favorite radio station completes hundreds of thousands, or even millions, of cycles per second.
Speaking of radio, a brilliant German physicist named Heinrich Hertz pioneered in the use of oscillators to produce long-range radio signals. In his honor, the frequency of an oscillator -- the number of cycles it completes each second -- is measured in "Hertz".
An oscillator that completes one cycle every second has a frequency of one Hertz. One million cycles per second earns an oscillator a speed rating of one million Hertz, or one Megahertz (1 MHz). And, as you've probably guessed, oscillators that complete one billion cycles in a single second run at the once blindingly fast rate of one Gigahertz (1 GHz).
Each of the computers on our desks, laps, or floors has dozens of oscillators. Many of these regulate our computers, providing them a cadence that other circuits must follow.
You can think of these oscillators as very precise symphony conductors. Each down stroke of their baton moves their orchestra one note closer to the end of the performance. Thanks to this maestro, the musicians perform as one, playing their appointed notes in perfect synchrony.
You've no doubt heard of "2 GHz" computers, or "400 MHz" memory. These refer to the speed of the conductor, err oscillator, orchestrating their performance. A "2 GHz" computer has an oscillator that orders its CPU to move briskly, completing 2 billion steps each second. Memory rated at "400 MHz" can complete 400 million transfers of computer data every second, thanks to the oscillator controlling the memory's circuits.
Each computer has also has one special oscillator. It doesn't give orders, or control other circuits. It simply oscillates, quietly completing millions of cycles per second in a lonely corner of our PC.
This oscillator is our computer's clock. It allows the computer to determine the time of day, and know the current date. It also lets the computer measure the passage of time, accurate to as little as a millionth of a second.
To perform all these feats, the computer simply counts the number of cycles the oscillator has completed. The count starts at zero each time the computer is turned on. And it continues to increase by one, after each of the oscillator's cycles, for as long as the computer runs.
It's easy to see how this oscillator measures the passage of time. For example, suppose the oscillator has completed 100 million cycles when a program starts. When the program ends, the oscillator's count has increased to 500 million. How long did the program run?
Well, it ran for 400 million "ticks" -- programmer-speak for completed cycles of the computer's oscillator clock. Converting ticks into seconds is easy, once you know the duration of each tick. And fortunately, the computer, through Windows, is eager to share this information.
But how does a computer know the current time and date? For that job it needs one additional bit of information. The computer must know exactly when it was turned on -- the exact date and time electrons began flowing through its copper veins and arteries.
This information initially comes from a tiny, battery-operated clock and calendar circuit built into your computer. Later, your computer may correct this information by consulting a network time server. These computers, located on your local network or the Internet, are connected to very precise clocks. Many of these clocks are so-called "atomic clocks", accurate to within a few trillionths of a second.
Once both parts of the puzzle are at hand, the solution is easy. Combining the date and time the computer started, with the number of ticks of the computer's oscillator clock, yields the current time of day. It's simple arithmetic.
Well, simple for a computer. :)
Counting ticks since a computer was turned on gives computers a simple way to measure intervals of time, and determine the current date and time. But what happens if two computers need to share a date or time? Imagine your computer telling one of its electronic colleagues: "Here's a copy of a file. It was modified when my clock's tick count was 1,398,773,231."
The second computer would roll its eyes, if it had any. Very likely the two computers began counting ticks at different times. And the computers' clocks may not even tick at the same rate. In short, one computer's tick count means nothing to another.
So how do computers exchange information about dates and times? Sometimes they resort to human methods, converting tick counts into old-fashioned years, months, days, hours, minutes and seconds, before sending them across the wires. The receiving computer reverses the process, converting human time formats into a more computer-friendly tick count.
But human date and time formats vary widely. Some folks use 24-hour clocks, while others relive the hours 1 through 12 twice each day. Some of us print the year first, followed by the month and then the day. Others begin with the month, followed by the day and year. Some folks use the Gregorian calendar, with its twelve months and a current year of 2003. But others use different calendars, producing different dates for the same moment in time.
Human date and time formats are also unwieldy. Try computing the number of days between June 10th, 1952 and July 15th, 2003. Or calculate the number of seconds that separate 3:43:18 a.m., and 12:16:33 p.m. the next afternoon. It's enough to make you reach for your pocket calculator. Or a computer.
That's why computers like tick counts so much. Arithmetic is easy. Just subtract the earlier time from the later time. The result is the interval, in tick counts, between the two. Tick counts also solve the problem of differing date and time formats we humans seem to enjoy. Dates and times are just pure numbers, with no funny "parts" to shift around.
To communicate easily and naturally, computers need a standard: a single agreed-upon starting time, and a universal tick rate. Then all moments could be expressed unambiguously as a single number: the number of standard ticks since (or until) the standard start time.
There's an old joke about standards. "If one is good, two are better. And three are better yet." Sadly, that sums up computer time standards. Over the years, programmers have selected several starting times, or "epochs". And there are several "standard" tick rates.
MS-DOS used midnight, January 1st, 1980 as its epoch. Contrary to rumor, this is not Bill Gates' birthday. It's simply the nearest "round" date that falls before the 1981 advent of the first IBM PC and the MS-DOS operating system. Ticks occurred only once every two seconds, apparently on the theory that not much could happen in just two seconds on an early PC.
Most programs written in the C and C++ programming languages use an epoch of midnight, January 1, 1970. This date falls just before the dawn of the UNIX operating system, where those two programming languages were born. Each of these ticks takes a full second.
Windows itself is largely written in the C and C++. Those languages are also used for most large "commercial" programs, running on Windows, the Apple Macintosh, Linux, and other computer operating systems. As a result, almost anyone who uses a computer relies on a C or C++ program to manage some dates and times.
Time Trivia: Most C and many C++ programs use 32-bits of memory to record a time's tick count. This means the largest possible tick count they can store is 2,147,483,647. This corresponds to a human time of 3:14:07 a.m., Tuesday, January 19, 2038. At that moment, these tick counters will overflow. One second later, these programs, and the people who use them, will be instantly transported back to 1902. :)
More Time Trivia: One billion seconds after the C/C++/UNIX epoch corresponds to 1:46:40 a.m., September 9, 2001. This moment is known as the "UNIX Billennium". Who says computer programmers don't have a sense of humor? :)
Visual Basic programs use midnight, the morning of Saturday, December 30, 1899 as their epoch. If that seems like an odd choice, wait until you hear the tick rate used by these programs: Visual Basic's ticks occur only once per day!
Fortunately, Visual Basic doesn't require tick counts to be whole numbers, or integers. Instead, a Visual Basic tick count can, and often does, consist of a whole number of days, plus some fraction of a day. For example, July 15th, 2003 at 8:30 a.m. becomes the number 37817.3541666667. That's because it falls 37817.3541666667 days after the Visual Basic epoch.
Even More Time Trivia: Visual Basic uses eight bytes (64 bits) to store its tick counts. This permits VB programs to work with dates and times falling far into the distant future or past. How far? How about years that are 308 digits long, AD or BC!
Yet More Time Trivia: Visual Basic's way of storing dates and times works so well, it's been adopted for all future versions of Windows. Most Windows programming languages are expected to adopt this standard too. Once this method becomes common-place, problems with date and time ranges should disappear, at least during our lifetimes. :)
Wow! Look at the time. It's almost 37817.1597453704! Time for me to rest my head.
But let's get together again real soon. And in the meantime, check out the new versions of my 'Net Monitor (more about this later!) and Countdown Timer II programs:
As always, both programs are free (for personal use). Time-challenged programmer-types can also download the programs' free Visual Basic source code!
Or if you prefer, get the latest version of every Power Tool, including the new 'Net Monitor and Countdown Timer II, on CD. The disc also includes three bonus Power Tools, not available anywhere else. You'll find every back issue of my newsletter, and a few articles, in the CD's library. The CD even includes a special license that lets you use your Power Tools at work.
Best of all, buying a CD is the easiest way to support the web site and this newsletter. To find out more about the CD, visit:
Until we meet again, don't forget that time waits for no one. If there's something you've been meaning to do, do it now. And if you see me visiting an old homestead, or on the 'net, be sure to wave and say "Hi!"
Power Tools Newsletter
- Directory Printer v5.4.4 Adds Unicode Characters Support
- Replicator v3.7.6 Eradicates Bug that caused Error 3
31515 Verified Subscribers
Subscribe to receive new issues of the newsletter about Karen and her free Power Tools.Click here to Subscribe