Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ DateTime class

I have my own C++ DateTime class defined as:

class DateTime
{
public:
  int year;
  int month;
  int day;
  int hour;
  int min;
  int sec;
  int millisec;
};

I have 2 DateTime which I need to compare to see which one is greater than (more recent) the other.

Is there any freely available C++ DateTime class that I can use to

  1. Convert my DateTime class to their DateTime class
  2. Their class should provide < , > , <= , >= operators for comparison

If a concrete example could be provided that would be great. Note that I need to compare down to millisecond.

I was thinking about Boost or Qt. Preferred Boost though.

like image 808
sivabudh Avatar asked Aug 12 '09 16:08

sivabudh


People also ask

What is the use of date and time in C?

The C date and time functions are a group of functions in the standard library of the C programming language implementing date and time manipulation operations. They provide support for time acquisition, conversion between date formats, and formatted output to strings. 1 Overview of functions.

What is The DateTime class in Python?

class datetime.datetime (year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0) The year, month, and day arguments are mandatory. tzinfo can be None, rest all the attributes must be an integer in the following range –

What is the difference between datetime and date class?

Like a date object, DateTime assumes the current Gregorian calendar extended in both directions; like a time object, DateTime assumes there are exactly 3600*24 seconds in every day. But unlike date class, the objects of DateTime class are potentially aware objects i.e. it contains information regarding time zone as well. Attention geek!

What is the datetime value type?

The DateTime value type represents dates and times with values ranging from 00:00:00 (midnight), January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.) in the Gregorian calendar.


2 Answers

What's wrong with using the content of <time.h> for implementing your class? It's standard C90.

like image 198
AProgrammer Avatar answered Oct 12 '22 21:10

AProgrammer


I ditch storing dates in gregorian ages ago. I store dates as an 32bit integer (sort of like a Julian date). So the date is composed as (Year * 1000) + DOY (DOY is day of year). I.e. - 2009001 Is Jan 1 2009 - 2009365 is Dec 31 2009

My date class of course provides methods for getting the Year, Month and Day, adding, subtracting, incrementing and decrementing, comparing, getting the number of days between dates etc..

For date and time, I use 64bit float where the integer portion of the real number is the same as integer (Julian like) dates described above, and the fraction represents the time in fraction of a day.

I.e.

  • 2009001.04166666666~ is Jan 1,2009 1:00am
  • 2009001.06249999999~ is Jan 1,2009 1:30am
  • 2009001.95833333333~ is Jan 1,2009 11:00pm

If you only need minute accuracy, you can use 32bit float for date and time but you can't adequately accurately store seconds and milliseconds.

The advantages of storing dates (and time) in this manner are:

  • You only need 8bytes to represent the data and time as compared to 28bytes (assuming 32bit integers) used by the DateTime class in the question.

  • Compared with dates stored as seconds from an epoch, when looking at the number (for example in the debugger) you can more or less identify from the number the year and the day of year, and the approximate time of day (to get the hour, minute, second after midnight simply mulitply by 24, 1440, 86400 respectively).

  • Comparing dates is trivial, simply compare the numbers (A single CPU operation compared to the several it would take for the example DateTime).

  • Fewer comparison operations to do date arithmetic.

The disadvange of this (for time time) is a slight loss of accuracy (this is practically a mute point) and you have to do some simple rounding to get nice integer values when convering to integer values of hours minutes and seconds.

like image 23
Roger Nelson Avatar answered Oct 12 '22 21:10

Roger Nelson