Example of date & time manipulation.
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";
assert(!is(date));
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;
}
Top level include file for the GNU uCommon C++ core library.
struct tm tm_t
Convenience type for struct tm.
Common namespace for all ucommon objects.
The Date class uses a julian date representation of the current year, month, and day.
unsigned dow(void) const
Get the day of the week (0-7).
int year(void) const
Get the year of the date.
const char * put(char *buffer) const
Get a ISO string representation of the date (yyyy-mm-dd).
unsigned day(void) const
Get the day of the month of the date.
long get(void) const
Get the date as a number for the object or 0 if invalid.
time_t timeref(void) const
Get a time_t for the julian date if in time_t epoch.
unsigned month(void) const
Get the month of the date (1-12).
The Datetime class uses a julian date representation of the current year, month, and day and a intege...
A copy-on-write string class that operates by reference count.