这里的日期实现比较简单:采用的是UTC日期(现在还不涉及到其他日期格式的转换如Gregorian日历),且以星期一作为每周的第一天。剩下的就看代码吧:

头文件:

class UTIL_API YKDateTime
{
public:enum DT_FORM{DTF_YMDHMS1,        //%Y-%m-%d %H:%M:%S,中文格式选此DTF_YMDHMS2,       //%m/%d/%Y %H:%M:%SDTF_YMDHM1,      //%Y-%m-%d %H:%M,中文格式选此DTF_YMDHM2,       //%Y/%m/%d %H:%MDTF_YMD1,       //%Y-%m-%d,中文格式选此DTF_YMD2,       //%Y/%m/%dDTF_HMS1,     //%H:%M:%S,中文格式选此DTF_HM1,            //%H:%M,中文格式选此DTF_MDYHMS1,       //%m-%d-%Y %H:%M:%SDTF_MDYHMS2,     //%m/%d/%Y %H:%M:%SDTF_MDYHM1,      //%m-%d-%Y %H:%MDTF_MDYHM2,     //%m/%d/%Y %H:%MDTF_MDY1,       //%m-%d-%YDTF_MDY2,     //%m/%d/%Y};static YK_UINT PER_SECOND;static YK_UINT PER_MINUTE;static YK_UINT PER_HOUR;static YK_UINT PER_DAY;//static YK_UINT PER_MONTH;  每个月的天数并不相同,因此不定义static YK_UINT PER_YEAR;
public:YKDateTime(YK_TM tm = 0);YKDateTime(YK_INT nYear, YK_INT nMonth, YK_INT nDay, YK_INT nHour = 0, YK_INT nMinute = 0, YK_INT nSecond = 0);~YKDateTime(void);static YKDateTime GetLocalTime();YKDateTime& operator=(YK_TM tm){ m_time = tm; return *this; }// 是否是有效日期YK_BOOL ValidDateTime();YK_TM GetDateTime() const { return m_time; }YKDateTime GetDate();            // 获取年月日YK_INT GetYear() const;         // 获取年YK_INT GetMonth() const;      // 获取月YK_INT GetDay() const;            // 获取天YK_INT GetHour() const;           // 获取小时YK_INT GetMinute() const;        // 获取分钟YK_INT GetSecond() const;        // 获取秒YK_INT GetDayOfYear() const;  // 获取时间所在年的第几天YK_INT GetDayOfMonth() const; // 获取时间所在月的第几天YK_INT GetDayOfWeek() const;  // 获取时间所在周的第几天,星期一作为第一天,(1~7)YK_INT GetWeekOfYear() const;  // 获取时间所在年的第几周,星期一作为第一天,(1~54)YK_INT GetWeekOfMonth() const;    // 获取时间所在月的第几周YK_INT GetFirstDayOfWeek() const; // 获取时间所在周的第一天YKString Format(DT_FORM dtf = DTF_YMDHMS1);// 日期或时间中文格式化 XXXX年XX月XX日 XX时XX分XX秒YKString FormatChs(DT_FORM dtf = DTF_YMDHMS1);YKString FormatPostfix(YK_WCHAR postfix);// 从字符串中提取日期时间static YKDateTime ParseDateTime(const YKString& str, DT_FORM tf = DTF_YMDHMS1);static YKDateTime ParseDateTimeChs(const YKString& str, DT_FORM tf = DTF_YMDHMS1);// 与当前时间相比,是否流逝了val秒YK_BOOL IsElapsed(YK_TM val) const;void AddSecond(YK_INT second){ *this += (second); }void AddMinute(YK_INT minute){ AddSecond(minute*PER_MINUTE); }void AddHour(YK_INT hour){ AddMinute(hour*PER_HOUR); }void AddDay(YK_INT day){ AddHour(day*PER_DAY); }void AddMonth(YK_INT month);void AddYear(YK_INT year){ AddMonth(year*PER_YEAR); }YKDateTime& operator+=(const YKDateTime& tm){ this->m_time += tm.m_time; return *this; }YKDateTime& operator-=(const YKDateTime& tm){ this->m_time -= tm.m_time; return *this; }YK_BOOL operator==(const YKDateTime& tm) const{ return this->m_time == tm.m_time; }YK_BOOL operator!=(const YKDateTime& tm ) const{ return !(*this == tm); }YK_BOOL operator>(const YKDateTime& tm ) const{ return this->m_time > tm.m_time; }YK_BOOL operator>=(const YKDateTime& tm ) const{ return !(*this < tm); }YK_BOOL operator<(const YKDateTime& tm ) const{ return tm > *this ; }YK_BOOL operator<=(const YKDateTime& tm ) const{ return !(*this > tm); }protected:YKString GetDTFormValue(DT_FORM dtf);private:YK_TM    m_time;
};

实现部分:

YK_UINT YKDateTime::PER_SECOND  = 1000;
YK_UINT YKDateTime::PER_MINUTE = 60;
YK_UINT YKDateTime::PER_HOUR = 60;
YK_UINT YKDateTime::PER_DAY = 24;
YK_UINT YKDateTime::PER_YEAR = 12;
YKDateTime::YKDateTime( YK_TM tm /* = 0 */ ): m_time(tm)
{}YKDateTime::YKDateTime( YK_INT nYear, YK_INT nMonth, YK_INT nDay, YK_INT nHour /*= 0*/, YK_INT nMinute /*= 0*/, YK_INT nSecond /*= 0*/ )
{tm t1;t1.tm_year = nYear-1900;t1.tm_mon = nMonth-1;t1.tm_mday = nDay;t1.tm_hour = nHour;t1.tm_min = nMinute;t1.tm_sec = nSecond;m_time = _mktime64(&t1);
}YKDateTime::~YKDateTime(void)
{
}YKDateTime YKDateTime::GetLocalTime()
{return YKDateTime(time(NULL));
}YK_BOOL YKDateTime::ValidDateTime()
{YKDateTime dtTemp(1900, 1, 1);return m_time >= dtTemp.m_time;
}YKDateTime YKDateTime::GetDate()
{return YKDateTime(GetYear(), GetMonth(), GetDay());
}YK_INT YKDateTime::GetYear() const
{tm t1;localtime_s(&t1, &m_time);return t1.tm_year + 1900;
}YK_INT YKDateTime::GetMonth() const
{tm t1;localtime_s(&t1, &m_time);return t1.tm_mon + 1;
}YK_INT YKDateTime::GetDay() const
{tm t1;localtime_s(&t1, &m_time);return t1.tm_mday;
}YK_INT YKDateTime::GetHour() const
{tm t1;localtime_s(&t1, &m_time);return t1.tm_hour;
}YK_INT YKDateTime::GetMinute() const
{tm t1;localtime_s(&t1, &m_time);return t1.tm_min;
}YK_INT YKDateTime::GetSecond() const
{tm t1;localtime_s(&t1, &m_time);return t1.tm_sec;
}YK_INT YKDateTime::GetDayOfYear() const
{tm t1;localtime_s(&t1, &m_time);char temp[128];strftime(temp, 128, "%j", &t1);return atoi(temp);
}YK_INT YKDateTime::GetDayOfMonth() const
{tm t1;localtime_s(&t1, &m_time);return t1.tm_mday;
}YK_INT YKDateTime::GetDayOfWeek() const
{tm t1;localtime_s(&t1, &m_time);return t1.tm_wday == 0 ? 7 : t1.tm_wday;
}YK_INT YKDateTime::GetWeekOfYear() const
{tm t1;localtime_s(&t1, &m_time);char temp[128];strftime(temp, 128, "%W", &t1);return atoi(temp)+1;
}YK_INT YKDateTime::GetWeekOfMonth() const
{tm t1;localtime_s(&t1, &m_time);char temp[128];strftime(temp, 128, "%w", &t1);return atoi(temp);
}YK_INT YKDateTime::GetFirstDayOfWeek() const
{YKDateTime dt(m_time);YK_INT dayDiff = GetDayOfWeek()-1;assert(dayDiff > 0);dt.AddDay(-dayDiff);return dt.GetDay();
}YKString YKDateTime::Format( DT_FORM dtf /* = TF_YMDHMS1 */ )
{tm t1;localtime_s(&t1, &m_time);YK_WCHAR temp[128];wcsftime(temp, 128, GetDTFormValue(dtf).c_str(), &t1);return temp;
}YKString YKDateTime::FormatChs( DT_FORM dtf /* = DTF_YMDHMS1 */ )
{tm t1;localtime_s(&t1, &m_time);YKString str;YK_BOOL bDate = false;switch (dtf){case DTF_YMDHMS1:case DTF_YMDHM1:case DTF_YMD1:{str += YKString::Format(t1.tm_year+1900) + L"年";str += YKString::FormatDigit(t1.tm_mon+1, 2) + L"月";str += YKString::FormatDigit(t1.tm_mday, 2) + L"日";bDate = true;}break;}switch (dtf){case DTF_YMDHMS1:case DTF_YMDHM1:case DTF_HMS1:case DTF_HM1:{if (bDate) str += L" ";str += YKString::FormatDigit(t1.tm_hour, 2) + L"时";str += YKString::FormatDigit(t1.tm_min, 2) + L"分";}break;}switch (dtf){case DTF_YMDHMS1:case DTF_HMS1:{str += YKString::FormatDigit(t1.tm_sec, 2) + L"秒";}break;}return str;
}YKString YKDateTime::FormatPostfix(YK_WCHAR postfix)
{YKString str;switch (postfix){case L'S':str = YKString::Format(m_time);break;case L'M':str = YKString::Format(((YK_DOUBLE)m_time)/PER_MINUTE);break;case L'H':str = YKString::Format(((YK_DOUBLE)m_time)/(PER_HOUR*PER_MINUTE));break;case L'D':str = YKString::Format(((YK_DOUBLE)m_time)/(PER_DAY*PER_HOUR*PER_MINUTE));break;default: return str;}str += postfix;return str;
}YKDateTime YKDateTime::ParseDateTime(const YKString& str, DT_FORM tf /* = TF_YMDHMS1 */)
{switch (tf){case DTF_YMDHMS1:{std::vector<YKString> vecDT = str.Parse<YKString>(L" ");if (vecDT.size() == 2){std::vector<YK_ULONG> vecDate = vecDT[0].Parse<YK_ULONG>(L"-");std::vector<YK_ULONG> vecTime = vecDT[1].Parse<YK_ULONG>(L":");if (vecDate.size() == 3 && vecTime.size() == 3){return YKDateTime(vecDate[0], vecDate[1], vecDate[2], vecTime[0], vecTime[1], vecTime[2]);}}}break;case DTF_YMDHMS2:{std::vector<YKString> vecDT = str.Parse<YKString>(L" ");if (vecDT.size() == 2){std::vector<YK_ULONG> vecDate = vecDT[0].Parse<YK_ULONG>(L"/");std::vector<YK_ULONG> vecTime = vecDT[1].Parse<YK_ULONG>(L":");if (vecDate.size() == 3 && vecTime.size() == 3){return YKDateTime(vecDate[0], vecDate[1], vecDate[2], vecTime[0], vecTime[1], vecTime[2]);}}}break;case DTF_YMDHM1:{std::vector<YKString> vecDT = str.Parse<YKString>(L" ");if (vecDT.size() == 2){std::vector<YK_ULONG> vecDate = vecDT[0].Parse<YK_ULONG>(L"-");std::vector<YK_ULONG> vecTime = vecDT[1].Parse<YK_ULONG>(L":");if (vecDate.size() == 3 && vecTime.size() == 2){return YKDateTime(vecDate[0], vecDate[1], vecDate[2], vecTime[0], vecTime[1]);}}}break;case DTF_YMDHM2:{std::vector<YKString> vecDT = str.Parse<YKString>(L" ");if (vecDT.size() == 2){std::vector<YK_ULONG> vecDate = vecDT[0].Parse<YK_ULONG>(L"/");std::vector<YK_ULONG> vecTime = vecDT[1].Parse<YK_ULONG>(L":");if (vecDate.size() == 3 && vecTime.size() == 2){return YKDateTime(vecDate[0], vecDate[1], vecDate[2], vecTime[0], vecTime[1]);}}}break;case DTF_YMD1:{std::vector<YK_ULONG> vecDate = str.Parse<YK_ULONG>(L"-");if (vecDate.size() == 3){return YKDateTime(vecDate[0], vecDate[1], vecDate[2]);}}break;case DTF_YMD2:{std::vector<YK_ULONG> vecDate = str.Parse<YK_ULONG>(L"/");if (vecDate.size() == 3){return YKDateTime(vecDate[0], vecDate[1], vecDate[2]);}}break;case DTF_HMS1:{std::vector<YK_ULONG> vecTime = str.Parse<YK_ULONG>(L":");if (vecTime.size() == 3){return YKDateTime(1970, 1, 1, vecTime[0], vecTime[1], vecTime[2]);}}break;case DTF_HM1:{std::vector<YK_ULONG> vecTime = str.Parse<YK_ULONG>(L":");if (vecTime.size() == 2){return YKDateTime(1970, 1, 1, vecTime[0], vecTime[1]);}}break;case DTF_MDYHMS1:{std::vector<YKString> vecDT = str.Parse<YKString>(L" ");if (vecDT.size() == 2){std::vector<YK_ULONG> vecDate = vecDT[0].Parse<YK_ULONG>(L"-");std::vector<YK_ULONG> vecTime = vecDT[1].Parse<YK_ULONG>(L":");if (vecDate.size() == 3 && vecTime.size() == 3){return YKDateTime(vecDate[2], vecDate[0], vecDate[1], vecTime[0], vecTime[1], vecTime[2]);}}}break;case DTF_MDYHMS2:{std::vector<YKString> vecDT = str.Parse<YKString>(L" ");if (vecDT.size() == 2){std::vector<YK_ULONG> vecDate = vecDT[0].Parse<YK_ULONG>(L"/");std::vector<YK_ULONG> vecTime = vecDT[1].Parse<YK_ULONG>(L":");if (vecDate.size() == 3 && vecTime.size() == 3){return YKDateTime(vecDate[2], vecDate[0], vecDate[1], vecTime[0], vecTime[1], vecTime[2]);}}}break;case DTF_MDYHM1:{std::vector<YKString> vecDT = str.Parse<YKString>(L" ");if (vecDT.size() == 2){std::vector<YK_ULONG> vecDate = vecDT[0].Parse<YK_ULONG>(L"-");std::vector<YK_ULONG> vecTime = vecDT[1].Parse<YK_ULONG>(L":");if (vecDate.size() == 3 && vecTime.size() == 2){return YKDateTime(vecDate[2], vecDate[0], vecDate[1], vecTime[0], vecTime[1]);}}}break;case DTF_MDYHM2:{std::vector<YKString> vecDT = str.Parse<YKString>(L" ");if (vecDT.size() == 2){std::vector<YK_ULONG> vecDate = vecDT[0].Parse<YK_ULONG>(L"/");std::vector<YK_ULONG> vecTime = vecDT[1].Parse<YK_ULONG>(L":");if (vecDate.size() == 3 && vecTime.size() == 2){return YKDateTime(vecDate[2], vecDate[0], vecDate[1], vecTime[0], vecTime[1]);}}}break;case DTF_MDY1:{std::vector<YK_ULONG> vecDate = str.Parse<YK_ULONG>(L"-");if (vecDate.size() == 3){return YKDateTime(vecDate[2], vecDate[0], vecDate[1]);}}break;case DTF_MDY2:{std::vector<YK_ULONG> vecDate = str.Parse<YK_ULONG>(L"/");if (vecDate.size() == 3){return YKDateTime(vecDate[2], vecDate[0], vecDate[1]);}}break;}return 0;
}YKString YKDateTime::GetDTFormValue( DT_FORM dtf )
{YKString str;switch (dtf){case DTF_YMDHMS1:str = L"%Y-%m-%d %H:%M:%S";break;case DTF_YMDHMS2:str = L"%Y/%m/%d %H:%M:%S";break;case DTF_YMDHM1:str = L"%Y-%m-%d %H:%M";break;case DTF_YMDHM2:str = L"%Y/%m/%d %H:%M";break;case DTF_YMD1:str = L"%Y-%m-%d";break;case DTF_YMD2:str = L"%Y/%m/%d";break;case DTF_HMS1:str = L"%H:%M:%S";break;case DTF_HM1:str = L"%H:%M";break;case DTF_MDYHMS1:str = L"%m-%d-%Y %H:%M:%S";break;case DTF_MDYHMS2:str = L"%m/%d/%Y %H:%M:%S";break;case DTF_MDYHM1:str = L"%m-%d-%Y %H:%M";break;case DTF_MDYHM2:str = L"%m/%d/%Y %H:%M";break;case DTF_MDY1:str = L"%m-%d-%Y";break;case DTF_MDY2:str = L"%m/%d/%Y";break;}return str;
}YKDateTime YKDateTime::ParseDateTimeChs( const YKString& str, DT_FORM tf /*= DTF_YMDHMS1*/ )
{YKString::size_type fPos = 0, lPos = 0;std::vector<YK_UINT> vecVal;for (; lPos < str.size(); ++lPos){if (str.IsAlpha(lPos)){YKString strTemp = str.substr(fPos, lPos-fPos);strTemp.TrimLeft();vecVal.push_back(strTemp.Convert<YK_UINT>());fPos = lPos+1;}}switch (tf){case DTF_YMDHMS1:return YKDateTime(vecVal[0], vecVal[1], vecVal[2], vecVal[3], vecVal[4], vecVal[5]);break;case DTF_YMDHM1:return YKDateTime(vecVal[0], vecVal[1], vecVal[2], vecVal[3], vecVal[4]);break;case DTF_YMD1:return YKDateTime(vecVal[0], vecVal[1], vecVal[2]);break;case DTF_HMS1:return YKDateTime(1970, 1, 1, vecVal[0], vecVal[1], vecVal[2]);break;case DTF_HM1:return YKDateTime(1970, 1, 1, vecVal[0], vecVal[1]);break;default: break;}return 0;
}YK_BOOL YKDateTime::IsElapsed( YK_TM val ) const
{YKDateTime dtNow = YKDateTime::GetLocalTime();return (dtNow.m_time-m_time) >= val;
}void YKDateTime::AddMonth( YK_INT month )
{tm t1;localtime_s(&t1, &m_time);YK_INT absMonth = month;if (month < 0)absMonth = abs(month);YK_INT year = absMonth / PER_YEAR;YK_INT mon = absMonth % PER_YEAR;if (month > 0){*this = YKDateTime(t1.tm_year+1900+year, t1.tm_mon+mon, t1.tm_mday, t1.tm_hour, t1.tm_min, t1.tm_sec);}else{*this = YKDateTime(t1.tm_year+1900-year, t1.tm_mon-mon, t1.tm_mday, t1.tm_hour, t1.tm_min, t1.tm_sec);}
}

软件架构设计之Utility模块——DateTime相关推荐

