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
相关推荐
夜月yeyue1 天前
Linux内高端内存
linux·运维·单片机·嵌入式硬件·ci/cd·硬件架构
猫豆~1 天前
nginx实战-PHP——day2
linux·centos·云计算
杨云龙UP1 天前
MySQL 自动备份与覆盖恢复实战:一套脚本搞定全库/按库备份恢复
linux·运维·数据库·sql·mysql
三小尛1 天前
Linux的常见指令
linux
starvapour1 天前
Ubuntu下sudo的免密操作
linux·ubuntu
sjg200104141 天前
Deepin 20.9 误装gcc-8-base_8.4.0-1ubuntu1~16.04.1_amd64 后卸载
linux·运维·服务器
一帘忧梦1 天前
linux 系统rcs脚本启动
linux·运维·lua
jerryinwuhan1 天前
1210_1 Linux
linux·运维·服务器
程序员Jared1 天前
深入浅出C语言——程序环境和预处理
c语言
应茶茶1 天前
从 C 到 C++:详解不定参数的两种实现方式(va_args 与参数包)
c语言·开发语言·c++