How to read the time in C + +

Source: Internet
Author: User

"Summary" This article describes the method of obtaining calendar time under C/s + +, which is different from the Java language, and it seems that there is not a one-time method to obtain the readability of the HH:MM:SS, the common three-step approach is introduced, and the time acquisition of Nanosecond and Microsecond is introduced in this paper.

1, for C language, need to include the header file:

1 #include <sys/time.h>

2, get the date need to get the calendar time, that is, January 1, 1970 00:00:00 to date the number of seconds, in the Linux system for the time_t type, which is equivalent to 1 long type.

Then the time_t is converted into a TM structure, and the TM structure includes data such as minutes, seconds, hours, days, months, and years.

Use Clock_gettime to get the calendar time code as follows:

#include <iostream>#include<sys/time.h>using namespacestd;intMain () {structTimespec tsp; Clock_gettime (Clock_realtime,&TSP); structTM *TMV = gmtime (&tsp.tv_sec); cout<<"Calendar Time:"<<tsp.tv_sec<<Endl; cout<<"seconds in UTC:"<<tmv->tm_sec<<Endl; cout<<"when in UTC:"<<tmv->tm_hour<<Endl;}

Results:
Calendar Time: 1475654852
Seconds in UTC: 32
Time in UTC: 8

There are three ways to get calendar time:

time_t time (time_t *calptr); // accurate to seconds int struct timespec *tsp); // Accurate to nanosecond int gettimeofday (structvoid *restrict tzp); // accurate to microseconds

3, if you need to obtain milliseconds and microseconds, you can not use the above time_t and TM data, Timespec and TIMEVAL two structures are available in C/s + +, where Timespec includes time_t type and nanosecond, Timeval includes time_ Type T and microsecond type.

#include <iostream>#include<sys/time.h>using namespacestd;intMain () {structTimespec tsp; structTimeval TVL; Clock_gettime (Clock_realtime,&TSP); cout<<"type of time_t in Timespec (precision seconds):"<<tsp.tv_sec<<Endl; cout<<"type of nanosecond in Timespec:"<<tsp.tv_nsec<<Endl; Gettimeofday (&tvl,null); cout<<"type of time_t in Timeval (precision seconds)"<<tvl.tv_sec<<Endl; cout<<"types of microseconds in Timeval:"<<tvl.tv_usec<<Endl;}

Results:
time_t type in Timespec (precision seconds): 1475654893
Nanosecond type in Timespec: 644958756
time_t type in Timeval (precision seconds) 1475654893
Type of microsecond in Timeval: 645036

4. Display the current date in an easy-to-read manner, and C + + provides the Strptime function to convert the time_t to a different type of time format, but it's more complex, and here's an example:

#include <iostream>#include<sys/time.h>using namespacestd;intMain () {structTimespec tsp; Clock_gettime (Clock_realtime,&TSP); Charbuf[ -]; structTM *tmp = localtime (&tsp.tv_sec); if(Strftime (BUF, -,"Date and time:%y-%m-%d%h:%m:%s", tmp) = =0) {cout<<"buffer length is too small\n"; }    Else{cout<<buf<<Endl; }} KnotFruit:
Date and time:2016-10-05 16:02:45

5. The relationship between time data type and time function

How to read the time in C + +

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.