linux C用strftime strptime将字符串与struct tm互转
参考摘自:https://bbs.csdn.net/topics/390674012
linux C中
strftime将struct tm转换为字符串:
NAME strftime - format date and time SYNOPSIS #include <time.h> size_t strftime(char *s, size_t max, const char *format, const struct tm *tm); DESCRIPTION The strftime() function formats the broken-down time tm according to the format specification format and places the result in the character array s of size max. RETURN VALUE Provided that the result string, including the terminating null byte, does not exceed max bytes, strf\u2010 time() returns the number of bytes (excluding the terminating null byte) placed in the array s. If the length of the result string (including the terminating null byte) would exceed max bytes, then strftime() returns 0, and the contents of the array are undefined. (This behavior applies since at least libc 4.4.4; very old versions of libc, such as libc 4.4.1, would return max if the array was too small.) Note that the return value 0 does not necessarily indicate an error. For example, in many locales %p yields an empty string. An empty format string will likewise yield an empty string. ATTRIBUTES For an explanation of the terms used in this section, see attributes(7).
strptime将字符串转换为struct tm;
NAME strptime - convert a string representation of time to a time tm structure SYNOPSIS #define _XOPEN_SOURCE /* See feature_test_macros(7) */ #include <time.h> char *strptime(const char *s, const char *format, struct tm *tm); DESCRIPTION The strptime() function is the converse function to strftime(3) and converts the character string pointed to by s to values which are stored in the tm structure pointed to by tm, using the format specified by format. RETURN VALUE The return value of the function is a pointer to the first character not processed in this function call. In case the input string contains more characters than required by the format string the return value points right after the last consumed input character. In case the whole input string is con\u2010 sumed the return value points to the null byte at the end of the string. If strptime() fails to match all of the format string and therefore an error occurred the function returns NULL. ATTRIBUTES For an explanation of the terms used in this section, see attributes(7).
实例1:strftime()
1 #include <time.h> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #include "time.h" 6 7 static char local_time[32] = {0}; 8 9 char * my_get_time(void) 10 { 11 time_t t_time; 12 struct tm *t_tm; 13 14 t_time = time(NULL); 15 t_tm = localtime(&t_time); 16 if (t_tm == NULL) 17 return NULL; 18 19 //if(strftime(local_time, sizeof(local_time), "%FT%T%z", t_tm) == 0) // 2020-01-08T09:28:54+0800 20 //if(strftime(local_time, sizeof(local_time), "%FT%T", t_tm) == 0) // 2020-01-08T09:30:27 21 //if(strftime(local_time, sizeof(local_time), "%F", t_tm) == 0) // 2020-01-08 22 if(strftime(local_time, sizeof(local_time), "%T", t_tm) == 0) // 09:32:40 23 return NULL; 24 25 return local_time; 26 } 27 28 int main(int argc, char *argv[]) 29 { 30 printf("%s\r\n", my_get_time()); 31 32 return 0; 33 }
实例2:strptime()
如下代码我是求两个时间的秒数
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 6 int main(int argc, char *argv[]) 7 { 8 //char fmt[] = "%Y-%m-%d %H:%M:%S"; 9 char fmt[] = "%F %T"; 10 time_t ggtime,curtime; 11 struct tm *tb = (struct tm*)malloc(sizeof(struct tm)); 12 13 strptime(argv[1], fmt, tb); 14 ggtime = mktime(tb); 15 16 curtime = time(NULL); 17 printf("The pause used %f seconds.\n",difftime(curtime,ggtime)); 18 19 return 0; 20 }
运行:
[root@localhost 05]# ./main '2020-1-8 10:00:02' The pause used 25.000000 seconds.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2020-01-08 两个数字交换的三种方法,Python有4种