Click here to Skip to main content
11,428,281 members (45,161 online)
Click here to Skip to main content

High resolution date and time class

, 24 Jan 2000
Rate this:
Please Sign up or sign in to vote.
A high resolution time class that is a replacement for COleDateTime that does not use MFC.
  • Download source files - 11 Kb
  • <!-- Article Starts -->

    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

    Share

    About the Author

    Håkan Still
    Software Developer
    Sweden Sweden
    No Biography provided

    Comments and Discussions

     
    GeneralClasses are not to be sold for profit Pin
    chunkeungyu20-May-10 9:57
    memberchunkeungyu20-May-10 9: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) method Pin
    Member 3211554-Feb-09 4:17
    memberMember 3211554-Feb-09 4:17 
    GeneralRe: Race condition in the const CHighTime& operator=(const time_t& timeSrc) method Pin
    rharding648-Jan-15 16:24
    professionalrharding648-Jan-15 16:24 
    QuestionVS 2005 or VS 2008 Version Anyone? Pin
    Paul Kissel10-Sep-08 23:20
    memberPaul Kissel10-Sep-08 23:20 
    AnswerRe: VS 2005 or VS 2008 Version Anyone? Pin
    coder1234567896-Oct-08 1:40
    membercoder1234567896-Oct-08 1:40 
    GeneralRe: VS 2005 or VS 2008 Version Anyone? Pin
    Kim Moung Soo5-Mar-10 21:25
    memberKim Moung Soo5-Mar-10 21:25 
    GeneralGet total Milliseconds Pin
    msurni13-Dec-07 20:28
    membermsurni13-Dec-07 20:28 
    GeneralMistake with milli/micro/nano Pin
    abcdenis12-Jan-07 0:02
    memberabcdenis12-Jan-07 0:02 
    GeneralActual resolution much lower... Pin
    Robert Bielik9-Jun-06 3:00
    memberRobert Bielik9-Jun-06 3:00 
    GeneralRe: Actual resolution much lower... Pin
    fvds_sub26-Oct-06 6:29
    memberfvds_sub26-Oct-06 6:29 
    GeneralWEIRD: CHighTimeSpan::Format() does not take 1 arguments Pin
    kd7osk21-Feb-06 10:11
    memberkd7osk21-Feb-06 10:11 
    QuestionIssue with subtraction bug? Pin
    David White23-Dec-05 5:47
    memberDavid White23-Dec-05 5:47 
    Generalambiguous call Pin
    EMTA4114-Oct-05 7:46
    memberEMTA4114-Oct-05 7:46 
    GeneralRe: ambiguous call Pin
    craig.miller30-Jan-06 15:00
    membercraig.miller30-Jan-06 15:00 
    GeneralRe: ambiguous call Pin
    kd7osk20-Feb-06 12:35
    memberkd7osk20-Feb-06 12:35 
    GeneralHightime for Visual C++ .NET 2003 Pin
    abudiman25017225-Sep-05 22:38
    memberabudiman25017225-Sep-05 22:38 
    Generalwhy not just use CTime? its a 64bit Integer Pin
    m00m0024-Aug-05 14:57
    memberm00m0024-Aug-05 14:57 
    GeneralRe: why not just use CTime? its a 64bit Integer Pin
    Steve Mayfield24-Aug-05 15:16
    memberSteve Mayfield24-Aug-05 15:16 
    Generalgetting started Pin
    Anonymous2-Feb-05 1:20
    sussAnonymous2-Feb-05 1:20 
    GeneralA bug in CHighTime::GetYear() when a year is negative Pin
    holevel629-Feb-04 20:59
    memberholevel629-Feb-04 20:59 
    QuestionWhats about CFileTime? Pin
    osy2-Feb-04 21:51
    memberosy2-Feb-04 21:51 
    AnswerRe: Whats about CFileTime? Pin
    Pierre Couderc10-Nov-05 5:42
    memberPierre Couderc10-Nov-05 5:42 
    AnswerRe: Whats about CFileTime? Pin
    hulvey27-Jan-06 15:13
    memberhulvey27-Jan-06 15:13 
    GeneralYou do good work! Pin
    devwat24-Jan-04 17:42
    memberdevwat24-Jan-04 17:42 
    GeneralGeneral questin on .inl files Pin
    dswigger11-Jan-04 6:09
    memberdswigger11-Jan-04 6:09 
    GeneralRe: General questin on .inl files Pin
    rharding648-Jan-15 16:32
    professionalrharding648-Jan-15 16:32 
    QuestionUpgrades ??? Pin
    JoeHeaton25-Jul-03 1:14
    memberJoeHeaton25-Jul-03 1:14 
    AnswerRe: Upgrades ??? Pin
    Holger Persch15-Sep-03 2:04
    memberHolger Persch15-Sep-03 2:04 
    GeneralRe: Upgrades ??? Pin
    Bernhard29-Oct-03 0:27
    memberBernhard29-Oct-03 0:27 
    GeneralRe: Upgrades ??? Pin
    jacques1118-Jun-04 10:41
    memberjacques1118-Jun-04 10:41 
    GeneralRe: Upgrades ??? Pin
    Holger Persch14-Jun-04 2:23
    memberHolger Persch14-Jun-04 2:23 
    GeneralRe: Upgrades ??? Pin
    Defenestration7-Oct-06 13:08
    memberDefenestration7-Oct-06 13:08 
    Questionvariant variable (VT_DATE) to CHighTime?? Pin
    Anonymous19-Jul-03 22:19
    sussAnonymous19-Jul-03 22:19 
    AnswerRe: variant variable (VT_DATE) to CHighTime?? Pin
    Holger Persch11-Aug-03 20:41
    memberHolger Persch11-Aug-03 20:41 
    GeneralRe: variant variable (VT_DATE) to CHighTime?? Pin
    tkuan19-Feb-05 18:03
    membertkuan19-Feb-05 18:03 
    General* bookmarked * Pin
    Nitron26-Mar-03 15:45
    memberNitron26-Mar-03 15:45 
    Generalunexpected end of file error Pin
    eranb27-Jan-03 22:58
    membereranb27-Jan-03 22:58 
    GeneralRe: unexpected end of file error Pin
    Mike Nordell27-Jan-03 23:15
    memberMike Nordell27-Jan-03 23:15 
    GeneralRe: unexpected end of file error Pin
    eranb27-Jan-03 23:51
    membereranb27-Jan-03 23:51 
    GeneralRe: unexpected end of file error Pin
    DenisZ2-Feb-03 6:13
    sussDenisZ2-Feb-03 6:13 
    GeneralResolution again. Pin
    Diego Serrano14-Jan-03 6:52
    memberDiego Serrano14-Jan-03 6:52 
    GeneralRe: Resolution again. Pin
    Håkan Trygg14-Jan-03 22:27
    memberHåkan Trygg14-Jan-03 22:27 
    GeneralTRUE resolution of this class Pin
    Art Baker22-Aug-02 4:22
    sussArt Baker22-Aug-02 4:22 
    GeneralRe: TRUE resolution of this class Pin
    Håkan24-Aug-02 6:40
    sussHåkan24-Aug-02 6:40 
    GeneralRe: TRUE resolution of this class Pin
    ANONUMOUS14-Oct-02 23:55
    sussANONUMOUS14-Oct-02 23:55 
    GeneralRe: TRUE resolution of this class Pin
    dwsmith3721-Feb-03 10:39
    memberdwsmith3721-Feb-03 10:39 
    GeneralRe: TRUE resolution of this class Pin
    JimmyO12-Oct-04 21:07
    memberJimmyO12-Oct-04 21:07 
    GeneralHow to get Get TRUE time : Pin
    Pierre Couderc10-Nov-05 6:13
    memberPierre Couderc10-Nov-05 6:13 
    GeneralBug fix in HighTime::replaceStr() Pin
    Marcello18-Aug-02 7:04
    memberMarcello18-Aug-02 7:04 
    GeneralPlease keep on working!!! Pin
    Holger Persch18-Jun-02 21:52
    memberHolger Persch18-Jun-02 21:52 

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

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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