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_REALTIME | 0 | 系统时间 |
CLOCK_MONOTONIC | 1 | 系统运行时间 |
CLOCK_PROCESS_CPUTIME_ID | 2 | 进程运行时间 |
CLOCK_THREAD_CPUTIME_ID | 3 | 线程运行时间 |
#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"