A brief talk on time_t function of C + +

Source: Internet
Author: User
Tags cst time local time wordpress blog

WordPress Blog Links:http://acmerbar.com/132.html

Recently turned to "C expert programming," the preface to see time_t function, unexpectedly, there is no concept, just about know is a function of time, this has learned C + + in the blogger, it is a taunt, this machine tested a bit, probably the following look:

#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include < Time.h>using namespace std;/* commonly used time functions: *////converts information in the structure to real-world time, displays Char *asctime as a string (const struct tm* timeptr);/// Converts TIMEP to real-world time, displayed as a string, and asctime the incoming parameter is not the same as Char *ctime (const time_t *TIMEP);///returns the number of seconds for two time difference double difftime (time_t Time1, time_t time2);////Returns the number of seconds and subtleties of the current distance from 1970, and the subsequent TZ is the time zone, generally without an int get_time_of_day (struct timeval *tv,struct timezone *tz); Converting the time represented by time_t to UTC time without time zone conversion is a struct TM struct pointer struct tm* gmtime (const time_t *TIMEP);///is similar to gmtime, but it is time-zone-converted. struct tm* localtime (const time_t *TIMPE);///Convert the time of the struct TM structure to the number of seconds from 1970 to the present time_t mktime (struct tm* timeptr);// Gets the number of seconds since January 1, 1970. time_t time (time_t *t); int main () {/* Run the program with the system command date to display the local times as follows: Thu, 12:53:13 2015*/time_t biggest= 0x7fffffff;    /time_t The maximum value that can be represented by printf ("biggest=%s \ n", CTime (&biggest));   printf ("biggest=%s \ n", Asctime (Gmtime (&biggest))); /* Output: biggest= Tue Jan 19 11:14:07 2038       biggest= Tue Jan 19 03:14:07 2038 We found a full 8 hour difference, because the CTime () function converts the parameter to local time, which is not the same as UTC (Greenwich Mean Time), depending on the time zone in which it is located, referring to the function above    Note: the Gmtime () function converts the time represented by time_t to UTC time without time zone conversion, is a struct TM structure pointer */* can test the following two examples */time_t TIMEP;  Time (&AMP;TIMEP);    Gets the current time of the time_t type printf ("%s\n", Asctime (Gmtime (&AMP;TIMEP)));   Output: Thu Nov 26 04:44:40 2015//UTC time time_t TIMEP without time zone conversion;   Time (&AMP;TIMEP);   printf ("%s\n", CTime (&AMP;TIMEP)); Output: Thu Nov 26 12:53:13 2015//UTC Time After time zone conversion return 0;
After other tests, we can summarize:
The time represented by Time,gmtime,asctime is UTC time, but the data type is different;
The time that Localtime,ctime represents is the time after the time zone is converted, and it should be consistent with the CST time that you represented with the system command date.

A brief talk on time_t function of 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.