Detailed explanation of linux time-related structures and functions

1. Time Concept

1.1 locale time

1.2 Greenwich Mean Time (GMT)

Universal time is the earliest time standard.
In 1884, 1s was determined internationally as 1/8.64×104 of the average daily length throughout the year. The time system formed by this standard is called Universal Time, or UT1.
The International Atomic Time Scale began to be used internationally in 1972. Since then, the time passing through the prime meridian of the Greenwich Old Observatory has been called Universal Time, UT2, or Greenwich Mean Time (GMT).

1.3 Time coordinated time (Universal Time Coordinated UTC)

1.3.1 Reasons for the emergence of UTC

Universal Coordinated Time is a time standard based on the rotation of the earth. Because the earth's rotation speed is not uniform, it is not exactly 86400 atomic s every day, which results in an error of 1 second between the rotation time and universal time in 18 months.
In order to correct this error, the International Earth Rotation Institute adjusted Greenwich Mean Time to increase or decrease leap s according to the actual situation of the Earth’s rotation, and cooperated with the International Bureau of Weights and Measures to issue standard time to the world.
This is the so-called world coordination. Time (UTC: CoordinatdeUniversalTime).

1.3.2 UTC

The representation of UTC is: year (y), month (m), day (d), hour (h), minute (min), second (s), all of which are represented by numbers.
Most of the time written in the computer logs we see in our daily work is calculated in UTC time

2. linux time processing

There are two common storage methods for storing time under Linux. One is how many seconds have passed since 1970, and the other is a structure to store the year, month, day, hour, minute, and second separately.
This type of time_t is used to store the number of seconds that have passed from 1970 to the present. To be more precise, you can use the structure struct timeval, which is accurate to subtle.

2.1 time_t

(1) time_t is a long integer, generally used to indicate the number of seconds since 1970.

Generally get the number of seconds through time_t time = time(NULL);

2.1.1 time_t code example 1

#include <time.h>
#include<stdio.h>

int main()
{
	time_t timep;
	time(&timep); /*当前time_t类型UTC时间*/
    printf("time():%d\n",(int)timep);

	time(&timep); /*获取time_t类型的当前时间*/
	/*用 gmtime 将time_t类型的时间转换为struct tm类型的时间按,//没有经过时区转换的UTC时间
	然后再用 asctime 转换为我们常见的格式 Fri Jan 11 17:25:24 2008
	*/
	printf("%s", asctime(gmtime(&timep)));
	return 0;
}

结果:
time():1597287548
Thu Aug 13 01:36:00 2020

Description:
struct tm* gmtime(const time_t *timep);
converts the time represented by time_t to UTC time without time zone conversion, which is a struct tm structure pointer

char asctime(const struct tm timeptr);
Convert the information in the structure to real-world time and display it as a string

2.1.2 time_t code example 2

#include <time.h>
#include<stdio.h>

int main()
{
	time_t timep;

	time(&timep); /*获取time_t类型当前时间*/   
	/*转换为常见的字符串:Fri Jan 11 17:04:08 2008*/
	printf("%s", ctime(&timep));
	return 0;
}

Thu Aug 13 10:29:57 2020

char *ctime(const time_t *timep);
Convert timep to real world time, display it as a string, and the parameter of asctime is tm, the difference lies in the form of the incoming parameter

2.1.3 time_t code example 3

#include <time.h>
#include<stdio.h>

int main()
{
	time_t timep;
	struct tm *p;

	time(&timep); /*当前time_t类型UTC时间*/
	printf("time():%d\n",timep);
	p = localtime(&timep); /*转换为本地的tm结构的时间按*/
	timep = mktime(p); /*重新转换为time_t类型的UTC时间,这里有一个时区的转换*/ //by lizp 错误,没有时区转换, 将struct tm 结构的时间转换为从1970年至p的秒数
	printf("time()->localtime()->mktime(): %d\n", timep);
	printf("%s", ctime(&timep));
	return 0;
}

结果: 
time():1597287634
time()->localtime()->mktime(): 1597287634
Thu Aug 13 11:02:33 2020

2.2 timeval

struct timeval has two members, one is seconds and the other is subtle.

