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

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

, 24 Jun 2005
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralERROR in class PinmemberAlezis10-Jul-07 1:38 
GeneralRe: ERROR in class [modified] PinmemberAlezis10-Jul-07 9:40 
GeneralRe: ERROR in class Pinmemberjjjch3-Jun-08 5:33 
GeneralRe: ERROR in class PinmemberAlezis8-Jun-08 4:34 
GeneralUse with the date time picker control PinmemberPerry Zh2-Jul-05 20:35 
Questionany docs ? Pinmembertoxcct30-Jun-05 0:23 
AnswerRe: any docs ? Pinmemberphwp30-Jun-05 5:49 
GeneralRe: any docs ? Pinmembertoxcct30-Jun-05 5:51 
GeneralLeap years Pinmembertrelliot29-Jun-05 19:45 
GeneralRe: Leap years Pinmemberphwp30-Jun-05 6:01 
GeneralMissing file PinmemberDavidCrow29-Jun-05 2:33 
GeneralRe: Missing file Pinmemberphwp29-Jun-05 6:37 

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 | Mobile
Web02 | 2.8.141022.2 | Last Updated 24 Jun 2005
Article Copyright 2005 by Philippe Kirsanov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid