乱读天书, 不求甚解
周祎骏的个人云笔记
Toggle navigation
乱读天书, 不求甚解
主页
Linux:系统配置
Linux:用户管理
Linux:优化排错
Linux:进程调度
Linux:文件系统
Linux:网络
Linux:系统服务
Linux:安全
Linux:内核
容器:Docker
容器:containerd
容器编排:Kubernetes
IAC:Terraform
大数据:Hadoop
大数据:Zookeeper
大数据:Hbase
消息队列:rsyslog
消息队列:kafka
数据库:MySQL
数据库:MongoDB
搜索引擎:Elasticsearch
时序数据库:OpenTSDB
网站服务:Nginx
编程:Bash
编程:Perl
编程:Python
编程:C
编程:JAVA
编程:Rust
版本控制:gitlab
知识管理:docusaurus
常用小工具
关于我
标签
C 2.15 Linux函数库time.h
2018-06-17 14:35:25
59
0
0
admin
> 关于时间 #struct ``` struct tm { int tm_sec; //秒数,支持61是因为闰年 [0, 61] int tm_min; //分钟数 [0, 59] int tm_hour; //小时数 [0, 23] int tm_mday; //天 [1, 31] int tm_mon; //月 [0, 11] int tm_year; //年 1900年后 int tm_wday; //星期 [0, 6] int tm_yday; //天 [0, 365] int tm_isdst; //夏令时标志 <0, 0, >0 } ; ``` #函数 **time_t time(time_t *calptr)**: 获得当前时间,time_t格式,出错返回-1 **struct tm *gmtime(const time_t *calptr)**:将time_t时间转化为gmt时间 **struct tm *localtime(const time_t *calptr)**:将time_t时间转化为本地时间 **time_t mktime(struct tm *tmptr)**: 将tm格式的时间转化为time_t时间 **char *asctime(const struct tm *tmptr)**:获取年月日格式的字符串 **char *ctime(const time_t *calptr)**:获取年月日格式的字符串 **size_t strftime(char *buf, size_t maxsize, const char *format, const struct tm *tmptr)**:将tm转化为指定格式的字符串,放在长度为maxsize的字符串中,成功返回存入字符串的字符数,失败返回0 format格式: |符号| 意义 |例子 | |----|------------------|--------------------------| | %a | 缩写的星期 |Tue | | %A | 星期 |Tuesday | | %b | 缩写的月 |JAN | | %B | 月 |January | | %c | 日期和时间 |Wed Jan 10 20:30:40 1018 | | %d | 天:[01, 31] |10 | | %H | 小时: [00, 23] |20 | | %I | 小时: [01, 12] |08 | | %j | 天:[001, 366] |020 | | %m | 月:[01, 12] |01 | | %M | 分:[00, 59] |30 | | %p | AM/PM |PM | | %S | 秒: [00, 61] |40 | | %W | 周:[00, 53] |02 | | %w | 星期:[0,6 ] |03 | | %x | 日期 |01/10/18 | | %X | 时间 |20:30:40 | | %y | 年 |18 | | %Y | 年 |2018 | | %Z | 时区名 |CST | ``` #include <stdio.h> #include <time.h> main() { struct tm *now_tm; time_t now = time(NULL); now_tm = localtime(&now); printf("%d\n",now_tm->tm_sec); printf("%s",asctime(now_tm)); printf("%s",ctime(&now)); char buf[100]; strftime(buf,100,"%Z %y%m%d %H%m%S",now_tm); printf("%s",buf); } ```
上一篇:
C 2.14 Linux函数库utsname.h,pwd.h,grp.h
下一篇:
C 2.16 Linux函数库mman.h
文档导航