Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
24 Jun 2005 1  
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