当前位置:网站首页>时间操作 - 时间格式转换
时间操作 - 时间格式转换
2022-06-29 06:41:00 【qazw9600】
说明
- 编程中,时间格式转换是常用的功能,特别是与格式化字符串之间的互相转换。
时间转格式化字符串
- 固定格式字符串 - asctime/asctime_r/ctime/ctime_r函数
- asctime/asctime_r函数支持将一个struct tm结构格式化为一个固定格式字符串;ctime/ctime_r函数支持将一个time_t变量转换为一个固定格式字符串。
- 固定格式:Www Mmm dd hh:mm:ss yyyy,Www 表示星期几,Mmm 是以字母表示的月份,dd 表示一月中的第几天,hh:mm:ss 表示时间,yyyy 表示年份
* 函数原型如下:
#include <time.h>
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);
* 示例
time_t curtime;
time(&curtime);
struct tm t;
gmtime_r(&curtime, &t);
puts(asctime(&t)
printf("当前时间 = %s", ctime(&curtime));
* 函数说明
asctime和ctime采用静态变量保存的转换结果,不是线程安全的,asctime_r和ctime_r是其线程安全版本
- 自定义格式字符串 - strftime函数
- 函数支持将一个struct tm结构格式化为一个自定义格式的字符串。
* 函数原型如下:
#include <time.h>
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
* 参数说明
s : 转换结果输出buf
max :输出buf(s)的内存空间大小
format:转换后字符串的格式
tm :输入,struct tm结构的时间
* format格式控制符号
%a : 星期几的简写形式
%A : 星期几的全称
%b : 月份的简写形式
%B : 月份的全称
%c : 日期和时间
...
更多格式请看man中的帮助说明
* 示例:
char time_str[20] = {0};
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M", localtime(&now));
* 运行结果
2020-11-28 21:52
- strftime函数可以满足大部分需求,示例:嵌入式Http服务器goahead不支持新的RFC1123标准的时间字符串,需要自主实现,如下:
RFC1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z"; //新式RFC1123标准的时间字符串格式
asctimePattern = "EEE MMM d HH:mm:ss yyyyy"; //当前goahead采用的时间字符串格式
* 使用strftime实现新标准
char buf[100] = {0};
strftime(buf,100,"%a, %d %b %Y %H:%M:%S %Z", &tm);
- 自定义格式字符串 - 自主实现
- 可通过sprintf函数实现自定义格式化。
格式化字符串转时间
- 自定义格式字符串 - strptime函数
- 函数支持将一个格式化字符串转换为一个struct tm结构,函数原型如下:
* 函数原型如下:
#include <time.h>
char *strptime(const char *buf,const char*format,struct tm *timeptr)
* 参数说明
buf : 输入的格式化时间字符串
format : 输入字符串的格式(与strftime中的格式一样)
timeptr: 输出时间转换结果
返回:返回一个指针,指向转换后的剩余字符。
* 示例:
struct tm tm;
char buf[255] = "24/Aug/2011:09:42:35";
strptime(buf, "%d/%b/%Y:%H:%M:%S" , &tm);
* 运行结果
tm中保存了转换后的时间
- 自定义格式字符串 - 自主实现
- 可通过sscanf函数实现自定义格式字符串转换成时间。
边栏推荐
- 【工控老马】单片机与西门子S7-200通信原理详解
- Kingbasees coping with transaction rollback caused by too fast growth of table age
- [translation] how Bink drives the digital loyalty transactions of some of the largest banks in the UK
- TF. Repeat and stack operations of slim
- MIPS instruction set and brief analysis
- 358. K distance interval rearrange string sorting
- tf. count_ nonzero
- [FreeRTOS] interrupt mechanism
- 719. find the distance of the number pair with the smallest K (two points)
- Some examples.
猜你喜欢
![[old horse of industrial control] detailed explanation of Siemens PLC s7-300scl programming](/img/da/08f23cfe7f8759b73576eb0638fec5.png)
[old horse of industrial control] detailed explanation of Siemens PLC s7-300scl programming

Interviewer: why does database connection consume resources? Where are the resources consumed?

Roblox sword nine sword two

Mmclassification installation and debugging

Schnuka: 3D visual inspection scheme 3D visual inspection application industry

Vulnhub's dc6 target

pycharm的虚拟环境如何共享到jupyter-lab

帆船动力学仿真分析

1032 Sharing

Some examples.
随机推荐
tf. compat. v1.assign
ROS当中的仿真时间以及Bag包操作
Mmclassification installation and debugging
Some examples.
面试官:为什么数据库连接很消耗资源,资源都消耗在哪里?
编译原理王者之路
感知健康生活 赋能无界连接 ——为OpenHarmony 3.1生态构建贡献芯海力量
tf.compat.v1.assign
[FreeRTOS] interrupt mechanism
Kingbasees v8r6 cluster maintenance case -- single instance data migration to cluster case
ES中配置ext.dic文件不生效的原因
Vulnhub's dc9 target
Appium automation test foundation ADB common commands (III)
Markdown skill tree (6): List
【深度之眼吴恩达机器学习作业班第四期】逻辑回归编程实现
tf.compat.v1.global_variables
Cv:: mat and Base64 conversion (including picture compression and decompression)
358. K 距离间隔重排字符串 排序
Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200
循环嵌套问题:为什么大循环在内,小循环在外可以提高程序的运行效率