nlib
misc/datetime/datetime.cpp

nn::nlib::DateTimeを利用して日時の計算や表示を行うサンプルです。

DateTimeクラスの豊富な機能を利用することにより、日時の計算や表示を簡単に行うことが可能です。

bool SampleDateTime() {
char buf[32];
DateTime now;
DateTime::GetNow(&now);
// prints RFC2822 string
now.ToRfc2822(buf);
nlib_printf("Now is '%s' (in RFC2822)\n", buf);
// prints W3C-DTF string
now.ToW3cDtf(buf);
nlib_printf("Now is '%s' (in W3CDTF)\n", buf);
// calculates the day number of the year
int nth;
now.GetDayOfYear(&nth);
nlib_printf("Now is %dth day of this year\n", nth);
// you can add years/months/days to a DateTime object.
DateTime tmp = now;
e = tmp.AddDays(1000);
if (nlib_is_error(e)) return false;
tmp.ToRfc2822(buf);
nlib_printf("1000 days after now is '%s'\n", buf);
tmp = now;
e = tmp.AddDays(-1000);
if (nlib_is_error(e)) return false;
tmp.ToRfc2822(buf);
nlib_printf("1000 days before now is '%s'\n", buf);
// you can parse a RFC2822/W3C-DTF string
DateTime dt;
TimeSpan delta;
e = DateTime::Parse("2000-01-01", &dt, &delta);
if (nlib_is_error(e)) return false;
// you can calculate the duration between two DateTimes
TimeSpan span = now - dt;
int days, seconds;
span.Get(&days, &seconds, NULL, NULL);
nlib_printf("Now is %d days and %d seconds from 2000/01/01\n", days, seconds);
return true;
}
bool SampleMain(int, char**) { return SampleDateTime(); }
NLIB_MAINFUNC