arm_linux 时间函数调用与转换

本文介绍了C语言中用于处理时间的函数,包括time(), ctime(), asctime(), localtime(), gmtime()等,以及它们在多线程环境下的线程安全性。通过示例代码展示了如何将时间戳转换为易读格式,并讨论了线程安全的_r版本函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很简单的time函数,在time.h中定义,用法如下:

#include <stdio.h>
#include <time.h>

int main (void)
{
    time_t timep;
    time(&timep);
    printf("UTC time is 0x%08x\n",timep);
    timep=time(NULL);
    printf("UTC time is 0x%08x\n",timep);
    return 0;
}

而上述函数读取出来的是从1970年一月一日开始到现在的秒数,不方便查看,所以需要使用时间转换的函数,常用的时间转换函数如下所示:

char *ctime(const time_t *timep);//change time to string type
struct tm *gmtime(const time_t *timep);//change time to gmt(格林尼治时间) time
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);
struct tm  //use to save and show time 
{
    int tm_sec;    /* Seconds (0-60) */
    int tm_min;    /* Minutes (0-59) */
    int tm_hour;   /* Hours (0-23) */
    int tm_mday;   /* Day of the month (1-31) */
    int tm_mon;    /* Month (0-11) */
    int tm_year;   /* Year - 1900 */
    int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
    int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
    int tm_isdst;  /* Daylight saving time */
};

上述函数中带有_r后缀的是线程安全型函数,也就是说在多线程中同时调用这些函数不会出现异常。换句话说一个类或者程序所提供的接口对于线程来说是原子操作或者多个线程之间的切换不会导致该接口的执行结果存在二义性,也就是说我们不用考虑同步的问题

   ┌───────────────┬───────────────┬─────────────────────────────────┐
   │Interface      │ Attribute     │ Value                           │
   ├───────────────┼───────────────┼─────────────────────────────────┤
   │asctime()      │ Thread safety │ MT-Unsafe race:asctime locale   │
   ├───────────────┼───────────────┼─────────────────────────────────┤
   │asctime_r()    │ Thread safety │ MT-Safe locale                  │
   ├───────────────┼───────────────┼─────────────────────────────────┤
   │ctime()        │ Thread safety │ MT-Unsafe race:tmbuf            │
   │               │               │ race:asctime env locale         │
   ├───────────────┼───────────────┼─────────────────────────────────┤
   │ctime_r(), gm‐ │ Thread safety │ MT-Safe env locale              │
   │time_r(), lo‐  │               │                                 │
   │caltime_r(),   │               │                                 │
   │mktime()       │               │                                 │
   ├───────────────┼───────────────┼─────────────────────────────────┤
   │gmtime(), lo‐  │ Thread safety │ MT-Unsafe race:tmbuf env locale │
   │caltime()      │               │                                 │
   └───────────────┴───────────────┴─────────────────────────────────┘

测试代码及运行效果:

#include <stdio.h>
#include <time.h>

int main (void)
{
    time_t timep;
    struct tm *tblock;
    time(&timep);
    printf("UTC time is 0x%08x\n",ctime(&timep));
    printf("UTC time is 0x%08x\n",asctime(gmtime(&timep)));
    tblock=localtime(&timep);
    printf("local time is :%s\n",asctime(tblock));
    printf("local time is :%s\n",ctime(&timep));
    return 0;
}

运行效果

[root@wly]# ./gettime                                                                                                                                                                                                          
UTC time is 0x401e4838
UTC time is 0x401e4838
local time is :Sun Jan  1 00:41:34 2012

local time is :Sun Jan  1 00:41:34 2012
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

T触发器

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值