Linux C 时间编程

时间编程

Linux中时间相关命令

1)date:打印当前的系统时间。

2)date -s 20231111:修改系统日期为2023年11月11日。

3)date -s "20231111 11:11:11":修改系统时间为2023年11月11日11点11分11秒。

时间编程

time  获取当前的时间

头文件:

#include<time.h>

函数原型:time_t time(time_t *t);

参数介绍:

t:可以返回秒数,也可以填空值

返回值:成功则返回秒数从1970.1.1的0:0:0到现在所经过的秒数,失败则返回time_t-1值,错误原因存于 errno 中。

c 复制代码
	time_t tim = time( NULL );

gmtime  获取当前日期时间

头文件:

#include<time.h>

函数原型:struct tm*gmtime(const time_t *timep);

参数介绍:

timep:指向表示日历时间的 time_t 值的指针。

返回值:该函数返回指向 tm 结构的指针,该结构带有被填充的时间信息。

c 复制代码
	struct tm * timepos = gmtime ( &tim );
	//下面时结构体tm的数据结构
	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;	//夏令时
	};

localtime  获取本地时间日期

头文件:

#include<time.h>

函数原型:struct tm *localtime(const time_t *timer);

参数介绍:

指向表示日历时间的 time_t 值的指针。

返回值:该函数返回指向 tm 结构的指针,该结构带有被填充的时间信息。

c 复制代码
	struct tm * ltimepos = localtime ( &tim );

asctime  规格时间结构体为字符串

头文件:

#include<time.h>

函数原型:==char *asctime(const struct tm *timeptr) ==

参数介绍:

timeptr:指向 tm 结构的指针。

返回值:返回一个指向字符串的指针,它代表了结构 struct timeptr 的日期和时间。

c 复制代码
	struct tm * ltimepos = localtime ( &tim );
	printf( "%s\n", asctime ( ltimepos  ) );
	//输出结果
	//Mon Nov 11 11:11:11 2023
相关推荐
淡淡烟雨淡淡愁1 天前
安装libreoffice
linux
蜀道山老天师1 天前
云原生监控入门:监控基础概念 + SLI/SLO/SLA 详解 + Prometheus 从零安装配置
linux·运维·云原生·prometheus
AIDF20261 天前
linux 服务器网络问题排查
linux·服务器·网络
楼兰公子1 天前
br_opi5_plus_defconfig 附带uboot
linux·运维·服务器
炸膛坦客1 天前
嵌入式 - 数据结构与算法:(1-9)数据结构 - 队列(Queue)
c语言·数据结构
mzhan0171 天前
Linux: signal: SIGALRM; alarm: ITIMER_REAL
linux·运维·服务器
mzhan0171 天前
Linux: compare的直观性
java·linux·服务器
代码中介商1 天前
栈结构完全指南:顺序栈实现精讲
c语言·开发语言·数据结构
原来是猿1 天前
TCP Server 业务扩展实战:从 Echo 到远程命令执行与词典翻译
linux·运维·服务器
剑神一笑1 天前
Linux awk 命令:文本处理的瑞士军刀
linux·运维·chrome