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;
}

运行结果:

相关推荐
2的n次方_3 小时前
Runtime 执行提交机制:NPU 硬件队列的管理与任务原子化下发
c语言·开发语言
凡人叶枫3 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
凡人叶枫5 小时前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++
傻乐u兔6 小时前
C语言进阶————指针3
c语言·开发语言
CodeSheep程序羊7 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
I'mChloe7 小时前
PTO-ISA 深度解析:PyPTO 范式生成的底层指令集与 NPU 算子执行的硬件映射
c语言·开发语言
2的n次方_8 小时前
Runtime 内存管理深化:推理批处理下的内存复用与生命周期精细控制
c语言·网络·架构
嵌入小生0078 小时前
标准IO---核心函数接口延续(嵌入式Linux)
c语言·vscode·vim·嵌入式·小白·标准io·函数接口
历程里程碑9 小时前
Linux20 : IO
linux·c语言·开发语言·数据结构·c++·算法
智者知已应修善业11 小时前
【洛谷P9975奶牛被病毒传染最少数量推导,导出多样例】2025-2-26
c语言·c++·经验分享·笔记·算法·推荐算法