Linux time zone explanation and common time functions
Time and time zone
The whole earth is divided into twenty-four time zones, each of which has its own local time.
We can think that Greenwich Mean Time is the Coordinated Time (GMT = UTC), and Greenwich Mean Time and UTC time are calculated in seconds.
UTC + time zone Difference = local time
The Time Zone difference is positive in the east and negative in the West. Here, the UTC + 8 Zone Time Zone difference is recorded as + 0800
UTC + (+ 0800) = Local (Beijing) Time
The UTC time displayed on the computer is calculated in seconds starting from (0:00:00, January 1, January 01, 1970. The UTC time that we see is the total number of seconds from the time point of January 1, 1970 to the specific time. The number of seconds is the Unix timestamp.
Time (obtain the current time)
Function Description:
# Include <time. h>
Time_t time (time_t * t );
This function returns the number of seconds that have elapsed since January 1, 1970 ad utc time since 00:00:00. If t is not a null pointer, this function also saves the returned value to the memory indicated by t pointer.
Return: the number of seconds is returned for success, and (time_t)-1) is returned for failure. The cause of the error is stored in errno.
Code Description:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <time.h>int main(int argc, char** argv){ int seconds = time(NULL); printf("%d\n", seconds); return 0;}
Execution result:
[Root @ bkjia_CentOS unixtime] # g ++-g-o unixtime_time unixtime_time.cpp
[Root @ bkjia_centos unixtime] #./unixtime_time
1445008165
Gmtime (get current time and date)
Function Description:
# Include <time. h>
Struct tm * gmtime (const time_t * timep );
Gmtime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result from the structure tm.
The structure tm is defined:
Struct tm
{
Int tm_sec;
Int tm_min;
Int tm_hour;
Int tm_mday;
Int tm_mon;
Int tm_year;
Int tm_wday;
Int tm_yday;
Int tm_isdst;
};
Int tm_sec indicates the current number of seconds. The normal range is 0-59, but the value can be 61 seconds.
Int tm_min indicates the current score, range: 0-59
Int tm_hour, which is counted from midnight, ranges from 0 to 23.
The number of days in the current month of int tm_mday, ranging from 01 to 31.
Int tm_mon indicates the current month. The value ranges from 0 to 11 from January 1, January.
Int tm_year: The number of years since January 1, 1900
The number of days in a week for int tm_wday. The value ranges from 0 to 6 from Monday.
Int tm_yday indicates the number of days since January 1, January 1 this year. The value range is 0-365.
Int tm_isdst time saving flag
The time and date returned by this function are not converted by the time zone, but UTC time.
Return: The structure tm indicates the current UTC time.
Code Description:
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <time. h>
Int main (int argc, char ** argv)
{
Const char * wday [] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat "};
Time_t timep;
Struct tm * p;
Time (& timep );
P = gmtime (& timep );
Printf ("curday = % d-% d \ n", (1900 + p-> tm_year), (1 + p-> tm_mon ), p-> tm_mday );
Printf ("curweek = % s, curtime = % d: % d \ n", wday [p-> tm_wday], p-> tm_hour, p-> tm_min, p-> tm_sec );
Return 0;
}
Result description:
[Root @ bkjia_centos unixtime] # g ++-g-o unixtime_gmtime unixtime_gmtime.cpp
[Root @ bkjia_centos unixtime] #./unixtime_gmtime
Curday = 2015-10-16
Curweek = Fri, curtime = 15:12:12
[Root @ bkjia_centos unixtime] # date-u
Fri Oct 16 15:12:13 UTC 2015
[Root @ bkjia_centos unixtime] # date
Fri Oct 16 23:12:16 CST 2015
[Root @ bkjia_centos unixtime] # date-R # the time zone information is printed here. Beijing is the UTC + 8 zone.
Fri, 16 Oct 2015 23:12:18 + 0800
We can see that the time and date returned by gmtime have not been converted by the time zone. The difference between the time and date is exactly 8 hours (China time zone ).
Ctime (Representation of time and date in string format)
Function Description:
# Include <time. h>
Char * ctime (const time_t * timep );
Ctime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result in string form. If you call the related time and date functions again, this string may be damaged.
Code Description:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <time.h>int main(int argc, char** argv){ time_t timep; time(&timep); printf("%s",ctime(&timep)); return 0;}
Result description:
[Root @ bkjia_centos unixtime] # g ++-g-o unixtime_ctime unixtime_ctime.cpp
[Root @ bkjia_centos unixtime] #./unixtime_ctime
Fri Oct 16 23:14:33 2015
[Root @ bkjia_centos unixtime] # date
Fri Oct 16 23:14:34 CST 2015
Asctime (represents the time and date in string format)
Function Description:
# Include <time. h>
Char * asctime (const struct tm * timeptr );
Asctime () converts the information in the tm structure referred to by the timeptr parameter to the time and date representation method used in the real world, and then returns the result in string form. If you call the related time and date functions again, this string may be damaged. The difference between this function and ctime is that the input parameters are different structures.
Code Description:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <time.h>int main(int argc, char** argv){ time_t timep; time(&timep); printf("%s", asctime(gmtime(&timep)));}
Result description:
[Root @ bkjia_centos unixtime] # g ++-g-o unixtime_asctime unixtime_asctime.cpp
[Root @ bkjia_centos unixtime] #./unixtime_asctime
Fri Oct 16 15:15:54 2015
[Root @ bkjia_centos unixtime] # date
Fri Oct 16 23:15:55 CST 2015
[Root @ bkjia_centos unixtime] # date-u
Fri Oct 16 15:15:57 UTC 2015
[Root @ bkjia_centos unixtime] # date-R
Fri, 16 Oct 2015 23:16:01 + 0800
Note that the time of the struct tm structure is returned through gmtime, so it does not undergo time zone conversion.
Gettimeofday (get current time)
Function Description:
# Include <sys/time. h>
# Include <unistd. h>
Int gettimeofday (struct timeval * TV, struct timezone * tz)
Gettimeofday () will return the current time with the structure indicated by TV, and put the information of the local time zone in the structure indicated by tz.
The timeval structure is defined:
Struct timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* microseconds */
};
The timezone structure is defined:
Struct timezone {
Int tz_minuteswest;/* the time difference between Greenwich and */
Int tz_dsttime;/* state of daylight saving time */
};
The above two structures are defined in/usr/include/sys/time. h. The status represented by tz_dsttime is as follows:
DST_NONE/* not used */
DST_USA/* United States */
DST_AUST/* Australia */
DST_WET/* Western Europe */
DST_MET/* Central Europe */
DST_EET/* Eastern Europe */
DST_CAN/* Canada */
DST_GB/* Great Britain */
DST_RUM/* Romania */
DST_TUR/* Turkey */
DST_AUSTALT/* Australia (after January 1, 1986 )*/
Return: 0 is returned for success,-1 is returned for failure, and the error code is stored in errno.
Code Description:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include<sys/time.h>int main(int argc, char** argv){ struct timeval tv; struct timezone tz; gettimeofday(&tv, &tz); printf("tv_sec = %d, tv_usec = %d, tz_minuteswest = %d, tz_dsttime = %d\n", tv.tv_sec, tv.tv_usec, tz.tz_minuteswest, tz.tz_dsttime) ; return 0;}
Result description:
[Root @ VM_174_centos unixtime] # g ++-g-o unixtime_gettimeofday unixtime_gettimeofday.cpp
[Root @ VM_174_centos unixtime] #./unixtime_gettimeofday
TV _sec = 1445008619, TV _usec = 699804, tz_minuteswest =-480, tz_dsttime = 0
[Root @ bkjia_centos unixtime] # date
Fri Oct 16 23:17:00 CST 2015
[Root @ bkjia_centos unixtime] # date-u
Fri Oct 16 15:17:02 UTC 2015
Here, the time zone difference is-480, that is, the GMT value is 8 hours later than our (China time zone.
Localtime (obtain the current local time and date)
Function Description:
# Include <time. h>
Struct tm * localtime (const time_t * timep );
Localtime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result from the tm structure. For the definition of the structure tm, see gmtime (). The time date returned by this function has been converted to the local time zone.
Code Description:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <time.h>int main(int argc, char** argv){ const char* wday[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; time_t timep; struct tm* p; time(&timep); p = localtime(&timep); printf("curday = %d-%d-%d\n", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday); printf("curweek = %s, curtime = %d:%d:%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec); return 0;}
Result description:
[Root @ bkjia_centos unixtime] # g ++-g-o unixtime_localtime unixtime_localtime.cpp
[Root @ bkjia_centos unixtime] #./unixtime_localtime
Curday = 2015-10-16
Curweek = Fri, curtime = 23:23:37
[Root @ bkjia_centos unixtime] #./unixtime_gmtime
Curday = 2015-10-16
Curweek = Fri, curtime = 15:23:37
The result here is compared with the gmtime result. It can be seen that gmtime is the GMT Standard Time, And localtime is the local time converted according to the time zone (Beijing time, UTC + 8, + 0800 ).
Mktime)
Function Description:
Time_t mktime (strcut tm * timeptr );
Mktime () is used to convert the tm structure data referred to by the timeptr parameter to the number of seconds that have elapsed since 00:00:00, January 1, January 1, 1970 AD.
Return: the number of seconds after which the request is returned.
Code Description:
/** Use time () to get the time (in seconds), use localtime () * to convert to struct tm, and then use mktine () convert struct tm to the original number of seconds */# include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <time. h> int main (int argc, char ** argv) {time_t timep; struct tm * p; time (& timep); printf ("time () = % d \ n ", timep); p = localtime (& timep); timep = mktime (p); printf (" time ()-> localtime () -> mktime (): % d \ n ", timep); return 0 ;}
Result description:
[Root @ bkjia_centos unixtime] # g ++-g-o unixtime_mktime unixtime_mktime.cpp
[Root @ bkjia_centos unixtime] #./unixtime_mktime
Time () = 1445010682
Time ()-> localtime ()-> mktime (): 1445010682
Settimeofday (set the current time)
Function Description:
# Include <sys/time. h>
# Include <unistd. h>
Int settimeofday (const struct timeval * TV, const struct timezone * tz );
Settimeofday () sets the current time to the structure information indicated by TV, and the local time zone information to the structure indicated by tz. For more information, see gettimeofday (). Note: Only the root permission can use this function to modify the time.
Return: 0 is returned for success,-1 is returned for failure, and the error code is stored in errno.
The root permission of EPERM is not enough to call settimeofday.
The EINVAL time zone or data is incorrect and the time cannot be set correctly.
This article permanently updates the link address: