strftime, localtime_r(替代localtime), gettimeofday(替代ftime)

本文详细介绍了C语言中处理日期和时间的各种函数,包括strftime和strptime等格式化和解析时间的函数,以及asctime、ctime、gmtime等转换时间格式的函数。此外还介绍了获取当前时间的gettimeofday函数,并给出了具体的使用示例。
部署运行你感兴趣的模型镜像

一、

       size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);

       The strftime() function returns the number of characters placed in  the
       array  s, not including the terminating null byte, provided the string,
       including the terminating null byte, fits.  Otherwise,  it  returns  0,
       and  the contents of the array is undefined. 

       把tm转换为字符串,存于s。如strftime(format_time, 100 - 2, "%F %T", time_ptr);

  •        %F     Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
  •        %T     The time in 24-hour notation (%H:%M:%S). (SU)


strptime -------- 将字符串按时间格式转换。命令行输入的检查会用到

二、transform date and time to broken-down time or ASCII

       char *asctime(const struct tm *tm);
       char *asctime_r(const struct tm *tm, char *buf);
       char *ctime(const time_t *timep);
       char *ctime_r(const time_t *timep, char *buf);
       struct tm *gmtime(const time_t *timep);
       struct tm *gmtime_r(const time_t *timep, struct tm *result);
       struct tm *localtime(const time_t *timep);
       struct tm *localtime_r(const time_t *timep, struct tm *result);
       time_t mktime(struct tm *tm);

asctime(), ctime(), gmtime() and localtime()返回指针指向静态数据,因此不是线程安全的。

线程安全版本为time_r(), ctime_r(), gmtime_r() and localtime_r()are specified by SUSv2, and available since libc 5.2.5.

三、

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

  • ftime 已经被淘汰了。
  • 秒级,使用time;
  • 毫秒级,使用gettimeofday;
  • 微秒级,使用lock_gettime,但使用不广泛。


举例:

    struct timeval tv;
    gettimeofday(&tv, NULL);
    time_t currentTime = tv.tv_sec;   

    struct tm CurlocalTime;
    localtime_r(&currentTime, &CurlocalTime);
    
    char dateTime[20];
    strftime(dateTime, 20, "%Y-%m-%dT%H:%M:%S", CurlocalTime);


您可能感兴趣的与本文相关的镜像

Langchain-Chatchat

Langchain-Chatchat

AI应用
Langchain

Langchain-Chatchat 是一个基于 ChatGLM 等大语言模型和 Langchain 应用框架实现的开源项目,旨在构建一个可以离线部署的本地知识库问答系统。它通过检索增强生成 (RAG) 的方法,让用户能够以自然语言与本地文件、数据库或搜索引擎进行交互,并支持多种大模型和向量数据库的集成,以及提供 WebUI 和 API 服务

在多线程或可重入环境中,使用 `gmtime()` 和 `localtime()` 是不安全的,因为它们返回的是指向静态内存的指针。为了避免线程安全问题,应将: - `gmtime()` 替换为 `gmtime_r()` - `localtime()` 替换为 `localtime_r()` 这两个函数是 POSIX 标准提供的线程安全版本。 --- ### ✅ 修改说明 #### 原始函数(不安全): ```c struct tm *gmtime(const time_t *timep); struct tm *localtime(const time_t *timep); ``` #### 替换后(线程安全): ```c struct tm *gmtime_r(const time_t *timep, struct tm *result); struct tm *localtime_r(const time_t *timep, struct tm *result); ``` --- ### ✅ 修改后的完整 C 代码片段(仅展示受影响部分) ```c // 将 UTC 时间戳转换为本地时间字符串(线程安全) char *utc_to_local_str(time_t utc_time) { static __thread char buffer[20]; // 使用 __thread 保证线程安全 struct tm tm_info; localtime_r(&utc_time, &tm_info); // 使用 localtime_r 替代 localtime strftime(buffer, 20, "%Y-%m-%d %H:%M:%S", &tm_info); return buffer; } // 将本地时间戳转换为 UTC 时间戳(线程安全) time_t local_to_utc(time_t local_time) { struct tm tm_local; struct tm tm_utc; // 使用 localtime_r 获取本地时间结构体 localtime_r(&local_time, &tm_local); // 转换为 UTC 时间戳 time_t utc_time = mktime(gmtime_r(&local_time, &tm_utc)); // 计算时区偏移量 return utc_time - (tm_local.tm_gmtoff); } ``` --- ### ✅ 修改说明表 | 函数名 | 是否线程安全 | 替代函数 | 是否推荐使用 | |--------------|---------------|------------------|----------------| | `gmtime()` | ❌ | `gmtime_r()` | ✅ 推荐 | | `localtime()`| ❌ | `localtime_r()` | ✅ 推荐 | --- ### ✅ 示例输出(封装后的 JSON) ```json { "data": { "audit_type": "system", "audit_list": [ { "description": "system power on", "time": "2025-08-29 17:14:00" }, { "description": "system power on", "time": "2025-08-29 18:14:00" } ], "page_no": 1, "page_count": 2, "total_count": 13, "total_page": 2 }, "resCode": 20000 } ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值