Linux C 语言日期时间函数总结

C 语言获取系统时间相关函数总结记录一下,用的时候不至于再去到处找了。

函数精度说明
time()获取系统时间
ftime()秒 + 毫秒获取系统时间 1/10^3
gettimeofday()秒 + 微秒获取系统时间 1/10^6
clock_gettime()秒 + 纳秒获取系统时间 1/10^9
sleep()延时函数
usleep()微秒延时函数 1/10^6
struct tm-存储年月日时分秒的结构
localtime()-time_t 时间戳转为tm格式本地时间
asctime()-tm格式时间转为系统默认时间字符串, “Sun Sep 5 16:35:16 2021”
ctime()-time_t时间戳转为系统默认时间格式,等效于asctime (localtime (timer))

获取时间函数

time()

获取系统Unix时间戳,精度 秒。

#include <time.h>

int main()
{
    time_t t = time(NULL);
    // or
    time_t t;
    time(&t);

    printf("%ld\n", t);  // 1630826068
}

ftime

获取系统Unix时间戳,精度 毫秒。

获取 秒 + 毫秒(1/10^3)

#include <sys/timeb.h>

int main()
{
    struct timeb tp;
    ftime(&tp);
    printf("%ld - %d\n", tp.time, tp.millitm);  // 1630829481 - 995
}

gettimeofday

获取系统Unix时间戳,精度 秒 + 微秒(1/10^6)。

#include <sys/time.h>

int main()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    printf("%ld - %ld\n", tv.tv_sec, tv.tv_usec);  // 1630829568 - 447959
}

clock_gettime()

获取系统Unix时间戳,精度 秒 + 纳秒(1/10^9)。
获取的这个时间,根据第一个参数值不同,可以获取 系统Unix时间、系统启动后时间、进程运行时间和线程运行时间等。

说明
CLOCK_REALTIME0系统时间
CLOCK_MONOTONIC1系统运行时间
CLOCK_PROCESS_CPUTIME_ID2进程运行时间
CLOCK_THREAD_CPUTIME_ID3线程运行时间
#include <time.h>

int main()
{
    struct timespec tv;
    clock_gettime(CLOCK_REALTIME, &tv);  // get Unix timestamp
    printf("%ld - %ld\n", tv.tv_sec, tv.tv_nsec);  // 1630829669 - 818878549
}

延时相关函数

sleep() + usleep()

延时 秒 + 微秒(1/10^6)

#include <unistd>

sleep(n)  // n秒
usleep(n) // n微秒(1/10^6 s)

日期时间格式化函数

strftime()

直接上最实用的,可以格式化自定义格式。注意存储字符串buffer的长度。常用的年月日时分秒看示例,还有一些不常用的%Z时区 等参看手册吧。

time_t t = time(NULL);

char d3[20] = {0};  // 注意长度
strftime(d3, sizeof(d3), "%Y-%m-%d %H:%M:%S\n", localtime(&t)); // 使用 localtime() 先转为本地时间

printf("%s\n", d3);
// "2021-09-05 16:41:56"

// 这个例子是获取HTTP heaader 中Date字段的日期时间类型(RFC1123格式)
char http_date[30] = {0};
strftime(http_date, sizeof(http_date), "%a, %d %b %Y %H:%M:%S GMT\n", gmtime(&t));
printf("%s\n", buff);
// "Mon, 25 Oct 2021 06:51:38 GMT"

localtime()

Unix时间戳都是统一的,但是显示时间不同时区不一样,这个函数是把Unix时间戳转换为struct tm结构本地时间。

struct tm *localtime (const time_t *__timer)

ctime()

这个最傻瓜式,直接格式化为系统默认的本地日期时间格式。等效于asctime (localtime (timer))

time_t t = time(NULL);

char *date = ctime(&t);

printf("%s", date);
// "Sun Sep  5 16:30:27 2021"

asctime()

也是系统默认格式,主要是对struct tm类型进行转换。

time_t t = time(NULL);

struct tm *dateTime = localtime(&t);
char *d2 = asctime(dateTime);

printf ("%s\n", d2);
// "Sun Sep  5 16:35:16 2021"

struct tm

struct tm 结构存储 年 月 日 时 分 秒 星期 等各个单位的值。

struct tm
{
  int tm_sec;			/* 秒.	[0-60] (1 leap second) */
  int tm_min;			/* 分钟.	[0-59] */
  int tm_hour;			/* 时.	[0-23] */
  int tm_mday;			/* 日.		[1-31] */
  int tm_mon;			/* 月.	[0-11] */
  int tm_year;			/* 年-1900. 使用时+1900为当前年份 */
  int tm_wday;			/* 星期几.[0-6] */
  int tm_yday;			/* Days in year.[0-365]	*/
  int tm_isdst;			/* DST.		[-1/0/1]*/
};

使用示例

time_t t = time(NULL);

struct tm *dt = localtime(&t);
sprintf(d3, "%d-%02d-%02d %02d:%02d:%02d",
            dt->tm_year + 1900,
            dt->tm_mon + 1,
            dt->tm_mday,
            dt->tm_hour,
            dt->tm_min,
            dt->tm_sec);

printf("%s\n", d3);
// "2021-09-05 16:49:16"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值