  1. 软件架构设计之系统耦合性拆分

    软件架构设计之系统模块的拆分 基本概念 功能模块 循环依赖问题 模块拆分原则 高内聚性 低耦合性 模块拆分方式 模块拆分示例 业务需求 业务分析 项目原始代码 需求重构 项目代码重构 总结 基本概念 ...

  2. 《软件架构设计》一书目录

    第一部分  软件架构概念与思想篇 1 第1章  解析软件架构概念 3 1.1  软件架构概念的分类 3 1.1.1  组成派 4 1.1.2  决策派 5 1.2  软件架构概念大观 5 1.2.1  ...

  3. SoC嵌入式软件架构设计之中的一个:系统内存需求评估

    内存是SoC(System on Chip,片上系统)集成设计的重要模块.是SoC中成本比重较大的部分.内存管理的软硬件设计是SoC软件架构设计的重要一环,架构设计师必需要在成本和效率中取得平衡,做到 ...

  4. 软件架构设计 温昱著 - 读书笔记

    #读后感#一本实用性很强的架构入门书籍.内容有深有浅,涉及面广,帮我们树立一个架构设计的全局观.  本书已读完,把读后感放在最前面,以下是详细的读书笔记. #读书笔记# 我们将软件架构概念分为两大流派 ...

  5. 软件工程系列教材:软件架构设计实践教程

