Use and conversion of 64-bit time and month-date time

Source: Internet
Author: User

Usually in our program, we need to use the date of the month and day type of time, also need to use 64-bit integer time, such as storage in the database (the corresponding Time field in the database type is the big int data type). The code will refer to how to get the current time, as well as the conversion problem between the month-date time and the 64-bit time. Below on the actual situation of their own, do a small summary.

1. Get the current time

There are a number of times to get the current time, and here are three common ways to get the current time using the time function, using the CTime class, and using the SystemTime struct.

(1) Use the time function to get the current times

This function should contain the time.h header file, which is easiest to use to get the current 64-bit time.

    __time64_t Curtime =:: Time (NULL);  
This method is more practical in the thread function, can be controlled in time, usually the threading function is a loop body, at the beginning of each cycle, we will get the current time, such as:

(i) In a client thread, on the one hand to the server to send the heartbeat packet data (if every 10 seconds), on the other hand, and other data to send and receive, and this data to send and receive in real time, so in this thread can not wait 10 seconds after the heartbeat packet, and then sent to receive other data, At this point, we can define a __time64_t type of time variable, recording the last time the heartbeat packet was sent, so that in the loop of the thread function, each loop only has to wait for a minute to enter the next loop, In real time to send and receive data while determining whether the current time interval from the last heartbeat packet has reached 10 seconds, if arrived to send a new heartbeat packet, and update the last heartbeat packet sent time.

(ii) In the video surveillance system, the concept of a planned video, how is the video plan implemented? How is the dispatch done? Here is a brief explanation, open a video scheduling thread, in the thread to traverse the list of recorded objects, the object contains the recording schedule start time and end time, when the current time is greater than or equal to the beginning of recording time, the recording begins, when the current time is greater than or equal to the end of the recording time, the end of the recording.

(2) Use the CTime class to get the current time

Because this method is used to CTime MFC classes, it can only be applied to MFC projects. First define a CTime class object, then Tune Ctime::getcurrenttime () to get the current time, and finally use getyear (), GetMonth (), GetDay (), Gethour (), Getminute () and Getsecond () and other functions get specific date and time information.

    CTime curtime;      curtime = Ctime::getcurrenttime ();      CString Strcurtime;      Strcurtime.format (_t ("%04d/%02d/%02d%02d:%02d:%02d"), Curtime.getyear (), Curtime.getmonth (), CurTime.GetDay (),              Curtime.gethour (), Curtime.getminute (), Curtime.getsecond ());      ...//Show the time  

(3) using SYSTEMTIME structure to get the current time

Similar to the CTime class, a struct object is defined, then the Getlocaltime function is called to get the current time, and the members of the struct object contain date information. Unlike the CTime class, the method can be applied with MFC engineering and also in Win32 engineering.

    SYSTEMTIME Curtime;      Getlocaltime (&curtime);      CString Strcurtime;      Strcurtime.format (_t ("%04d/%02d/%02d%02d:%02d:%02d"), Curtime.wyear             , Curtime.wmonth, Curtime.wday, Curtime.whour, Curtime.wminute, curtime.wsecond);      ...//show the current time  


2, Month day time and 64-bit time of mutual transformation

64-bit time to save to the database is very convenient, before a lot of people have asked how to save the month and day time to the database, I think they converted to 64-bit time after saving to the database is better, as long as there is a big int time field in the database can be, so not only simplifies the time saving method, It also facilitates sorting queries on data tables. Data in the interface to be saved to the database, to convert it to 64 time, from the database read 64 is the time, to convert it to month and day time on the interface display.

(1) using the CTime class to achieve the conversion of month-date time and 64-bit time

(i) Month and day time converted to 64-bit time: When constructing the CTime class object, the month-date information is used as the construction parameter.

    CTime Thattime (+, +, +);      __time64_t thattime_64t = Thattime.gettime ();  
(ii) 64-bit time conversion to month-date time: When constructing the CTime class object, the 64-bit time is used as the construction parameter.
    __time64_t Tahttime;      ...//intermediate processing, get 64-bit time      CTime ctthattime (tahttime);      CString Strthattime;      Strthattime.format (_t ("%04d/%02d/%02d%02d:%02d:%02d"), Ctthattime.getyear (), Ctthattime.getmonth (), Ctthattime.getday (),             ctthattime.gethour (), Ctthattime.getminute (), Ctthattime.getsecond ());      ......  
The above method is suitable for MFC engineering, and the TM structure is used in non-MFC engineering.

(2) Using TM structure to achieve the conversion between month day time and 64 bit time

The definition of a TM struct is as follows:

struct TM {        int tm_sec;     /* seconds after the minute-[0,59] */        int tm_min;     /* minutes after the hour-[0,59] */        int tm_hour;    /* Hours since midnight-[0,23] */        int tm_mday;    /* Day of the month-[1,31] */        int tm_mon;     /* months since January-[0,11] */        int tm_year;    /* years since 1900 */        int tm_wday;    /* Days since Sunday-[0,6] */        int tm_yday;    /* Days since January 1-[0,365] */        int tm_isdst;   /* Daylight Savings Time flag */        };

(i) Month and day time conversion to 64-bit time: First use the SYSTEMTIME structure to obtain the current time, and then use the TM structure will be obtained from month to day time to 64-bit time.

    SYSTEMTIME Curtime;      Getlocaltime (&curtime);      struct TM Tmtime;      Tmtime.tm_year = curtime.wyear-1900;      Tmtime.tm_mon = Curtime.wmonth;      Tmtime.tm_mday = Curtime.wday;      Tmtime.tm_hour = Curtime.whour;      Tmtime.tm_min = Curtime.wminute;      Tmtime.tm_sec = Curtime.wsecond;      __time64_t curtime_64t = _mktime64 (&tmtime);   
(ii) 64-bit time conversion to month-date time: First convert 64-bit time to TM structure time, and then convert to month-date time.
    __time64_t Tahttime;      ...//Get 64 bit time      tm *tmthattime = _localtime64 (&thattime);      Char cthattime[30];      memset (cthattime, 0,);      sprintf (Cthattime, "%04d/%02d/%02d%02d:%02d:%02d", tmthattime->tm_year+1900, Tmthattime->tm_mon+1,           Tmthattime->tm_mday, Tmthattime->tm_hour, Tmthattime->tm_min, tmthattime->tm_sec);      ......  

Use and conversion of 64-bit time and month-date time

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.