Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

High Resolution Date and Time Class

0.00/5 (No votes)
24 Jan 2000 2  
A high resolution time class that is a replacement for COleDateTime that does not use MFC.

Overview

The CHighTime and CHighTimeSpan are two classes for replacement of COleDateTime and COleDateTimeSpan. Instead of using a double for storing the date, it uses a 64 bit integer to store the date and time. The range is +/-29000 years and the smallest time span is 0.1 microseconds.

Background

As "everybody" knows, accuracy of floating point is not so good with small values. My experience says COleDateTime(Span) cannot handle 1 second time and spans correctly. Sometimes, I would get: (2sec-1sec) != 1sec ... This was not what I wanted!

Secondly, the resolution for COleDateTime is only 1 second and I needed better. CHighTime(Span) handles down to 0.1 microsecond. I choose this because FILETIME and the KeQuerySystemTime function in the kernel use this resolution. One strange thing is that they have zero time at January 1, 1601. But I follow that convention for easy integration.

Finally, I needed to calculate time in a kernel driver and there floating point maths not possible. There are some changes that are needed to be made before it is possible to use it in a driver. All MFC use must also be removed. I have started but haven't finished it. It should also be possible to use the classes in a non MFC project with some small not yet implemented parts... See below.

To Use

Using CHighTime(Span) is quite simple and similar to COleDateTime(Span). Include CHighTime.h where you need it and create an instance of CHighTime. There are some different constructors. Both with separate date/time parts and with COleDateTime or SYSTEMTIME or FILETIME as arguments. The output format string is the same as for COleDateTime(Span).Format and _tcsftime with additional codes for millisec(%s), microsec(%u), nanosec(%n).

CHighTimeSpan is also capable of handling "out of range" values, e.g. 30 hours = > 1 day + 6 hours

The constructors have milli, micro, nano default value 0 so it is possible to replace COleDateTime directly without any changes.

CHighTime PresentTime, SomeTime;
CHighTimeSpan TheLife, OneDay(1,0,0,0);
CString sText;
SYSTEMTIME systime;

PresentTime = CHighTime::GetPresentTime();
SomeTime = CHighTime(1968, 6, 8, 0, 2, 0);
TheLife = PresentTime - SomeTime;
sText = TheLife.Format(
    "I have lived %D days %H hours %M minutes %S seconds %s milliseconds\n"
);
AfxMessageBox(sText);
systime = CHighTime(2000,1,13, 14,07,10, 20, 40, 100);
SomeTime.SetDateTime(2000, 1, 13, 14, 25, 10);
sText.Format("The time now is %s\n", (LPCTSTR)PresentTime.Format("%H:%M:%S:%s"));
sText.Format(
    "The date tomorrow is %s\n",
    (LPCTSTR)(PresentTime+OneDay).Format("%Y:%m:%d")
);

If you want to use the class in a MFC project, add #include "stdafx.h" before the include of hightime.h, in the hightime.cpp like this:

#include "stdafx.h"
#include "hightime.h"

Things to Improve

  • Modify so a kerneldriver can use the classes. The string functions must be removed/changed. The only need for the CHighTime::Format function should be for trace. So why make that work for almost nothing....??? A additional #define could be used for using the classes in a driver.

Please feel free to send me any suggestions about these classes.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here