C + + DateTime structure

Source: Internet
Author: User

Os:win7, tools:vs2015

DateTime.h

#pragmaOncestructdatetime{ Public: Unsigned year; //years since 1900Unsigned ShortMonth;//months since January-[0, one]Unsigned ShortDay;//Day of the month-[1]Unsigned ShortHour;//hours since midnight-[0, 23°c]Unsigned ShortMinute;//minutes after the hour-[0, +]Unsigned ShortSeconds;//seconds after the minute-[0, $] including leap second//unsigned short DayOfWeek;//Days since Sunday-[0, 6]//unsigned short dayofyear;//Days since January 1-[0, 365] Public:    //ctorDateTime (unsigned y, unsigned ShortM, unsigned ShortD, unsigned ShortH, unsigned ShortMI, unsigned Shorts); DateTime (); Public:    //is leapyear    BOOLisleapyear (); //Day of Week    Const Char*DayOfWeek (); /* Day of the year*/    intdayofyear (); Public:    //Start Elapsed    Static voidStart (); //End Elapsed, return Secs    Static DoubleEnd ();Private:    Const Char* _weekday_chs[7] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};//Week of 中文版    Const Char* _weekday_eng[7] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};//Week of ChinesePrivate:    Const intdaysofmonth[ A] = { to, -, to, -, to, -, to, to, -, to, -, to};//Days of Month//const int* _daysofmonth = new int[12]{ , +,------------------Private:    //static variables must be initialized externally    Static LongElapsed; };
View Code

DateTime.cpp

//localtime function#define_crt_secure_no_warnings#include<time.h>#include"DateTime.h"//Test#include <iostream>//Initializing static variablesLongdatetime::elapsed = NAN;//data loss warning ignored. DateTime::D atetime (unsigned y, unsigned ShortM, unsigned ShortD, unsigned ShortH, unsigned ShortMI, unsigned Shorts) { Year=y; Month=m; Day=D; Hour=h; Minute=mi; Seconds=s;} DateTime::D atetime () {//structure value from 19900月 to 0: Sunday, 1: Mondaytime_t DT; structtm*Now ; Time (&DT); now= LocalTime (&DT); /*out error 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;}BOOLdatetime::isleapyear () {return(Year%4==0&& Year% -!=0) || (Year% -==0));}intDateTime::D ayofyear () {intTMP =0; //take array length    intLen = (sizeof(Daysofmonth)/sizeof(daysofmonth[0])); //Test//std::cout << len << Std::endl;     for(inti =0; I < Month-1; i++) {tmp+=Daysofmonth[i]; }    if(Isleapyear ()) TMP++; returnTMP +Day ;}Const Char*DateTime::D ayofweek () {/*//Method One: Caille formula, the division in the formula takes an integer.                    Symbolic Meaning: w: Week eg:{0= Sunday, 1 = Monday, ...} C: Number of years ago two eg:{2015 c=20} y: two bits eg:{2015 y=15} m: Month eg:{2015 year February 28 m=14, at this time y=2014} note      1, February to be treated as 13 of the previous year, 1 April calculation D: Day eg:{28 day d=28}①w= (y + y/4 + C/4-2*c + (m+1))/10 + d-1)%7    or ②w= (C/4-2*c + y + y/4 + (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 + (* (M + 1))/5 + d-1)% 7;    return _WEEKDAY_CHS[W]; */    /*//Method Two: Kimlarsson formula, the division in the formula takes the integer symbolic meaning: w: Week eg:{6= Sunday, 0 = Monday, ...} Y: Number of years eg:{2015 y=2015} m: Month eg:{2015 February 28 m=14, at this time y=2014} Note 1, February as 13 of the previous year, 1 April calculation D: Day    Eg:{28 Day d=28} w= (d + 2*m + (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]; */    /*//Method Three: Cardinal method, the division in the formula takes an integer 1. The annual radix YB: Common year = 1, Leap year = 2.  2. Monthly base MB: the sum of the number of days before the month (excluding the month) divided by 7 for Yu ping years ={0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5}, Leap year ={0, 3, 4, 0, 2, 5, 0, 3,        6, 1, 4, 6}.            3. Symbolic Meaning: w: Week eg:{0= Sunday, 1 = Monday, ...} Y: Year eg:{2015 March 4 y=2015} YB: Year base eg:{2015 year March 4 yb=1} MB: Month base eg:{2015 year March 4 mb=3} d: Day eg:{ March 4, 2015 d=4} w= (y + y/4 + y/400-y/100-yb + MB + d)%7*/    intnly[ A] = {0,3,3,6,1,4,6,2,5,0,3,5 }; intly[ A] = {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/ --Y/ --YB + MB + d)%7; return_weekday_chs[w];}voidDatetime::start () {Elapsed=clock ();}DoubleDatetime::end () {//Test    /*Long tmp = Clock ()-Elapsed; Std::cout << tmp << Std::endl;*/    returnstatic_cast<Double> (Clock ()-Elapsed)/clocks_per_sec;}
View Code

Main.cpp

#include <iostream>#include"DateTime.h"using namespacestd;intMain () {Datetime::start (); //Datetime::now (); //DateTime DT2 (1997, 3, 4,.);DateTime DT2; cout<< DT2. Year <<"years"<< DT2. Month <<"Month"<< DT2. Day <<"Day"<< DT2. Hour <<"when"<< DT2. Minute <<"points"<< DT2. Seconds <<"seconds"<<Endl; cout<< DT2. Isleapyear () <<Endl; cout<< DT2. DayOfYear () <<Endl; cout<< DT2. DayOfWeek () <<Endl; cout<< datetime::end () <<Endl;    GetChar (); return 0;}

C + + DateTime structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.