C language note: time.h standard library operating time-related issues

Original link: http://c.biancheng.net/c/

time.h files associated with the time data types and functions

Time.h C language header files, the definition of a date and time data types and functions. There are two ways in C language indicates date and time values: time_t type and type struct tm.

The following is a common theme time.h time data type header file:

typedef long clock_t;
typedef _int32 _time32_t;
typedef _time32_t time_t;
struct tm {
    int tm_sec;				//秒取值区间为【0,59】
    int tm_min;				//分取值区间为【0,59】
    int tm_hour;			//时取值区间为【0,23】
    int tm_mday;			//一个月中的日期取值区间为【1,31】
    int tm_mon;				//月份取值区间为【0.11】(0表示1月,所以编程中常常对其加1)
    int tm_year;			//年份,其值等于实际年份减去1900(实际年份需要对其加1900)
    int tm_wday;			//星期取值区间为【0,6】,其中0表示星期天,1表示星期一
    int tm_yday;			//从每年1月1日开始的天数,其中0表示1月1日
    int tm_isdst;			//夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。
  };

The following are common time time.h header file function prototype screenshot:
First,

time_t __cdecl time(time_t *_Time);//返回从1970-1-1,00:00:00到现在经历的秒数

time_t value is a type of long (typedef renamed), generally can not determine the specific year, month, date and other data according to its value. But can () function to get the current calendar time time_t types of computer systems through time. time () is passed a pointer variable time_t, the contents of the variable bit number of seconds after operation becomes. Of course, it can also return a number of seconds the same value is assigned to another content of time_t variable contents of two variables are equal.

( Calendar time: with "standard time point from a time at that point to the number of seconds elapsed" time expressed in calendar time (Calendar Time) is represented by time_t data type, the time indicated by time_t (calendar time ) from a point in time (e.g.: seconds at 0:00:00 on January 1, 1970) to the case. )

two,

struct tm *__cdecl localtime(const time_t *_Time)

The time () Returns the value of the number of seconds time conversion costs, taking into account the local time zone and daylight savings time flag. The above time () function is a random variable takes the address of time_t passed as arguments of the function, and returns a pointer to a structure of type struct tm pointer type, then using the pointer member Fischer operation "->" taken - in - day hour: minutes: seconds

three,

char *__cdecl asctime(const struct tm *_Tm)//将结构体指针*_Tm所指向的结构体转换为字符串形式

versus

char *__cdecl ctime(const time_t *_Time)//将time_t类型指针*_Tm指向的日历时间转换为字符串形式

Achieve the same effect: it is converted into a string of time, two equivalent functions.
The difference is: asctime () function is passed the argument localtime () return value; the ctime () function is passed the parameter time () return value.

four,

struct tm *__cdecl gmtime(const time_t *_Time)//返回的时间日期未经时区转换,是UTC时间(又称世界时间,即格林尼治时间)

And localtime () function the same, argument passed time () returns a pointer of type struct tm, on the basis of the structure required function directed into the asctime string (), then print output.

Code examples

#include <stdio.h>
#include <time.h>//定义了日期和时间的数据类型以及函数 
int main(void)
{
	time_t now,t1;
	t1=time(&now);
	printf("当前的日历时间:%u秒(距离1970年1月1日0时的秒数)\n",t1);
	
	struct tm *localtp;
	localtp=localtime(&now); 
	printf("本地时间是:%d年%d月%d日 %d:%d:%d\n",localtp->tm_year+1900,localtp->tm_mon+1,localtp->tm_mday,localtp->tm_hour,localtp->tm_min,localtp->tm_sec);
	printf("以asctime函数呈现本地时间:%s",asctime(localtp));
	printf("以ctime函数呈现本地时间:%s",ctime(&now));
	
	struct tm *gmtp;
	gmtp=gmtime(&now);
	printf("UTC时间:%s",asctime(gmtp));
	return 0;
}

Output:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_42124234/article/details/101974116