Example of date & time manipulation.
#ifndef DEBUG
#define DEBUG
#endif
#include <ucommon-config.h>
#include <stdio.h>
using namespace UCOMMON_NAMESPACE;
int main(int argc, char **argv)
{
Date date = Date(2003, 1, 6);
int exp_year = 2003;
int exp_month = 1;
int exp_day = 6;
int exp_dayofweek = 1;
time_t exp_ctime;
char buf[20];
snprintf(buf, sizeof(buf),
"%04d-%02d-%02d", exp_year, exp_month, exp_day);
memset(&exp_dt, 0, sizeof(exp_dt));
exp_dt.tm_year = exp_year - 1900;
exp_dt.tm_mon = exp_month - 1;
exp_dt.tm_mday = exp_day;
exp_ctime = mktime(&exp_dt);
assert(exp_year == date[Date::year]);
assert(exp_month == date[Date::month]);
assert(exp_day == date[Date::day]);
assert(exp_dayofweek == date[Date::dow]);
exp_stringdate = date();
assert(
eq(*exp_stringdate,
"2003-01-06"));
date.get(buf);
assert(
eq(buf,
"2003-01-06"));
assert(exp_ctime == date.getTime());
Date aday = date;
Date nextday(2003, 1, 7);
assert(aday == date);
assert((++aday) == nextday);
assert(aday != date);
assert(date <= aday);
assert(date < aday);
Date newday = nextday + 5l;
assert((long)newday == 20030112l);
assert((long)nextday == 20030107l);
assert(newday - nextday == 5);
assert(20030106l == date.get());
date -= 6;
assert(20021231l == date.get());
date = "20031306";
date = "2003-08-04";
assert((long)date == 20030804l);
DateTimeString dts("2003-02-28 23:59:55");
eq((
const char *)dts,
"2003-02-28 23:59:55");
DateTime tmp("2003-02-28 23:59:55");
snprintf(buf, sizeof(buf), "%.5f", (double)tmp);
assert(
eq(buf,
"2452699.99994"));
assert((long)tmp == 20030228l);
tmp += 5;
assert((long)tmp == 20030301l);
return 0;
}