    第1章 认识软件架构 1.1 软件架构与软件工程 1.1.1 软件产业的工业化与现代化 1.1.2 软件系统的复杂性 1.1.3 克服"软件危机"的进程 1.1.4 现代软件产业发 ...

  6. 《软件架构设计》读书笔记

    前言 春节前后花了将近两个月时间才把<软件架构设计>一书看完.此书紧紧围绕"软件架构设计"这一主题,非常系统地解析了软件架构的概念,阐述了切实可行的软件架构设计方法,给 ...

  7. 软件架构设计(第2版)——程序员向架构师转型必备

    软件架构设计(第2版)--程序员向架构师转型必备 温昱 著 ISBN 978-7-121-17087-4 2012年7月出版 定价:39.00元 16开 256页 宣传语:本书内容务实.技能梳理清晰, ...

  8. 什么是“软件架构设计”(推荐)

    解析软件架构概念 组合派:软件系统的架构将系统描述为计算组件及组件之间的交互. 决策派:架构是一系列重要决策的集合,这些决策与以下内容有关:软件的组织,构成系统的结构元素及其接口的选择,这些元素在相互 ...

  9. SoC嵌入式软件架构设计之二:虚拟内存管理原理、MMU硬件设计及代码分块管理...

    程序的大部分代码都可以在必要的时候才加载到内存去执行,运行完后可以被直接丢弃或者被其他代码覆盖.我们PC上同时跑着很多的应用程序,每个应用程序使用的虚拟地址空间几乎可以整个线性地址空间(除了部分留给操 ...

  10. 何谓成功的软件架构设计

    所谓成功的架构设计,就是设计出的软件架构是高质量的,并且在所花费的时间.技术决策等方面也都满足具体开发情况的要求. 好的软件架构应当具有如下品质: 良好的模块化.每个模块职责明晰,模块之间松耦合,模块 ...

最新文章

  1. 在Ubuntu 14.04 64bit上下载更新x265源码
  2. tf.signal.stft() 短时傅里叶变换的示例
  3. C# Winform下一个热插拔的MIS/MRP/ERP框架14(自动更新)
  4. 注册了一个域名WELAI.NET
  5. Eclipse里代码自动完成 auto completion的快捷键设置
  6. mysql5.7 索引
  7. 细节模拟题:素数回文
  8. sklearn文本聚类分析
  9. 带你认识!通用网络安全开发包(Libdnet)
  10. 《华为研发》阅读 - 15 (分解“满汉全席”,“先谋而后动”)
  11. html怎么把正方形改成圆形,css如何把正方形变成圆形
  12. 初识html5-当当网图书分类页面,html+css静态页面当当网案例
  13. python大作业设计报告_python大作业.doc
  14. ecshop 添加php标签,ecshop模板调用标签大全
  15. arch Linux更新添加源,Arch Linux 更新源(以清华 arch 源为例)
  16. kali中steghide命令工具教程;7z命令详解;kali中base64命令详解
  17. Python爬虫下载视频(梨视频)
  18. 关于ORA-01034和ORA-27101的一种解决方法
  19. 在IDEA中写Python
  20. 一句话证明你是搞python_如何用一句话证明你是程序员?

热门文章

  1. 用 js 写的 WebSocketHeartBeat,心跳检测,断线重连
  2. 通配符星号(*)和问号(?)的区别
  3. 闹钟流程_自考专升本1月份统考粤康码申报流程
  4. 50年过去了,嫦娥五号探月依然不能直播,告诉你三个可能
  5. Freeswitch视频会议远超Telepresence
  6. BAT中一行太长,如何折行
  7. 类似平行宇宙的灵异事件,三个常见的解释
  8. 研究黑洞内部的一种方法
  9. SVN刷新不及时,要手动操作
  10. 由电梯紧急按钮,谈用户体验