在开始入坑C++的时间魔法学习的开始,我曾发下宏愿,希望能够通过所学构建一个自己的好用的C++的时间帮助类。它需要具有下面一些特性:
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名与空间、虚拟主机、营销软件、网站建设、社旗网站维护、网站推广。进过一段时间的努力,C++的时间魔法的学习要渐渐迎来它的落幕。今天在这里我需要兑现上面相当大一部分诺言。大地上慢慢响起时间魔法咒语的迎唱声,我看向深邃的星空,繁星开始律动,洒下无数的星辉,时间的五彩光辉在时空尽头盘旋,横梗古今 —— 那就是我的魔法啊!
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的时间进行增减运算。同时,为了支持LocalDateTime的时间判断能力,我也对TimeScale的能力进行的扩充,增加了逻辑判断运算符的重载:
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);
这样我们可以借助对时间刻度的逻辑判断实现时间魔法的逻辑判断,以大小判断为例:
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;
}
同时提供了多个重载的构造方法,实现了获取本地时间,通过日期或者通过具体时间构建时间的方法:
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);
其中还是以默认构造函数为例:
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);
}
此外了为了方便调试,我还提供了全局的输出操作重载,打印输出时间值。当然这里有一点比较重要就是,不要把输出重载操作定义在命名空间内,不然你有可能找不到定义。
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;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