Example of date & time manipulation.
#ifndef DEBUG
#define DEBUG
#endif
#include <ucommon-config.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int exp_year = 2003;
unsigned exp_month = 1;
unsigned exp_day = 6;
unsigned 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.
year());
assert(exp_month == date.
month());
assert(exp_day == date.
day());
assert(exp_dayofweek == date.
dow());
exp_stringdate = date();
assert(
eq(*exp_stringdate,
"2003-01-06"));
assert(
eq(buf,
"2003-01-06"));
assert(exp_ctime == date.
timeref());
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);
eq((
const char *)dts,
"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;
}