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 小时前
Centos中内存CPU硬盘的查询
linux·运维·centos
科大饭桶1 小时前
C++入门自学Day11-- String, Vector, List 复习
c语言·开发语言·数据结构·c++·容器
Felven1 小时前
C. Game of Mathletes
c语言·开发语言
luoqice2 小时前
linux下查看 UDP Server 端口的启用情况
linux
倔强的石头_3 小时前
【Linux指南】动静态库与链接机制:从原理到实践
linux
Cx330❀3 小时前
【数据结构初阶】--排序(四):归并排序
c语言·开发语言·数据结构·算法·排序算法
赏点剩饭7783 小时前
linux中的hostpath卷、nfs卷以及静态持久卷的区别
linux·运维·服务器
意疏3 小时前
【C语言篇】srand函数的详细用法解析
c语言·开发语言
神鸟云3 小时前
DELL服务器 R系列 IPMI的配置
linux·运维·服务器·网络·边缘计算·pcdn
herderl4 小时前
**僵尸进程(Zombie Process)** 和**孤儿进程(Orphan Process)**
linux·运维·服务器·网络·网络协议