The Code Project View our sponsorsClick here for Dundas Consulting - experts in MFC, C++, TCP/IP and ASPAdvertise on the CodeProject
Home >> Date / Time handling >> General

High resolution date and time class
By Håkan Trygg

A high resolution time class that is a replacement for COleDateTime that does not use MFC. 
 VC6, Win9x, NT4
 Posted 18 Jan 2000
 Updated 25 Jan 2000
Articles by this author
Send to a friend
Printer friendly version
Lounge New Articles Sign in Forums Contribute
Broken links? Email us!
17 users have rated this article. result:
4.65 out of 5.

  • Download source files - 11 Kb
  • 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 that accuracy of floating point is not so good with small values. My experience says COleDateTime(Span) can not 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) handle 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 do before it is possible to use it in a driver. All MFC use must be also 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. eg. 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

    1. 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.

    [Top] Sign in to vote for this article:     PoorExcellent  
    Hint: For improved responsiveness, use Internet Explorer 4 (or above) with Javascript enabled, choose 'Use DHTML' from the View dropdown and hit 'Set Options'.
     Keyword Filter
     View   Per page   Messages since
    New threadMessages 1 to 12 of 12 (Total: 12)First Prev Next Last
    Subject 
    Author 
    Date 
      0.00002s time delay
    Julia 11:25 26 Apr 01 
      0.00002s time delay
    Julia 11:23 26 Apr 01 
      Re: 0.00002s time delay
    Håkan Trygg 15:45 26 Apr 01 
      This class don't support UNICODE??
    Marcin Zielinski 4:33 30 Aug 00 
      Missing Feature: Assignment/c'tor with VARIANT
    Uwe Keim 13:04 13 Apr 00 
      Re: Missing Feature: Assignment/c'tor with VARIANT
    Håkan 2:54 14 Apr 00 
      Fantastic
    Russell Robnson 2:59 21 Jan 00 
      Daylight saving?
    Holger Persch 3:19 19 Jan 00 
      Re: Daylight saving?
    elib 6:25 16 Oct 00 
      Re: Daylight saving?
    elib 13:40 16 Oct 00 
      Re: Daylight saving?
    Håkan 14:20 16 Oct 00 
      Re: Daylight saving?
    Holger Persch 2:02 27 Oct 00 
    Last Visit: 12:00 Friday 1st January, 1999First Prev Next Last

    Home >> Date / Time handling >> General
    last updated 25 Jan 2000
    Article content copyright Håkan Trygg, 2000
    everything else © CodeProject, 1999-2001.
    The Code Project View our sponsorsClick here for Dundas Software's TCP/IP Development KitAdvertise on the CodeProject