Click here to Skip to main content
Licence 
First Posted 24 Jun 2005
Views 30,567
Bookmarked 17 times

Small class representing DateTime in seconds elapsed since "01 Jan, 0001 00:00:00"

By | 24 Jun 2005 | Article
A small class representing DateTime in seconds elapsed since "01 Jan, 0001 00:00:00".

Introduction

Everyone, from time to time faces the problem that CRT "time" does not meet the requirements of the task. Mostly it's lack of date range, a lot of things have happened before the year 1970. I've implemented a simple class that keeps date/time as seconds elapsed since "01 Jan, 0001 00:00:00". It allows converting numeric representation of date/time to seconds and vice versa. Also, it allows to add/subtract a period of time.

The class can be modified very simply to meet the extra requirements you might have.

Classes

  • CUDateTime - class representing DateTime as seconds elapsed since "01 Jan, 0001 00:00:00".
  • SUDateTime - helper structure representing DateTime as a set of unsigned short's.
  • CUDateSpan - helper class representing period of time to add to date or to subtract from date.

Interface of CUDateTime class

  • CUDateTime ( y, m, d, h, n, s )
  • CUDateTime ( const CUDateTime & )
  • CUDateTime ( const SUDateTime & )
  • unsigned short WeekDay ( void ) const - returns day of week [0-6] (Sun-Sat).
  • operator SUDateTime ( void ) const - converts to SUDateTime.
  • operator std::wstring ( void ) const - converts to std::wstring.
  • friend bool operator < ( const CUDateTime &, const CUDateTime & ) - operator less.
  • friend bool operator == ( const CUDateTime &, const CUDateTime & ) - operator equal.
  • friend CUDateTime operator + ( const CUDateTime &, const CUDateSpan & ) - add span to date.
  • friend CUDateTime operator - ( const CUDateTime &, const CUDateSpan & ) - subtract span from date.
  • friend CUDateTime & operator -= ( CUDateTime &, const CUDateSpan & ) - add span to date.
  • friend CUDateTime & operator += ( CUDateTime &, const CUDateSpan & ) - subtract span from date.

Interface of CUDateSpan class

  • CUDateSpan ( void )
  • CUDateSpan ( const CUDateSpan & )
  • CUDateSpan ( d, h, n, s )

Example of usage

//"22 June, 2005 21:22:15"
CUDateTime dt ( 2005, 6, 22, 21, 22, 15 );
//Add 1 day, 1 hour and 3 seconds
SUDateTime sdt = dt + CUDateSpan ( 1, 1, 0, 3 );
wprintf ( L"%s", ( ( std::wstring ) CUDateTime ( sdt ) ).c_str () );

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

Philippe Kirsanov



Canada Canada

Member



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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralERROR in class PinmemberAlezis1:38 10 Jul '07  
GeneralRe: ERROR in class [modified] PinmemberAlezis9:40 10 Jul '07  
GeneralRe: ERROR in class Pinmemberjjjch5:33 3 Jun '08  
GeneralRe: ERROR in class PinmemberAlezis4:34 8 Jun '08  
GeneralUse with the date time picker control PinmemberPerry Zh20:35 2 Jul '05  
Questionany docs ? Pinmembertoxcct0:23 30 Jun '05  
AnswerRe: any docs ? Pinmemberphwp5:49 30 Jun '05  
GeneralRe: any docs ? Pinmembertoxcct5:51 30 Jun '05  
GeneralLeap years Pinmembertrelliot19:45 29 Jun '05  
GeneralRe: Leap years Pinmemberphwp6:01 30 Jun '05  
GeneralMissing file PinmemberDavidCrow2:33 29 Jun '05  
GeneralRe: Missing file Pinmemberphwp6:37 29 Jun '05  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 24 Jun 2005
Article Copyright 2005 by Philippe Kirsanov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid