C标准库-时间函数

时间函数

time函数

c 复制代码
time_t time(time_t* tloc);

功能:获取自1970-01-01 00:00:00 到当前的秒数

tloc:若传入非空指针,函数会把"秒数"结果写入变量中,若传入NULL函数仅返回秒数,但不写入变量。

返回值:成功返回秒数,失败返回错误码并设置errno

localtime函数

c 复制代码
struct tm* localtime(const time_t* timep)

功能:将time_t的秒数转换为tm结构体

timp:秒钟变量的地址

返回值:成功返回tm结构体,失败返回NULL并设置相应的错误码

tm结构体

c 复制代码
struct tm {
	int tm_sec; //秒(0-60)
	int tm_min; //分(0-59)
	int tm_hour; //小时(0-23)
	int tm_mday; //日(1-31)
	int tm_mon; //月(0-11)
	int tm_year; //年(Year - 1900)
	int tm_wday; //星期(0-6 Sunday=0)
	int tm_yday; //年中的第几天(0-365 1 Jan = 0)
	int tm_isdst; //夏令时
};

实例

c 复制代码
#include <time.h>
#include <stdio.h>

int main(void)
{
	time_t now = time(0);
	struct tm* tstruct = localtime(&now);
	fprintf(stdout,"Current time:%d\n", &now);
	fprintf(stdout,"%04d-%02d-%02d %02d:%02d:%02d\n",
		tstruct->tm_year + 1900, tstruct->tm_mon, tstruct->tm_mday,
		tstruct->tm_hour, tstruct->tm_min, tstruct->tm_sec);

	return 0;
}

运行结果:

相关推荐
祈安_1 天前
C语言内存函数
c语言·后端
norlan_jame3 天前
C-PHY与D-PHY差异
c语言·开发语言
czy87874753 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
m0_531237173 天前
C语言-数组练习进阶
c语言·开发语言·算法
Z9fish3 天前
sse哈工大C语言编程练习23
c语言·数据结构·算法
代码无bug抓狂人3 天前
C语言之单词方阵——深搜(很好的深搜例题)
c语言·开发语言·算法·深度优先
CodeJourney_J3 天前
从“Hello World“ 开始 C++
c语言·c++·学习
枫叶丹43 天前
【Qt开发】Qt界面优化(七)-> Qt样式表(QSS) 样式属性
c语言·开发语言·c++·qt
with-the-flow3 天前
从数学底层的底层原理来讲 random 的函数是怎么实现的
c语言·python·算法
Sunsets_Red3 天前
P8277 [USACO22OPEN] Up Down Subsequence P 题解
c语言·c++·算法·c#·学习方法·洛谷·信息学竞赛