在開始入坑C++的時間魔法學(xué)習(xí)的開始,我曾發(fā)下宏愿,希望能夠通過所學(xué)構(gòu)建一個自己的好用的C++的時間幫助類。它需要具有下面一些特性:
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名與空間、虛擬主機(jī)、營銷軟件、網(wǎng)站建設(shè)、社旗網(wǎng)站維護(hù)、網(wǎng)站推廣。進(jìn)過一段時間的努力,C++的時間魔法的學(xué)習(xí)要漸漸迎來它的落幕。今天在這里我需要兌現(xiàn)上面相當(dāng)大一部分諾言。大地上慢慢響起時間魔法咒語的迎唱聲,我看向深邃的星空,繁星開始律動,灑下無數(shù)的星輝,時間的五彩光輝在時空盡頭盤旋,橫梗古今 —— 那就是我的魔法??!
class LocalDateTime
{
private:
TimeScale *year;
TimeScale *month;
TimeScale *day;
TimeScale *hour;
TimeScale *minute;
TimeScale *second;
TimeScale *milliSecond;
public:
LocalDateTime();
LocalDateTime(long milliSecond);
LocalDateTime(int year, int month, int day);
LocalDateTime(int year, int month, int day, int hour, int minute, int second);
LocalDateTime(int year, int month, int day, int hour, int minute, int second, int milliSecond);
LocalDateTime(LocalDateTime &source);
TimeScale *getYear();
TimeScale *getMonth();
TimeScale *getDay();
TimeScale *getHour();
TimeScale *getMinute();
TimeScale *getSecond();
TimeScale *getMilliSecond();
bool operator<(LocalDateTime &other);
bool operator>(LocalDateTime &other);
bool operator<=(LocalDateTime &other);
bool operator>=(LocalDateTime &other);
bool operator==(LocalDateTime &other);
bool operator!=(LocalDateTime &other);
~LocalDateTime();
};
在時間魔法中,LocalDateTime是由Year,Month,Day等咒語組成的。通過LocalDateTime的每個時間刻度部分,我們可以對整個LocalDateTime的時間進(jìn)行增減運算。同時,為了支持LocalDateTime的時間判斷能力,我也對TimeScale的能力進(jìn)行的擴(kuò)充,增加了邏輯判斷運算符的重載:
bool operator<(const TimeScale &other);
bool operator>(const TimeScale &other);
bool operator<=(const TimeScale &other);
bool operator>=(const TimeScale &other);
bool operator==(const TimeScale &other);
bool operator!=(const TimeScale &other);
這樣我們可以借助對時間刻度的邏輯判斷實現(xiàn)時間魔法的邏輯判斷,以大小判斷為例:
bool datetime::LocalDateTime::operator<(datetime::LocalDateTime &other)
{
bool result = (*year)< (*(other.year));
if (!result)
{
result = (*month)< (*(other.month));
if (!result)
{
result = (*day)< (*(other.day));
if (!result)
{
result = (*hour)< (*(other.hour));
if (!result)
{
result = (*minute)< (*(other.minute));
if (!result)
{
result = (*second)< (*(other.second));
if (!result)
{
result = (*milliSecond)< (*(other.milliSecond));
}
}
}
}
}
}
return result;
}
同時提供了多個重載的構(gòu)造方法,實現(xiàn)了獲取本地時間,通過日期或者通過具體時間構(gòu)建時間的方法:
LocalDateTime();
LocalDateTime(long milliSecond);
LocalDateTime(int year, int month, int day);
LocalDateTime(int year, int month, int day, int hour, int minute, int second);
LocalDateTime(int year, int month, int day, int hour, int minute, int second, int milliSecond);
LocalDateTime(LocalDateTime &source);
其中還是以默認(rèn)構(gòu)造函數(shù)為例:
datetime::LocalDateTime::LocalDateTime()
{
long _milliseconds = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count();
_milliseconds += datetime::timezoneOffset() * 3600000;
int _year;
int _month;
int _day;
int _hour;
int _minute;
int _second;
int _millisecond;
datetime::getGregorianDateTime(_milliseconds, _year, _month, _day, _hour, _minute, _second, _millisecond);
new (this) LocalDateTime(_year, _month, _day, _hour, _minute, _second, _millisecond);
}
此外了為了方便調(diào)試,我還提供了全局的輸出操作重載,打印輸出時間值。當(dāng)然這里有一點比較重要就是,不要把輸出重載操作定義在命名空間內(nèi),不然你有可能找不到定義。
std::ostream &operator<<(std::ostream &out, datetime::LocalDateTime &localDateTime);
所以是時候,亮出我們真正的時間魔法了:
datetime::LocalDateTime::LocalDateTime()
{
long _milliseconds = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count();
_milliseconds += datetime::timezoneOffset() * 3600000;
int _year;
int _month;
int _day;
int _hour;
int _minute;
int _second;
int _millisecond;
datetime::getGregorianDateTime(_milliseconds, _year, _month, _day, _hour, _minute, _second, _millisecond);
new (this) LocalDateTime(_year, _month, _day, _hour, _minute, _second, _millisecond);
}
datetime::LocalDateTime::LocalDateTime(long milliSecond)
{
int _year;
int _month;
int _day;
int _hour;
int _minute;
int _second;
int _millisecond;
datetime::getGregorianDateTime(milliSecond, _year, _month, _day, _hour, _minute, _second, _millisecond);
new (this) LocalDateTime(_year, _month, _day, _hour, _minute, _second, _millisecond);
}
datetime::LocalDateTime::LocalDateTime(int year, int month, int day) : LocalDateTime(year, month, day, 0, 0, 0, 0) {}
datetime::LocalDateTime::LocalDateTime(int year, int month, int day, int hour, int minute, int second) : LocalDateTime(year, month, day, hour, minute, second, 0) {}
datetime::LocalDateTime::LocalDateTime(int year, int month, int day, int hour, int minute, int second, int milliSecond)
{
int daysInMonths[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (datetime::isLeapYear(year))
{
daysInMonths[1] = 29;
}
this->year = new datetime::Year{year};
this->month = new datetime::Month{this->year, month};
this->year->setChild(this->month);
this->day = new datetime::Day{this->month, day, daysInMonths[month - 1]};
this->month->setChild(this->day);
this->hour = new datetime::TimeScale{this->day, hour, 24};
this->day->setChild(this->hour);
this->minute = new datetime::TimeScale{this->hour, minute, 60};
this->hour->setChild(this->minute);
this->second = new datetime::TimeScale{this->minute, second, 60};
this->minute->setChild(this->second);
this->milliSecond = new datetime::TimeScale{this->second, milliSecond, 1000};
this->second->setChild(this->milliSecond);
}
datetime::LocalDateTime::LocalDateTime(LocalDateTime &source)
{
int _year = *(source.year);
int _month = *(source.month);
int _day = *(source.day);
int _hour = *(source.hour);
int _minute = *(source.minute);
int _second = *(source.second);
int _millisecond = *(source.milliSecond);
new (this) LocalDateTime(_year, _month, _day, _hour, _minute, _second, _millisecond);
}
datetime::TimeScale *datetime::LocalDateTime::getYear()
{
return year;
}
datetime::TimeScale *datetime::LocalDateTime::getMonth()
{
return month;
}
datetime::TimeScale *datetime::LocalDateTime::getDay()
{
return day;
}
datetime::TimeScale *datetime::LocalDateTime::getHour()
{
return hour;
}
datetime::TimeScale *datetime::LocalDateTime::getMinute()
{
return minute;
}
datetime::TimeScale *datetime::LocalDateTime::getSecond()
{
return second;
}
datetime::TimeScale *datetime::LocalDateTime::getMilliSecond()
{
return milliSecond;
}
bool datetime::LocalDateTime::operator<(datetime::LocalDateTime &other)
{
bool result = (*year)< (*(other.year));
if (!result)
{
result = (*month)< (*(other.month));
if (!result)
{
result = (*day)< (*(other.day));
if (!result)
{
result = (*hour)< (*(other.hour));
if (!result)
{
result = (*minute)< (*(other.minute));
if (!result)
{
result = (*second)< (*(other.second));
if (!result)
{
result = (*milliSecond)< (*(other.milliSecond));
}
}
}
}
}
}
return result;
}
bool datetime::LocalDateTime::operator>(datetime::LocalDateTime &other)
{
bool result = (*year) >(*(other.year));
if (!result)
{
result = (*month) >(*(other.month));
if (!result)
{
result = (*day) >(*(other.day));
if (!result)
{
result = (*hour) >(*(other.hour));
if (!result)
{
result = (*minute) >(*(other.minute));
if (!result)
{
result = (*second) >(*(other.second));
if (!result)
{
result = (*milliSecond) >(*(other.milliSecond));
}
}
}
}
}
}
return result;
}
bool datetime::LocalDateTime::operator==(datetime::LocalDateTime &other)
{
return (*year) == (*(other.year)) && (*month) == (*(other.month)) && (*day) == (*(other.day)) && (*hour) == (*(other.hour)) && (*minute) == (*(other.minute)) && (*second) == (*(other.second)) && (*milliSecond) == (*(other.milliSecond));
}
bool datetime::LocalDateTime::operator<=(datetime::LocalDateTime &other)
{
return !this->operator>(other);
}
bool datetime::LocalDateTime::operator>=(datetime::LocalDateTime &other)
{
return !this->operator<(other);
}
bool datetime::LocalDateTime::operator!=(datetime::LocalDateTime &other)
{
return !this->operator==(other);
}
datetime::LocalDateTime::~LocalDateTime()
{
delete year;
delete month;
delete day;
delete hour;
delete minute;
delete second;
delete milliSecond;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