struct timeval 
{
	long tv_sec; /* seconds */
	long tv_usec; /* microseconds  微秒 */
};


Obtained by int gettimeofday(struct timeval *tv, struct timezone *tz);. gettimeofday //Get the number of seconds and microseconds from 1970

2.2.1 timeval code example

#include <time.h>
#include<stdio.h>
#include<sys/time.h>

int main()
{
	time_t timep;
	struct tm *p;

	time(&timep); /*当前time_t类型UTC时间*/
	printf("time():%d\n",(int)timep);
	
	struct timeval us;
	gettimeofday(&us,NULL);
	printf("gettimeofday: tv_sec=%ld, tv_usec=%ld\n", us.tv_sec, us.tv_usec);

	return 0;
}

结果: 
time():1597288484
gettimeofday: tv_sec=1597288484, tv_usec=430228

2.3 timespec

struct timespec has two members, one is seconds and the other is nanoseconds, so the highest accuracy is nanoseconds.

struct timespec
{
	time_t tv_sec; /* seconds */
	long tv_nsec; /* nanoseconds */  //纳秒
};

Generally obtained by the function long clock_gettime (clockid_t which_clock, struct timespec *tp);.

2.3.1 timespec code example

#include <time.h>
#include<stdio.h>
#include<sys/time.h>

int main()
{
	time_t timep;
	struct tm *p;

	time(&timep); /*当前time_t类型UTC时间*/
	printf("time():%d\n",(int)timep);
	
	struct timespec ts;
	clock_gettime(CLOCK_REALTIME, &ts);
	printf("clock_gettime : tv_sec=%ld, tv_nsec=%ld\n", ts.tv_sec, ts.tv_nsec);

	return 0;
}

结果: 
time():1597288411
clock_gettime : tv_sec=1597288411, tv_nsec=419949167

2.4 tm //Get detailed time format

This time of tm structure is expressed as decomposition time.
Date and time can be obtained through tm structure

struct tm
{
	int tm_sec;  /*秒,正常范围0-59, 但允许至61*/
	int tm_min;  /*分钟,0-59*/
	int tm_hour; /*小时, 0-23*/
	int tm_mday; /*日,即一个月中的第几天,1-31*/
	int tm_mon;  /*月, 从一月算起,0-11*/  1+p->tm_mon;
	int tm_year;  /*年, 从1900至今已经多少年*/  1900+ p->tm_year;
	int tm_wday; /*星期,一周中的第几天, 从星期日算起,0-6*/
	int tm_yday; /*从今年1月1日到目前的天数,范围0-365*/
	int tm_isdst; /*日光节约时间的旗标*/
};

3. Time related interface description

3.1 gmtime //Convert the time represented by time_t to UTC time without time zone conversion

struct tm* gmtime(const time_t *timep);
converts the time represented by time_t to UTC time without time zone conversion, which is a struct tm structure pointer

3.2 asctime //Convert tm information into real-world time and display it as a string

Description:
#include <time.h>
char asctime(const struct tm timeptr);
Convert the information in the structure to real-world time, and display it in the form of a string

3.3 ctime //time_t is converted to a common string

char *ctime(const time_t *timep);
Convert timep to real world time, display it as a string, it is different from asctime in the form of the passed parameters

3.4 difftime //The number of seconds between two times

double difftime(time_t time1, time_t time2);
returns the number of seconds between two times

3.5 gettimeofday //Get the number of seconds and microseconds from 1970

int gettimeofday(struct timeval *tv, struct timezone *tz);
returns the current number of seconds and microseconds from 1970, the latter tz is the time zone, generally not

3.6 localtime //Time after time zone conversion

stuct tm* localtime(const time_t *timep);
Similar to gmtime, but it is the time after time zone conversion.

3.7 mktime //Convert the time of the struct tm structure to the number of seconds from 1970 to the present

time_t mktime(struct tm* timeptr);
Convert the time of the struct tm structure to the number of seconds from 1970 to the present

reference:

https://www.runoob.com/w3cnote/cpp-time_t.html
https://www.cnblogs.com/hushaojun/p/7990951.html

Guess you like

Origin blog.csdn.net/lqy971966/article/details/107975594