// Copyright (c) 2020 Lv Decai, Shenzhen, China.// Filename: TimeUtility.cpp// Version: 1.0// Release date: 2020年10月11日// License: MIT (http://opensource.org/licenses/MIT)#include "StdAfx.h"#include "StringUtility.h"#include "TimeUtility.h"NAMESPACE_BEGIN;// 统一的日期格式static CString s_sDateFormat = TimeUtility::DEFAULT_DATE_FORMAT;// 统一的时间格式static CString s_sTimeFormat = TimeUtility::DEFAULT_TIME_FORMAT;// 统一的日期时间格式static CString s_sDateTimeFormat = TimeUtility::DEFAULT_DATE_TIME_FORMAT;// 统一的日期选择控件显示格式static CString s_sDateTimeCtrlFormat = TimeUtility::DEFAULT_DATE_TIME_CTRL_FORMAT;// 返回统一的日期格式CString TimeUtility::GetDateFormat(){return s_sDateFormat;}// 设置统一的日期格式void TimeUtility::SetDateFormat(PCTSTR sFormat){s_sDateFormat = sFormat;}// 返回统一的时间格式CString TimeUtility::GetTimeFormat(){return s_sTimeFormat;}// 设置统一的时间格式void TimeUtility::SetTimeFormat(PCTSTR sFormat){s_sTimeFormat = sFormat;}// 返回统一的日期时间格式CString TimeUtility::GetDateTimeFormat(){return s_sDateTimeFormat;}// 设置统一的日期时间格式void TimeUtility::SetDateTimeFormat(PCTSTR sFormat){s_sDateTimeFormat = sFormat;}// 返回统一的日期选择控件显示格式CString TimeUtility::GetDateTimeCtrlFormat(){return s_sDateTimeCtrlFormat;}// 设置统一的日期选择控件显示格式void TimeUtility::SetDateTimeCtrlFormat(PCTSTR sFormat){s_sDateTimeCtrlFormat = sFormat;}// 返回统一格式的当前日期字符串CString TimeUtility::GetCurrentDate(){return FormatDate(COleDateTime::GetCurrentTime());}// 返回统一格式的当前日期时间字符串CString TimeUtility::GetCurrentDateTime(){return COleDateTime::GetCurrentTime().Format(s_sDateTimeFormat);}// 按统一格式输出日期字符串CString TimeUtility::FormatDate(const CTime& Date){return Date.Format(s_sDateFormat);}// 按统一格式输出日期字符串CString TimeUtility::FormatDate(const COleDateTime& Date){return Date.Format(s_sDateFormat);}// 按统一格式输出日期字符串CString TimeUtility::FormatDate(CString sDate){sDate.Replace(_T("/"), _T(""));sDate.Replace(_T("-"), _T(""));sDate.TrimLeft();sDate.TrimRight();if (sDate.GetLength() < 8){return sDate;}CString sYear = sDate.Left(4);CString sMonth = sDate.Mid(4, 2);CString sDay = sDate.Mid(6, 2);int iYear = _ttoi(sYear);int iMonth = _ttoi(sMonth);int iDay = _ttoi(sDay);if (iMonth < 1 || 12 < iMonth|| iDay < 1 || 31 < iDay){return sDate;}return FormatDate(COleDateTime(iYear, iMonth, iDay, 0, 0, 0));}// 按统一格式输出时间字符串CString TimeUtility::FormatTime(const CTime& Date){return Date.Format(s_sTimeFormat);}// 按统一格式输出时间字符串CString TimeUtility::FormatTime(const COleDateTime& Date){return Date.Format(s_sTimeFormat);}// 按统一格式输出日期时间字符串CString TimeUtility::FormatDateTime(const CTime& Date){return Date.Format(s_sDateTimeFormat);}// 按统一格式输出日期时间字符串CString TimeUtility::FormatDateTime(const COleDateTime& Date){return Date.Format(s_sDateTimeFormat);}// 解析时间字符串COleDateTime TimeUtility::ParseTime(PCTSTR sTimeString){COleDateTime DateTime;CString sFormatedTimeString;if (StringUtility::IsNumeric(sTimeString)){sFormatedTimeString = FormatDate(sTimeString);if (_tcslen(sTimeString) >= 12){CString sHour(sTimeString + 8, 2);CString sMinute(sTimeString + 10, 2);sFormatedTimeString += _T(" ") + sHour + _T(":") + sMinute;}if (_tcslen(sTimeString) >= 14){CString sSecond(sTimeString + 12, 2);sFormatedTimeString += _T(":") + sSecond;}sTimeString = sFormatedTimeString.GetBuffer(0);}if (DateTime.ParseDateTime(sTimeString)){return DateTime;}else{return InvalidTime;}}// 设置日期选择控件显示格式为统一的日期格式void TimeUtility::SetDateTimeCtrlFormat(CDateTimeCtrl* pDateTimeCtrl){if (pDateTimeCtrl != NULL){pDateTimeCtrl->SetFormat(s_sDateTimeCtrlFormat);}}// 判断一个年份是否闰年BOOL TimeUtility::IsLeapYear(int iYear){return iYear % 4 == 0&& (iYear % 100 != 0 || iYear % 400 == 0);}// 根据年数、月数、天数计算出生日期COleDateTime TimeUtility::CalculateDateOfBirth(int iYears, int iMonths, int iDays){COleDateTime Now = COleDateTime::GetCurrentTime();int Day = iDays > 0 ? iDays : 1;int Month = iMonths > 0 ? iMonths : 1;if (iYears != 0){if (Now.GetYear() - iYears >= 1800){return COleDateTime(Now.GetYear() - iYears, Month, Day, 0, 0, 0);}else{return COleDateTime(1800, 1, 1, 0, 0, 0);}}else if (iMonths != 0){if (Now.GetMonth() > iMonths % 12){return COleDateTime(Now.GetYear() - iMonths / 12, Now.GetMonth() - iMonths % 12, Day, 0, 0, 0);}else{return COleDateTime(Now.GetYear() - iMonths / 12 - 1, Now.GetMonth() + 12 - iMonths % 12, Day, 0, 0, 0);}}else{COleDateTimeSpan TimeSpan(iDays, 0, 0, 0);return Now - TimeSpan;}}// 根据出生日期计算年龄int TimeUtility::CalculateAge(const COleDateTime& DateOfBirth, AGE_UNIT_ENUM iUnit){if (DateOfBirth.GetStatus() != COleDateTime::valid){ASSERT(FALSE);return 0;}COleDateTime Now = COleDateTime::GetCurrentTime();// 如果以天为单位计算,00:00:00 和 23:59:59 都是同一天,需要把当前时间调整到今天的最后一秒来计算,或者把出生日期取整来计算if (iUnit == YEAR|| iUnit == MONTH){Now.SetDateTime(Now.GetYear(), Now.GetMonth(), Now.GetDay(), 23, 59, 59);}// 如果以小时为单位计算,00:00 和 59:59 都是同一小时,需要把当前时间调整到整点的最后一秒来计算else if (iUnit == DAY){Now.SetDateTime(Now.GetYear(), Now.GetMonth(), Now.GetDay(), Now.GetHour(), 59, 59);}else{Now.SetDateTime(Now.GetYear(), Now.GetMonth(), Now.GetDay(), Now.GetHour(), Now.GetMinute(), 59);}if (Now <= DateOfBirth){return 0;}COleDateTimeSpan TimeSpan = Now - DateOfBirth;int iYearSpan = Now.GetYear() - DateOfBirth.GetYear();switch (iUnit){case YEAR:if (Now.GetMonth() > DateOfBirth.GetMonth()|| (Now.GetMonth() == DateOfBirth.GetMonth() && Now.GetDay() >= DateOfBirth.GetDay())){return iYearSpan;}else{return iYearSpan - 1;}case MONTH:if (Now.GetDay() >= DateOfBirth.GetDay()){return iYearSpan * 12 + Now.GetMonth() - DateOfBirth.GetMonth();}else{return iYearSpan * 12 + Now.GetMonth() - DateOfBirth.GetMonth() - 1;}case DAY:return (int) TimeSpan.GetTotalDays();case HOUR:return (int) TimeSpan.GetTotalHours();default:return 0;}}// 返回从开机到当前时间经过的毫秒数,精确到 1 毫秒ULONGLONG TimeUtility::GetUpTime(){static LARGE_INTEGER s_StartUpCounter = { 0 };static LARGE_INTEGER s_Frequency = { 0 };if (s_StartUpCounter.QuadPart == 0){QueryPerformanceCounter(&s_StartUpCounter);QueryPerformanceFrequency(&s_Frequency);s_StartUpCounter.QuadPart -= GetTickCount64() * s_Frequency.QuadPart / 1000;}LARGE_INTEGER CurrentCounter = { 0 };QueryPerformanceCounter(&CurrentCounter);return (CurrentCounter.QuadPart - s_StartUpCounter.QuadPart) * 1000 / s_Frequency.QuadPart;}// 休眠一段时间,在休眠期间可以响应界面事件void TimeUtility::Sleep(UINT iMs){ULONGLONG iStartTime = GetUpTime();MSG Msg;while (GetUpTime() - iStartTime < iMs){if (PeekMessage(&Msg, NULL, NULL, NULL, PM_REMOVE)){TranslateMessage(&Msg);DispatchMessage(&Msg);}}}NAMESPACE_END;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。