A UTC TDateTime function

By Mark G. Wiseman

If you read part 1 of my series of articles Hidden treasures of Sysutils, you know that both the TDateTime class and the Sysutils namespace in the VCL contain a rich selection of time and date functions.

However, there is one function I recently needed that was not in the VCL. I needed a function that returned a TDateTime object with the current system time. In the 32-bit Windows operating systems, the system time is Coordinated Universal Time, not the local time.

What is Coordinated Universal Time and why would I want to use it? Coordinated Universal Time (which for some reason I don’t understand is abbreviated UTC) was called Greenwich Mean Time (GMT) until 1972. It is still referred to as “Zulu Time”, in some military organizations. UTC is the international standard for representing time.

UTC makes comparing times much easier. In the application where I use UTC, I am logging events in a client/server program. These events are occurring on clients that can be spread across several time zones. In order to understand the correct chronological order of the events all the times are logged as UTC.

As it turns out, returning the UTC time as a TDateTime object was fairly easy after I did some investigative work. I found that the Windows API function, GetSystemTime(), fills a SYSTEMTIME struct with the system date and time. Unfortunately there is no VCL equivalent that returns this time in a TDateTime object. However, the VCL function, SystemTimeToDateTime(), converts a SYSTEMTIME struct to a TDateTime object. This was all I needed to create the SystemDateTime() function. Here’s how that function looks:

 

TDateTime SystemDateTime()
{
  SYSTEMTIME systemTime;
  GetSystemTime(&systemTime);
  return (SystemTimeToDateTime(systemTime));
}

This simple function allows me to deal with UTC times in a familiar TDateTime object.

If you have a further interest in UTC and date and time standards, here are two Web sites you should look at: 

www.cl.cam.ac.uk/~mgk25/iso-time.html

www.iso.ch/markete/8601.pdf