Click here to Skip to main content
Click here to Skip to main content

High resolution date and time class

By , 24 Jan 2000
 
  • 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.

    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

    About the Author

    Håkan Still
    Software Developer
    Sweden Sweden
    Member
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralClasses are not to be sold for profitmemberchunkeungyu20 May '10 - 8:57 
    I am interested in compile and link the CHighTime class binary into a commerical product and sell it (not including the original source code). Is there a license restriction on this. The term "Classes are not to be sold for profit" is not clear whether it is OK to include the binary into a commercial product. Can you please clarify?
    GeneralRace condition in the const CHighTime& operator=(const time_t& timeSrc) methodmemberMember 3211554 Feb '09 - 3:17 
    const CHighTime& CHighTime::operator=(const time_t& timeSrc)
    {
        tm *pTimeSrc = localtime(&timeSrc);
     
        if (pTimeSrc) {
            _HighTimeFormat  SrcTime;
            SrcTime.nYear   = pTimeSrc->tm_year+1900;
            SrcTime.nMonth  = pTimeSrc->tm_mon+1;
            SrcTime.nDay    = pTimeSrc->tm_mday;
            SrcTime.nHour   = pTimeSrc->tm_hour;
            SrcTime.nMinute = pTimeSrc->tm_min;
            SrcTime.nSecond = pTimeSrc->tm_sec;
            ...
        }
    ...
    } // CHighTime::operator=(time_t)
     
    The localtime function you use in the assignment operator is not thread-safe as it uses statically allocated buffer which is not synchronized and each call to this function will destroys the contents of the previous call. However, I tried to find if there is a thread-safe version of that function as for strtok for example, but I failed! If there isn't, it means that the assignment operator and the constructor that uses it are not thread-safe.
     
    Thanks for the class!
    Orlin Hristov
    QuestionVS 2005 or VS 2008 Version Anyone?memberPaul Kissel10 Sep '08 - 22:20 
    I wrote a program a few years back using CHighTime and am now trying to move it to either VS 2005 or VS 2008 (preferred). I'm seeing a lot of build errors and warnings and wondered if anyone out there has modified the classes to build with either tool? If so, please email the new version to paulk@bendbroadband.com. Thanks!
    AnswerRe: VS 2005 or VS 2008 Version Anyone?membercoder1234567896 Oct '08 - 0:40 
    Same problem. Looking for any suggestion. Thanks too!
    GeneralRe: VS 2005 or VS 2008 Version Anyone?memberKim Moung Soo5 Mar '10 - 20:25 
    It's easy.
    Add MFC header for COleDateTime class definition.
    #include <afxdisp.h>
     
    And remove "CHighTime(const LONGLONG dateSrc);" function because VC++ says.
     
    e:\src\devp\usnproject\3thpartlib\hightime.h(75) : error C2535: 'CHighTime::CHighTime(const LONGLONG)' : member function already defined or declared
     
    Because LONGLONG and time_t type is identical.
    ^^
    GeneralGet total Millisecondsmembermsurni13 Dec '07 - 19:28 
    I want to get present time in total milliseconds not the number of milliseconds in present time. Can I get like that?
    GeneralMistake with milli/micro/nanomemberabcdenis11 Jan '07 - 23:02 
    thank you for lib.
     
    Your code (parameter names etc) based on big mistake: you mean, that in time period
     
    1,222333444
     
    there are 222 milliseconds, 333 microseconds, 444 nanoseconds.
     
    In fact, there are 222 milliseconds, 222333 microseconds and 222333444 nanoseconds.
     
    I mean, all the parameters should be named like milli_sec_part, micro_sec_part, nano_sec_part.
     
    Regards,
    Denis
    GeneralActual resolution much lower...memberRobert Bielik9 Jun '06 - 2:00 
    Although the apparent resolution is in the ns range, the actual resolution is about 1/100 th of a second. Try it.
    GeneralRe: Actual resolution much lower...memberfvds_sub26 Oct '06 - 5:29 
    When I interpete the code it is using the win32-timer which has a resolution of 55ms (This timer was already in good old DOS)
     
    fvds
    GeneralWEIRD: CHighTimeSpan::Format() does not take 1 argumentsmemberkd7osk21 Feb '06 - 9:11 
    I ran into this while trying to use the class in MFC. I'm embarrassed to say how long it took me to track down the problem. Let's just say I'm not accustumed to multiple class definitions in the same file.
     
    It appears that the method declatrations:
     

    CString Format(LPCTSTR lpszFormat) const;
    CString Format(UINT nFormatID) const;

     
    Were at some point moved outside the CHhighTimesSpan class definition. I missed it because I kept looking at the CHighTime class, being confused becase they were clearly there. I cut and pasted the lines from later in the file back into the class definition, and the problem was solved.
     
    Weird though...

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Permalink | Advertise | Privacy | Mobile
    Web03 | 2.6.130523.1 | Last Updated 25 Jan 2000
    Article Copyright 2000 by Håkan Still
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid