"Thinkinginc++" 48, using the time function in standard C library to generate a simple class

Source: Internet
Author: User

Cpptime.h


/*** book: "thinkinginc++" * Function: Generate a simple time class with the temporal function in the standard C library * Time: September 11, 2014 07:53:56* Author: cutter_point*/#ifndef cpptime_h_ Included#define cpptime_h_included#include<ctime> #include <cstring>//here CTime and CString are not including using  namespace Std; h is in C, which contains the using namespace std;/* and the direct storage date is a structure: struct tm{int tm_sec;  /* seconds, normal range 0-59, but allowed to 61* int tm_min; /* minutes, 0-59* int tm_hour; /* hours, 0-23* int tm_mday;  /* Day, that is, the first day of the one months, 1-31* int Tm_mon;    /* month, starting from January, 0-11*//*1+p->tm_mon;  int tm_year;    /* years, from 1900 to now how many years * 1900+ p->tm_year; int tm_wday; /* Week, Day of the week, starting from Sunday, 0-6* int tm_yday; /* FROM January 1 this year to the current number of days, range 0-365* int tm_isdst;  /* Daylight saving Time flag *};*/#include <iostream>class time{std::time_t t;  time_t is a type time_t this type is used to store the number of seconds from 1970 to now Std::tm Local;    TM is a struct char asciirep[26];    unsigned char Lflag, aflag;            void Updatelocal () {if (!lflag) {/* struct tm* gmtime (const time_t *TIMEP); Converts the time represented by time_t to a time that has not elapsed time zone conversion, is a struct TMstruct pointer, struct tm* localtime (const time_t *TIMEP);            Similar to Gmtime, but it is time-zone-converted.            time_t This type is used to store the number of seconds from 1970 to now, this function is to convert the second to the time we can understand * * Local=*std::localtime (&AMP;T);        ++lflag;            }} void Updateascii () {if (!aflag) {updatelocal ();            /* Char *asctime (const struct tm* timeptr);        Converts the information in the structure to the real-world time, displaying strcpy (char*, string/char*) as a string, copying the second string to the first top *// std::cout<< "???"            <<std::endl;            std::strcpy (Asciirep, Std::asctime (&local));        ++aflag;    }}public:time () {mark ();}        void Mark () {lflag=aflag=0;  Std::time (&t);    Get the system Time time_t times (time_t *t), and get the number of seconds since January 1, 1970.        } const char* ASCII ()//Returns a const char* type of data {updateascii ();    return asciirep;  } int Delta (time* DT) const//This const indicates that the function cannot change the member data of the class {      /* Oubledifftime (time_t time1, time_t time2);    Returns the number of seconds in a two-time difference */return INT (std::d ifftime (T, dt->t));        } int daylightsaving () {updatelocal (); int tm_isdst;    Daylight saving time flag return LOCAL.TM_ISDST;  }/* and the direct storage date is a structure: struct tm{int tm_sec;  /* seconds, normal range 0-59, but allowed to 61* int tm_min; /* minutes, 0-59* int tm_hour; /* hours, 0-23* int tm_mday;  /* Day, that is, the first day of the one months, 1-31* int Tm_mon;    /* month, starting from January, 0-11*//*1+p->tm_mon;  int tm_year;    /* years, from 1900 to now how many years * 1900+ p->tm_year; int tm_wday; /* Week, Day of the week, starting from Sunday, 0-6* int tm_yday; /* FROM January 1 this year to the current number of days, range 0-365* int tm_isdst;        /* Daylight saving time flag *};*/int dayofyear () {//int tm_yday;/* FROM January 1 this year to the current number of days, Range 0-365* updatelocal ();    return local.tm_yday;        } int DayOfWeek () {//int tm_wday;/* week, Day of the week, starting from Sunday, 0-6* updatelocal ();    return local.tm_wday;  } int since1900 () {//int tm_year; /* years, from 1900 to now how many years * 1900+ p->tm_year;       Updatelocal ();    return local.tm_year;  } int month () {//int tm_mon;        /* month, starting from January, 0-11*//*1+p->tm_mon;        Updatelocal ();    return Local.tm_mon;        } int DayOfMonth () {//int tm_mday;/* day, that is, the first day of the one month, 1-31 updatelocal ();    return local.tm_mday;        } int Hour () {//int tm_hour;/* hours, 0-23* updatelocal ();    return local.tm_hour;  } int minute () {//int tm_min;        /* minutes, 0-59* updatelocal ();    return local.tm_min;  }//int tm_sec;        /* seconds, normal range 0-59, but allowed to 61* int second () {updatelocal ();    return local.tm_sec; }}; #endif//cpptime_h_included


Cpptime.cpp


/*** book: "thinkinginc++" * Function: Test C + + Time class * Time: September 11, 2014 07:54:38* Author: cutter_point*/#include "Cpptime.h" #include < Iostream>using namespace Std;int Main () {time    start;    for (int i=1; i<5000; ++i)    {        cout<<i<< ";        if (i%10 = = 0)            cout<<endl;    }    Time End;    cout<<endl;    cout<< "start =" <<start.ascii () <<endl;    cout<< "end =" <<end.ascii () <<endl;    cout<< "Delta =" <<end.delta (&start) <<endl;    return 0;}






"Thinkinginc++" 48, using the time function in standard C library to generate a simple class

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.