首页 运维 linux运维 linux有计算时间的函数吗

linux有计算时间的函数吗

Apr 11, 2023 pm 05:46 PM
linux

linux有计算时间的函数,例如:可获取秒级时间差的函数time()、可获取微秒级时间差的函数gettimeofday()和settimeofday()、可获取纳秒级时间差的函数clock_gettime()等等。

linux有计算时间的函数吗

本教程操作环境:linux7.3系统、Dell G3电脑。

下面讲解的是linux中 时间相关的函数和将时间转换相关函数

1.获取时间相关函数

1.1 获取秒级时间差函数

#include <time.h>
time_t time(time_t *timer);//通过函数返回值或者timer 变量均可以获取到当前时间
登录后复制

time_t实际上是一个长整型,表示UTC时间(1970年1月1日0时0分0秒,Linux系统的Epoch时间)到当前系统时间的秒数级时间差

1.2 获取微秒级时间差函数

#include <sys/time.h>
#include <unistd.h>
struct timeval {
    time_t       tv_sec;     /* seconds */
    suseconds_t tv_usec;    /* microseconds */
};

struct timezone{
 
    int tz_minuteswest; /*miniutes west of Greenwich*/
 
    int tz_dsttime; /*type of DST correction*/
 
};

//函数执行成功返回0,失败返回-1. 其中timezone 是时区相关的结构体
int gettimeofday(struct timeval *tv, struct timezone *tz);

//用来设置制定的时间和时区信息
int settimeofday(const struct timeval *tv, const struct timezone *gz);
登录后复制

1.3获取纳秒级时间差函数

#include <time.h>

/*
其中clk_id 用来制定对应的时钟类型,不同的类型可以用来获取不同的时间值,具体有四种:
CLOCK_REALTIME: 			系统实时时间,从UTC开始计时,若时间被用户更改计数时间相应改变;
CLOCK_MONOTONIC:			从系统启动开始计时,即使用户更改时间也没有影响;
CLOCK_PROCESS_CPUTIME_ID:	本进程开始到执行到当前程序系统CPU花费的时间;
CLOCK_THREAD_CPUTIME_ID:	本线程开始到执行到当前程序系统CPU花费的时间

*/

struct timespec{
    time_t tv_sec;    //s
    long tv_nsec;    //ns
};

int clock_gettime(clockid_t clk_id, struct timespec* tp);
登录后复制

2.转换时间相关函数

2.1将上述获取时间函数获取到的时间参数time_t转换为结构体

struct tm,该结构体包含年月日等非常详细的域。下如所示:

#include <time.h>
 
struct tm{
    int tm_sec;   //秒
    int tm_min;   //分
    int tm_hour;  //时;取值区间为[0, 23]
    int tm_mday;  //日;取值区间为[1, 31]
    int tm_mon;   //月份;取值区间为[0, 11]; 0表示1月份依次递增到12月份
    int tm_year;   //年份;其值为1900年至今年数
    int tm_wday;  //星期;0代表星期天,1代表星期1,以此类推
    int tm_yday;   //日期;0代表1月1日
    int tm_isdst;   //夏令时标识符;使用夏令时为正,不使用t为0,不确定时为负*/
};
登录后复制

将time_t转换成struct tm结构体常用的函数如下:

#include <time.h> 
struct tm* gmtime(const time_t* timep);
struct tm*localtime(const time_t* timep);
登录后复制

gmtime() 和localtime() 函数可以将time_t 数据格式转化成tm格式的类型。区别是gmtime()转换的结果是GMT(中央时区)对应的信息,而localtime() 函数转换的结果是当前所在时区的信息。

2.2将time_t转换成我们习惯性使用的时间和日期字符串

对应转换函数如下:

#include <time.h>
char* ctime(time_t* timep);
登录后复制

2.3 将struct tm 转换成 time_t

对应函数如下:

#include <time.h>
time_t mktime(struct tm *p_tm);
登录后复制

2.4将struct tm转换成我们习惯性使用的时间和日期字符串

对应函数如下:

#include <time.h>
char *asctime(const struct tm *p_tm); //习惯性字符串 Thu Dec  9 07:13:35 2021
登录后复制

2.5 将时间字符串转换成 struct tm格式

/**************************************
** description: 将struct tm 按照指定的format格式转化成字符串
** parameter:
		** *s : 需要被转换的时间字符串
		** *format:时间字符串的格式
		** *tm:转换后的tm时间
**************************************/
char *strptime(const char *s, const char *format, struct tm *tm);
登录后复制

