当前位置: 首页 > news >正文

C语言编程中的时间处理

最简单的time

在C语言编程中,处理时间最简单的函数就是time了。它的原型为:

#include <time.h>  time_t time(time_t *_Nullable tloc);

返回自从EPOCH,即1970年1月1日的零点零时零分,到当前的秒数。

输入参数可以是NULL。如果输入参数不是NULL,那么返回值也会存入tloc的地址处。

需要注意的是,在不同的平台上,这里的time_t定义可能不同。

glibc-devel-2.41中,/usr/include/bits/types/time_t.h中有如下定义:

 #include <bits/types.h>5 6 /* Returned by `time'.  */7 #ifdef __USE_TIME64_REDIRECTS8 typedef __time64_t time_t;9 #else10 typedef __time_t time_t;11 #endif

__time64_t__time_t的定义则如下:

__STD_TYPE __TIME64_T_TYPE __time64_t;__STD_TYPE __TIME_T_TYPE __time_t;      /* Seconds since the Epoch.  */

再继续跟下去,涉及到各种宏等分支,最终的定义在Fedora 42的X86_64平台上,这个值是long int。

Linux中的gettimeofday/settimeofday

time简单,但是也粗糙,它只能取到秒这个单位。而gettimeofday则可能取到微秒。

与之相对,settimeofday则可以设置系统时间。

gettimeofday/settimeofday的原型如下:

       #include <sys/time.h>int gettimeofday(struct timeval *restrict tv,struct timezone *_Nullable restrict tz);int settimeofday(const struct timeval *tv,const struct timezone *_Nullable tz);

struct timeval的定义为:

          struct timeval {  time_t      tv_sec;     /* seconds */  suseconds_t tv_usec;    /* microseconds */  };

tv_sec就是秒数,tv_usec是微秒。

struct timezone的定义为:

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

当执行gettimeofday之后,返回0表示成功,非0表示失败。

需要注意的是,tz这个结构已经过时,在我们使用gettimeofday与settimeofday的时候,直接把tz设为NULL,取得或者设置当前的本地时间就好了。

Windows中的GetLocalTime/SetLocalTime

在Windows环境下,取得与设置时间,可以使用GetLocalTime与SetLocalTime函数。

这两个函数的原型如下:

void GetLocalTime([out] LPSYSTEMTIME lpSystemTime
);BOOL SetLocalTime([in] const SYSTEMTIME *lpSystemTime
);

相关的数据结构定义如下:

typedef struct _SYSTEMTIME {WORD wYear;WORD wMonth;WORD wDayOfWeek;WORD wDay;WORD wHour;WORD wMinute;WORD wSecond;WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;

其中:
wYear

年数,范围是从1601到30872。

mMonth

月数,范围是从1到12。

mDayOfWeek

周偏移数,范围是从0到6。

wDay

天数,范围是从 1 到 31。

wHour

小时数,范围是从 0 到 23。

wMinute

分钟数,范围是从 0 到 59。

wSecond

秒数,范围是从 0 到 59。

wMilliseconds

毫秒数,范围是从 0 到 999。

还需要注意,SetLocalTime的返回值是布尔值,成功为非0。

以下代码,把一个从Linux取得的timeval时间,转化成Windows上的时间,进行设置:

int set_local_time_by_timeval (const struct timeval *tv)
{struct tm utc_tm;SYSTEMTIME st = { 0 };   if (localtime_s (&utc_tm, &tv->tv_sec) != 0)  {    return -1;}  st.wYear = utc_tm.tm_year + 1900;  st.wMonth = utc_tm.tm_mon + 1;  st.wDay = utc_tm.tm_mday;  st.wHour = utc_tm.tm_hour;  st.wMinute = utc_tm.tm_min;  st.wSecond = utc_tm.tm_sec;  st.wMilliseconds = (WORD)(time % (1000 * 1000) / 1000);  return SetLocalTime (&st) ? 0 : -1;

glib里的g_get_monotonic_time

如果我们的项目使用了glib(注意是glib,不是glibc),还有一个方便的函数可以使用,那就是g_get_monotonic_time

这个函数表示系统启动以来经过的微秒数,用来做一些时间间隔设计等工作。

比如,我们需要开发一些timer类功能,在程序经过若干时间之后,执行什么操作,如果使用time或者gettimeofday之类的函数,在系统时间改变之后,就会错乱。

但是,如果我们使用g_get_monotonic_time则可以完全避免这种问题。

如:

gint64 start = g_get_monotonic_time();/* 此处是需要测量的业务代码 */
/* 而且有可能系统时间更改 */gint64 end = g_get_monotonic_time();
g_print("耗时: %" G_GINT64_FORMAT " 微秒\n", end - start);

glib的这个函数有点像C++中的std::chrono::steady_clock

底层的clock_gettime

如果进行更精确地时间控制,在Linux中还可以使用clock族的几个函数:

    #include <time.h>  int clock_getres(clockid_t clockid, struct timespec *_Nullable res);  int clock_gettime(clockid_t clockid, struct timespec *tp);  int clock_settime(clockid_t clockid, const struct timespec *tp);

其中,clockid是可以控制的一些参数,如CLOCK_MONOTONIC、CLOCK_BOOTTIME、CLOCK_REALTIME等,具体意义可以见文知义。

而timespec的定义类似于timeval,但是第二个参数不是微秒,而是纳秒。

      #include <time.h>  struct timespec {  time_t     tv_sec;   /* Seconds */  /* ... */   tv_nsec;  /* Nanoseconds [0, 999'999'999] */  };
http://www.xdnf.cn/news/481735.html

相关文章:

  • 【学习笔记】机器学习(Machine Learning) | 第七章|神经网络(1)
  • Vue3中setup运行时机介绍
  • MyBatis—动态 SQL
  • 网安面试经(1)
  • MySQL8.x新特性:与mysql5.x的版本区别
  • SpringBoot--Bean管理详解
  • 3D生成新突破:阶跃星辰Step1X-3D开源,可控性大幅提升
  • python + flask 做一个图床
  • ThinkPad X250电池换电池芯(理论技术储备)
  • windows系统中下载好node无法使用npm
  • STM32控制电机
  • duxapp 2025-03-29 更新 编译结束的复制逻辑等
  • MySQL的触发器
  • ubuntu 20.04 更改国内镜像源-阿里源 确保可用
  • 适合学校使用的桌面信息看板,具有倒计时、桌面时钟、课程表、天气预报、自动新闻联播、定时关机、消息通知栏、随机点名等功能。
  • CMU-15445(4)——PROJECT#1-BufferPoolManager-Task#2
  • 【软件工程】符号执行与约束求解缺陷检测方法
  • MySQL性能优化
  • RAG-MCP:基于检索增强生成的大模型工具选择优化框架
  • Cross-Site Scripting(XSS)
  • AT9850B—单北斗导航定位芯片
  • 【leetcode】94. 二叉树的中序遍历
  • 逻辑回归(二分类)
  • Linux的日志管理
  • 青少年ctf平台应急响应-应急响应2
  • 在vue3中使用Cesium的保姆教程
  • 【2025年软考中级】第一章1.5 输入输出技术(外设)
  • 代码随想录第51天|岛屿数量(深搜)、岛屿数量(广搜)、岛屿的最大面积
  • 2025年Ai写PPT工具推荐,这5款Ai工具可以一键生成专业PPT
  • Java—— 方法引用 : :