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);// ortime_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 timestampprintf("%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"
Linux C 语言日期时间函数总结相关推荐
- c语言时间函数状态,C语言的时间函数
下面是C语言的获取本地时间和构造时间进行格式化时间显示输出的相关函数: This page is part of release 3.35 of the Linux man-pages project ...
- [转]ORACLE日期时间函数大全
本文转自:http://www.cnblogs.com/chuncn/archive/2009/04/29/1381282.html ORACLE日期时间函数大全 TO_DATE格式(以时间:2007 ...
- DELPHI日期时间函数(DateUtils单元)
原文出自:http://shao171.blog.163.com/blog/static/238397019201502011844293/ CompareDate 函数 比较两个日期时间值日期部分 ...
- Oracle 日期时间函数大全
ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits ...
- date( 1day ) php,PHP_使用PHP的日期与时间函数技巧,PHP的日期时间函数date() 1,年- - phpStudy...
使用PHP的日期与时间函数技巧 PHP的日期时间函数date() 1,年-月-日 复制代码 代码如下: echo date('Y-m-j'); 2007-02-6 echo date('y-n-j') ...
- ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digi
ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits ...
- MySQL 学习笔记(3)— 字符串函数、数值函数、日期时间函数、流程函数、聚集函数以及分组数据
1. 字符串函数 MySQL 的常用函数包括字符串函数.数值函数.日期时间函数.流程函数等. SELECT ascii("abc"),char(97),concat("h ...
- PHP 中日期时间函数 date() 用法总结
[导读] date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考.格式化日期date() 函数的第一个参数规定了如何格式化日期 时间.它 ...
- Php中如何记录本报时间,详细讲解PHP的日期时间函数date()
详细讲解PHP的日期时间函数date() 作者:wang 日期:2009-06-06 字体大小: 小 中 大 1,年-月-日 echo date('Y-m-j'); 2007-02-6 echo da ...
最新文章
- Json的list、map、对象对应格式
- Java之IO,BIO,NIO,AIO
- mysql的分页怎么不对_mysql一对多关联查询分页错误问题的解决方法
- 取某个日期所在周的任意一天日期
- JavaScript立即执行函数学习
- 国行ps4服务器维护,不能登录其地区的PSN代表什么
- 没有web.xml如何使用 Maven打war包?
- ES6语法实现数据的双向绑定
- 计算机网络体系结构(详图)
- HttpClient介绍
- python小玩意——性格测试
- 派森python_派森(Python)
- MEM/MBA 写作-论证有效性分析(09)逻辑缺陷-误用百分数滑坡谬误
- python统计中文字符数量
- 死链提交为什么不能提交 html文件,死链提交有什么用(如何处理网站死链)
- 《Android系统源代码情景分析》一书勘误
- 9.1.4 用 send2trash 模块安全地删除
- [摘录]第四部分 教训篇(1)
- 江苏省小学升初中数学题:求图中阴影面积
- 计算机硬件硬盘分区,电脑硬盘分区知多少 怎么分区才合理?