2.6 将struct tm 按照指定的format格式转化成字符串

/**************************************
** description: 将struct tm 按照指定的format格式转化成字符串
** parameter:
		** *s : 生成的时间字符串
		** max: 字符串最大字符数(即最大可生成的字符数量)
		** *format:生成的字符串格式
		** *tm:需要被转换的tm时间
**************************************/
size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);
登录后复制

3.举例

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>

int main(int argc, char *argv[])
{	
	char *pstr = NULL;
	struct tm *ptm = NULL;

	printf("************** 使用ctime获取时间time_t **************\n");
	time_t times = 0;
	time(&times);
	printf("time_t:%ld\n\n", times);
	
	printf("************** 使用ctime转换time_t成我们习惯性使用的时间和日期字符串 **************\n");
	pstr = ctime(&times);
	printf("ctime:%s\n", pstr);
	
	printf("************** 使用gmtime转换time_t成struct tm 时间和日期**************\n");
	ptm = gmtime(&times);
	printf("time : %d:%d:%d\n", ptm->tm_hour,  ptm->tm_min,  ptm->tm_sec);
	printf("date: %d:%d:%d\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
	printf("year: wday:%d yday:%d isdst:%d\n\n", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);
	
	printf("************** 使用asctime转换struct tm成我们习惯性使用的时间和日期字符串**************\n");
	pstr = asctime(ptm);
	printf("asctime:%s\n\n", pstr);
	
	printf("************** 使用localtime转换time_t成struct tm 时间和日期**************\n");
	ptm = localtime(&times);
	printf("time : %d:%d:%d\n", ptm->tm_hour,  ptm->tm_min,  ptm->tm_sec);
	printf("date: %d:%d:%d\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
	printf("year: wday:%d yday:%d isdst:%d\n", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);

	pstr = asctime(ptm);
	printf("asctime:%s\n\n", pstr);

	printf("************** 使用gettimeofday获取微秒级的时间**************\n");
	struct timeval tv;
	struct timezone tz;
	gettimeofday(&tv, &tz);
	ptm = localtime(&tv.tv_sec);
	printf("time : %d:%d:%d\n", ptm->tm_hour,  ptm->tm_min,  ptm->tm_sec);
	printf("date: %d:%d:%d\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
	printf("year: wday:%d yday:%d isdst:%d\n", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);
	printf("tv_usec:%ld\n\n", tv.tv_usec);
	
	printf("************** 使用clock_gettime获取纳秒级的时间**************\n");
	struct timespec tp;
	clock_gettime(CLOCK_REALTIME, &tp);
	ptm = localtime(&tp.tv_sec);
	printf("time : %d:%d:%d\n", ptm->tm_hour,  ptm->tm_min,  ptm->tm_sec);
	printf("date: %d:%d:%d\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
	printf("year: wday:%d yday:%d isdst:%d\n", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);	
	printf("tp.tv_nsec:%ld\n\n", tp.tv_nsec);
	return 0;
}
登录后复制

特定的时间字符相互转换

int str_to_time(void)
{
	char pstr[128] = {0};
    struct tm t;
    strptime("2021-04-23 12:34:56", "%Y-%m-%d %H:%M:%S", &t);
	
    printf("**** tm_isdst: %d, tm_yday:%d, tm_wday%d,\n %d-%d-%d \n %d:%d:%d\n", 
    t.tm_isdst, t.tm_yday, t.tm_wday, t.tm_year+1900, t.tm_mon+1, t.tm_mday,
    t.tm_hour, t.tm_min, t.tm_sec);
    printf("mktime ts:%ld\n", mktime(&t));
	printf("asctime:%s\n", asctime(&t));
	
	strftime(pstr, sizeof(pstr), "%Y-%m-%d %H:%M:%S", &t);
    printf("pstr:%s\n", pstr);
}

int time_to_str(void)
{
	char pstr[128] = {0};
    struct tm t = {
               .tm_sec = 56,    /* Seconds (0-60) */
               .tm_min = 34,    /* Minutes (0-59) */
               .tm_hour = 12,   /* Hours (0-23) */
               .tm_mday = 23,   /* Day of the month (1-31) */
               .tm_mon = 4-1,     /* Month (0-11) */
               .tm_year = 2021-1900,   /* Year - 1900 */
               .tm_wday = 5,     /* Day of the week (0-6, Sunday = 0) */
               .tm_yday = 113,   /* Day in the year (0-365, 1 Jan = 0) */
               .tm_isdst = 0,    /* Daylight saving time */
           };
    strftime(pstr, sizeof(pstr), "%Y-%m-%d %H:%M:%S", &t);
    printf("pstr:%s\n", pstr);
}
登录后复制

若想获取从系统启动开始计时,即使用户更改时间也没有影响的时间,单位毫秒,举例如下:

unsigned long long clock_systick_get(void)
{
    int ret = -1;
    unsigned long long time;
    int cnt = 0;
    struct timespec  now = {0, 0};

    while (ret < 0 && cnt < 3)
    {
        ret = clock_gettime(CLOCK_MONOTONIC, &now);	//获取失败重试,最大执行3次
        cnt++;
    }
    time = now.tv_sec * 1000 + now.tv_nsec / (1000000);
    return time;
}
登录后复制

相关推荐:《Linux视频教程

以上是linux有计算时间的函数吗的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

vscode需要什么电脑配置 vscode需要什么电脑配置 Apr 15, 2025 pm 09:48 PM

VS Code 系统要求:操作系统:Windows 10 及以上、macOS 10.12 及以上、Linux 发行版处理器:最低 1.6 GHz,推荐 2.0 GHz 及以上内存:最低 512 MB,推荐 4 GB 及以上存储空间:最低 250 MB,推荐 1 GB 及以上其他要求:稳定网络连接,Xorg/Wayland(Linux)

vscode 无法安装扩展 vscode 无法安装扩展 Apr 15, 2025 pm 07:18 PM

VS Code扩展安装失败的原因可能包括:网络不稳定、权限不足、系统兼容性问题、VS Code版本过旧、杀毒软件或防火墙干扰。通过检查网络连接、权限、日志文件、更新VS Code、禁用安全软件以及重启VS Code或计算机,可以逐步排查和解决问题。

notepad怎么运行java代码 notepad怎么运行java代码 Apr 16, 2025 pm 07:39 PM

虽然 Notepad 无法直接运行 Java 代码,但可以通过借助其他工具实现:使用命令行编译器 (javac) 编译代码,生成字节码文件 (filename.class)。使用 Java 解释器 (java) 解释字节码,执行代码并输出结果。

vscode是什么 vscode是干什么用的 vscode是什么 vscode是干什么用的 Apr 15, 2025 pm 06:45 PM

VS Code 全称 Visual Studio Code,是一个由微软开发的免费开源跨平台代码编辑器和开发环境。它支持广泛的编程语言,提供语法高亮、代码自动补全、代码片段和智能提示等功能以提高开发效率。通过丰富的扩展生态系统,用户可以针对特定需求和语言添加扩展程序,例如调试器、代码格式化工具和 Git 集成。VS Code 还包含直观的调试器,有助于快速查找和解决代码中的 bug。

vscode 可以用于 mac 吗 vscode 可以用于 mac 吗 Apr 15, 2025 pm 07:36 PM

VS Code 可以在 Mac 上使用。它具有强大的扩展功能、Git 集成、终端和调试器,同时还提供了丰富的设置选项。但是,对于特别大型项目或专业性较强的开发,VS Code 可能会有性能或功能限制。

VSCode怎么用 VSCode怎么用 Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) 是一款跨平台、开源且免费的代码编辑器,由微软开发。它以轻量、可扩展性和对众多编程语言的支持而著称。要安装 VSCode,请访问官方网站下载并运行安装程序。使用 VSCode 时,可以创建新项目、编辑代码、调试代码、导航项目、扩展 VSCode 和管理设置。VSCode 适用于 Windows、macOS 和 Linux,支持多种编程语言,并通过 Marketplace 提供各种扩展。它的优势包括轻量、可扩展性、广泛的语言支持、丰富的功能和版

Linux体系结构:揭示5个基本组件 Linux体系结构:揭示5个基本组件 Apr 20, 2025 am 12:04 AM

Linux系统的五个基本组件是:1.内核,2.系统库,3.系统实用程序,4.图形用户界面,5.应用程序。内核管理硬件资源,系统库提供预编译函数,系统实用程序用于系统管理,GUI提供可视化交互,应用程序利用这些组件实现功能。

Linux的主要目的是什么? Linux的主要目的是什么? Apr 16, 2025 am 12:19 AM

Linux的主要用途包括:1.服务器操作系统,2.嵌入式系统,3.桌面操作系统,4.开发和测试环境。Linux在这些领域表现出色,提供了稳定性、安全性和高效的开发工具。

See all articles