#include <stdio.h>
#include <time.h>
int main() {
struct tm tm;
char buf[255];
strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
puts(buf);
return 0;
}
這兩個函數都是時間日期的格式控制函數,在功能上看起來正好相反。
size_t strftime(char *s,size_t maxsize,char *format,const struct tm *timeptr)
作用:strftime將一個tm結構格式化為一個字符串
參數:format為輸出數據格式
char *strptime(const char *buf,const char *format,struct tm *timeptr)
作用:strptime則是將一個字符串格式化為一個tm結構。
參數:buf為要修改的時間,一般為GPS時間
Format為數據格式,與buf時間格式必須一致,要不然會有段錯誤
%a |
星期幾的簡寫形式 |
%A |
星期幾的全稱 |
%b |
月份的簡寫形式 |
%B |
月份的全稱 |
%c |
日期和時間 |
%d |
月份中的日期,0-31 |
%H |
小時,00-23 |
%I |
12進制小時鍾點,01-12 |
%j |
年份中的日期,001-366 |
%m |
年份中的月份,01-12 |
%M |
分,00-59 |
%p |
上午或下午 |
%S |
秒,00-60 |
%u |
星期幾,1-7 |
%w |
星期幾,0-6 |
%x |
當地格式的日期 |