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
相关推荐
RuoZoe4 小时前
重塑WPF辉煌?基于DirectX 12的现代.NET UI框架Jalium
c语言
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
深紫色的三北六号2 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash2 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行3 天前
Linux和window共享文件夹
linux
木心月转码ing3 天前
WSL+Cpp开发环境配置
linux
祈安_3 天前
C语言内存函数
c语言·后端