Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / CUDA
Article

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

Rate me:
Please Sign up or sign in to vote.
3.22/5 (7 votes)
24 Jun 20052 min read 135.8K   301   18   12
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


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralERROR in class Pin
Alezis10-Jul-07 1:38
professionalAlezis10-Jul-07 1:38 
GeneralRe: ERROR in class [modified] Pin
Alezis10-Jul-07 9:40
professionalAlezis10-Jul-07 9:40 
GeneralRe: ERROR in class Pin
jjjch3-Jun-08 5:33
jjjch3-Jun-08 5:33 
GeneralRe: ERROR in class Pin
Alezis8-Jun-08 4:34
professionalAlezis8-Jun-08 4:34 
GeneralUse with the date time picker control Pin
Mingliang Zhu2-Jul-05 20:35
Mingliang Zhu2-Jul-05 20:35 
Questionany docs ? Pin
toxcct30-Jun-05 0:23
toxcct30-Jun-05 0:23 
AnswerRe: any docs ? Pin
phwp30-Jun-05 5:49
phwp30-Jun-05 5:49 
GeneralRe: any docs ? Pin
toxcct30-Jun-05 5:51
toxcct30-Jun-05 5:51 
GeneralLeap years Pin
trelliot29-Jun-05 19:45
trelliot29-Jun-05 19:45 
GeneralRe: Leap years Pin
phwp30-Jun-05 6:01
phwp30-Jun-05 6:01 
GeneralMissing file Pin
David Crow29-Jun-05 2:33
David Crow29-Jun-05 2:33 
GeneralRe: Missing file Pin
phwp29-Jun-05 6:37
phwp29-Jun-05 6:37 

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

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