OS:Win7 ,Tools:VS2015
DateTime.h
#pragma once struct DateTime { public: unsigned Year; // years since 1900 unsigned short Month; // months since January - [0, 11] unsigned short Day; // day of the month - [1, 31] unsigned short Hour; // hours since midnight - [0, 23] unsigned short Minute; // minutes after the hour - [0, 59] unsigned short Seconds; // seconds after the minute - [0, 60] including leap second //unsigned short DayOfWeek;// days since Sunday - [0, 6] //unsigned short DayOfYear;// days since January 1 - [0, 365] public: //ctor DateTime(unsigned y, unsigned short m, unsigned short d, unsigned short h, unsigned short mi, unsigned short s); DateTime(); public: //Is LeapYear bool IsLeapYear(); //Day Of Week const char* DayOfWeek(); /*Day Of Year*/ int DayOfYear(); public: //Start Elapsed static void Start(); //End Elapsed , return Secs static double End(); private: const char* _WeekDay_CHS[7] = { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" };//Week Of English const char* _WeekDay_ENG[7] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };//Week Of Chinese private: const int DaysOfMonth[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };//Days of Month //const int* _DaysOfMonth = new int[12]{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; private: //静态变量必须在外部初始化 static long Elapsed; };
DateTime.cpp
//localtime 函数 #define _CRT_SECURE_NO_WARNINGS #include <time.h> #include "DateTime.h" //测试 #include <iostream> //初始化静态变量 long DateTime::Elapsed = NAN;//数据丢失警告忽略。 DateTime::DateTime(unsigned y, unsigned short m, unsigned short d, unsigned short h, unsigned short mi, unsigned short s) { Year = y; Month = m; Day = d; Hour = h; Minute = mi; Seconds = s; } DateTime::DateTime() { //结构值 为 1990年0月 起 0:星期日,1:星期一 time_t dt; struct tm* now; time(&dt); now = localtime(&dt); /* 出错误 time_t dt; struct tm* now = { 0 }; localtime_s(now, &dt); */ Year = now->tm_year + 1900; Month = now->tm_mon + 1; Day = now->tm_mday; Hour = now->tm_hour; Minute = now->tm_min; Seconds = now->tm_sec; } bool DateTime::IsLeapYear() { return ((Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0)); } int DateTime::DayOfYear() { int tmp = 0; //取数组长度 int len = (sizeof(DaysOfMonth) / sizeof(DaysOfMonth[0])); //测试 //std::cout << len << std::endl; for (int i = 0; i < Month - 1; i++) { tmp += DaysOfMonth[i]; } if (IsLeapYear()) tmp++; return tmp + Day; } const char* DateTime::DayOfWeek() { /*//方法一:蔡勒公式,公式中的除法取整数。 符号意义: w:星期 eg:{0=星期日,1=星期一,...} c:年数前两位 eg:{2015 c=20} y:年数后两位 eg:{2015 y=15} m:月 eg:{2015年2月28日 m=14,此时y=2014} 注意 1,2月要当成上一年的13,14月计算 d:日 eg:{28日 d=28} ① w=(y + y/4 + c/4 - 2*c + (26 * (m+1))/10 + d - 1) %7 或② w=(c/4 - 2*c + y + y/4 + (13 * (m+1))/5 + d -1) %7 unsigned w, c, y, m, d; c = Year / 100; y = Year % 100; m = Month; if (Month == 1 || Month == 2) { y--; m += 12; } d = Day; w = (c / 4 - 2 * c + y + y / 4 + (13 * (m + 1)) / 5 + d - 1) % 7; return _WeekDay_CHS[w]; */ /*//方法二:基姆拉尔森公式,公式中的除法取整数 符号意义: w:星期 eg:{6=星期日,0=星期一,...} y:年数 eg:{2015 y=2015} m:月 eg:{2015年2月28日 m=14,此时y=2014} 注意 1,2月要当成上一年的13,14月计算 d:日 eg:{28日 d=28} w=(d + 2*m + 3*(m+1)/5 + y + y/4 - y/100 + y/400) %7 unsigned w, y, m, d; y = Year; m = Month; if (Month == 1 || Month == 2) { y--; m += 12; } d = Day; w = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 +1; //std::cout << w << std::endl; return _WeekDay_CHS[w]; */ /*//方法三:基数法,公式中的除法取整数 1.年基数yb: 平年=1, 闰年=2。 2.月基数mb:该月之前(不含该月)的每月天数的总和除以7取余 平年={0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5}, 闰年={0, 3, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6}。 3.符号意义: w:星期 eg:{0=星期日,1=星期一,...} y:年份 eg:{2015年3月4日 y=2015} yb:年基数 eg:{2015年3月4日 yb=1} mb:月基数 eg:{2015年3月4日 mb=3} d:日 eg:{2015年3月4日 d=4} w=(y + y/4 + y/400 - y/100 - yb + mb + d)%7 */ int NLY[12] = { 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; int LY[12] = { 0, 3, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6 }; unsigned y, yb, mb, d, w; if (IsLeapYear()) { yb = 2; mb = LY[Month - 1]; } else { yb = 1; mb = NLY[Month - 1]; } y = Year; d = Day; w = (y + y / 4 + y / 400 - y / 100 - yb + mb + d) % 7; return _WeekDay_CHS[w]; } void DateTime::Start() { Elapsed = clock(); } double DateTime::End() { //测试 /*long tmp = clock() - Elapsed; std::cout << tmp << std::endl;*/ return static_cast<double>(clock() - Elapsed) / CLOCKS_PER_SEC; }
Main.cpp
#include <iostream> #include "DateTime.h" using namespace std; int main() { DateTime::Start(); //DateTime::Now(); //DateTime dt2(1997, 3, 4, 22, 58, 31); DateTime dt2; cout << dt2.Year << "年" << dt2.Month << "月" << dt2.Day << "日" << dt2.Hour << "时" << dt2.Minute << "分" << dt2.Seconds << "秒" << endl; cout << dt2.IsLeapYear() << endl; cout << dt2.DayOfYear() << endl; cout << dt2.DayOfWeek() << endl; cout << DateTime::End() << endl; getchar(); return 0; }