やあ子供たち。
time_tのオレオレラッパーを作ってみたので紹介するぞ。最後に簡単なカレンダーの作り方もあるよ。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
using namespace std;
#define TIMETNEO_LINEMAX 80
#define TIMETNEO_FORMAT_DATE "%d/%02d/%02d"
#define TIMETNEO_FORMAT_TIME "%02d:%02d:%02d"
class TimeNeo
{
public:
TimeNeo(void) {
this->LoadNow();
}
TimeNeo(const TimeNeo& t)
{
_time = t._time;
}
void LoadNow(void)
{
_time = time(nullptr);
}
string getDateAsText(void)
{
int y, m, d;
this->getDate(&y, &m, &d);
char ss[TIMETNEO_LINEMAX];
sprintf(ss, TIMETNEO_FORMAT_DATE, y, m, d);
return string(ss);
}
string getDateTimeAsText(void)
{
int y, m, d, h, min, s;
this->getDateTime(&y, &m, &d, &h, &min, &s);
char ss[TIMETNEO_LINEMAX];
sprintf(ss, TIMETNEO_FORMAT_DATE " " TIMETNEO_FORMAT_TIME, y, m, d, h, min, s);
return string(ss);
}
void getDate(int* pyear, int* pmonth, int* pdate)
{
int h, m, s;
getDateTime(pyear, pmonth, pdate, &h, &m, &s);
return;
}
void getDateTime(int* pyear, int* pmonth, int* pdate, int* phour, int* pmin, int* psec)
{
tm tm0;
localtime_s(&tm0, &_time);
*pyear = tm0.tm_year + 1900;
*pmonth = tm0.tm_mon + 1;
*pdate = tm0.tm_mday;
*phour = tm0.tm_hour;
*pmin = tm0.tm_min;
*psec = tm0.tm_sec;
}
int getYear()
{
tm tm0;
localtime_s(&tm0, &_time);
return tm0.tm_year + 1900;
}
int getMonth()
{
tm tm0;
localtime_s(&tm0, &_time);
return tm0.tm_mon + 1;
}
int getDay()
{
tm tm0;
localtime_s(&tm0, &_time);
return tm0.tm_mday;
}
int getWeekDay()
{
tm tm0;
localtime_s(&tm0, &_time);
return tm0.tm_wday;
}
void setDate(const int year, const int month, const int date)
{
tm tm0;
localtime_s(&tm0, &_time);
tm0.tm_year = year - 1900;
tm0.tm_mon = month - 1;
tm0.tm_mday = date;
_time = mktime(&tm0);
return;
}
void setDate(const int date)
{
tm tm0;
localtime_s(&tm0, &_time);
tm0.tm_mday = date;
_time = mktime(&tm0);
return;
}
void setDateTime(const int year, const int month, const int date, const int hour, const int min, const int sec)
{
tm tm0;
localtime_s(&tm0, &_time);
tm0.tm_year = year - 1900;
tm0.tm_mon = month - 1;
tm0.tm_mday = date;
tm0.tm_hour = hour;
tm0.tm_min = min;
tm0.tm_sec = sec;
_time = mktime(&tm0);
return;
}
friend double operator-(const TimeNeo& a, const TimeNeo& b)
{
return difftime(a._time, b._time);
}
time_t operator=(const TimeNeo& a)
{
_time = a._time;
return _time;
}
bool operator<(const TimeNeo& a)
{
return (a - *this) >= 0;
}
TimeNeo& advanceDate(const int dday)
{
int y, m, d, h, min, s;
this->getDateTime(&y, &m, &d, &h, &min, &s);
d += dday;
this->setDateTime(y, m, d, h, min, s);
return *this;
}
TimeNeo& advanceMonth(const int dmon)
{
int y, m, d, h, min, s;
this->getDateTime(&y, &m, &d, &h, &min, &s);
m += dmon;
this->setDateTime(y, m, d, h, min, s);
return *this;
}
TimeNeo& incDate(void)
{
return advanceDate(1);
}
TimeNeo& decDate(void)
{
return advanceDate(-1);
}
TimeNeo& incMonth(void)
{
return advanceMonth(1);
}
TimeNeo& decMonth(void)
{
return advanceMonth(-1);
}
time_t _time;
};
int main(int argc, char* argv[])
{
TimeNeo tn0;
TimeNeo tn1;
tn1.setDate(1971, 2, 17);
cout << tn0.getDateTimeAsText() << endl;
cout << tn1.getDateTimeAsText() << endl;
{
int y, m, d, h, min, s;
tn0.getDateTime(&y, &m, &d, &h, &min, &s);
d += 30;
h += 12;
tn0.setDateTime(y, m, d, h, min, s);
cout << tn0.getDateTimeAsText() << endl;
}
TimeNeo tn2 = tn0;
{
int y, m, d, h, min, s;
tn2.getDateTime(&y, &m, &d, &h, &min, &s);
s += 30;
tn2.setDateTime(y, m, d, h, min, s);
cout << tn2.getDateTimeAsText() << endl;
cout << "difftime: " << tn2 - tn1 << endl;
}
TimeNeo tn3;
{
int y, m, d, h, min, s;
tn3.getDateTime(&y, &m, &d, &h, &min, &s);
s-=1.6e9;
tn3.setDateTime(y, m, d, h, min, s);
cout << tn3.getDateTimeAsText() << endl;
}
cout<< ( tn3 < tn0) <<endl;
TimeNeo idate;
idate.setDate(2022, 8, 17);
cout << "**** Calendar "
<< setw(4) << to_string(idate.getYear())
<<"/"<< setw(2) << setfill('0') << to_string(idate.getMonth())
<<" ****" << endl;
cout << "Sun Mon Tue Wed Thr Fri Sat" << endl;
{
idate.setDate(1);
while (idate.getWeekDay() != 0)
idate.decDate();
TimeNeo edate = idate;
{
edate.incMonth();
while (edate.getWeekDay() != 0)
edate.incDate();
edate.decDate();
}
int col = 0;
while (idate < edate)
{
cout << setw(2) << to_string(idate.getDay()) << " ";
if (++col == 7)
{
cout << endl;
col = 0;
}
idate.incDate();
}
}
return 0;
}