DateTime in C++
Many years ago we had a need to manipulate the data of types "date" and "time" (DateTime in C# and Delphi/C++Builder). I had developed the corresponding class for these purposes.
Here is the interface with comments:
/*
* Initializes the object with the values taken from the formatted string like:
* "YYYY-MM-DD hh:mm:ss"
* "YYYY-MM-DD hh:mm"
* "YYYY-MM-DD"
* "hh:mm:ss"
* "hh:mm"
* where number of the seconds is 0 by default
*/
DateTime(const char* datetime);
DateTime(const string& datetime);
/*
* Copy constructor
*/
DateTime(const DateTime& datetime);
/*
* Assignment operator
*/
DateTime& operator =(const DateTime& datetime);
enum Format
{
INVALID = 0,
DATETIME,
DATE,
TIME
};
enum TimeUnit
{
SECONDS,
MINUTES,
HOURS,
DAYS,
MONTHS,
YEARS,
};
enum DatePart
{
NONE = -1,
SECOND = 0,
MINUTE,
HOUR,
TIME_HM,
TIME_HMS,
DATE_TIME_HM,
DATE_TIME_HMS,
DATE_ONLY,
DAY_OF_WEEK,
DAY_OF_MONTH,
DAY_OF_YEAR,
MONTH_OF_YEAR,
MONTH_AND_DAY,
QUARTER,
YEAR,
YEAR_AND_MONTH,
YEAR_AND_QUARTER,
};
/*
* Returns the part of date
*
* E.g. if the initial date is 01/01/1973 00:00:00
* getDatePart(YEAR) returns 1973
* getDatePart(MONTH) returns 1
* getDatePart(DAY_OF_MONTH) returns 1
*/
DatePartValue getDatePart(DatePart part);
/*
* Returns the number of the required date/time unit since the epoch
* (currently is 00:00:00 November 17, 1858)
* If the format is TIME, returns the 0 for all date units.
*
* E.g. if the initial date is 01/01/1973 00:00:00
* getValue( HOURS ) returns the number of hours since the epoch
* getValue( SECONDS ) returns the number of seconds since the epoch
*/
Value getValue(TimeUnit unit);
/*
* Returns one of the datetime formats, see the Format enumeration
*/
Format getFormat();
/*
* Returns the date and time in format "YYYY-MM-DD hh:mm:ss"
*/
string toString();
/*
* Increments the date/time value. The offset is expressed in the time units
* specified in timeUnit.
*/
void increment(int offset, TimeUnit timeUnit);
/*
* Returns the modified Julian date (the number of days since 17/11/1858)
* of the date specified by the day/month/year
*/
static Value getMJDFromDate(short day, short month, short year);
/*
* Returns the date structure given the number of days since 17/11/1858
* Note that the tm_year member already contains the right year (not the year minus 1900),
* and the tm_mon member starts with 1, not 0.
*/
static struct tm getDateFromMJD(Value days);
/*
* Returns the quarter number given the month number
*/
static short getQuarterFromMonth(short month);
/*
* Returns the day of week (1 - Monday, 2 - Tuesday, ... 7 - Sunday)
* of the date specified by the day/month/year
*/
static short getDayOfWeek(short day, short month, short year);
/*
* Returns the absolute value of the difference between two dates
* specified by day/month/year in the specified time units
* Warning: only DAYS, MONTHS and YEARS units accepted
* otherwise returns 0
*/
static Value getDateDiff
(
short day1, short month1, short year1,
short day2, short month2, short year2,
TimeUnit unit
);
/*
* Returns true if the specified year is a leap year
*/
static bool isLeapYear(short year);
/*
* Returns the number of days in the specified month/year
*/
static short getDaysInMonth(short month, short year);
/*
* Returns the number of days in the specified month/year
*/
static short getMonthNumberByName(const char *name);
These files DateTime.h
and DateTime.cpp
are under GPL. You can download them and use/modify like your owns.
blog comments powered by Disqus