linux 时间格式化函数strftime和strptime使用
来源:互联网 发布:sql合并查询结果 编辑:程序博客网 时间:2024/05/17 20:12
linux 时间格式化函数strftime和strptime使用
这两个函数都是时间日期的格式控制函数,在功能上看起来正好相反。strftime将一个struct tm结构格式化为一个字符串,strptime则是将一个字符串格式化为一个struct tm结构。
l strftime
函数原型:size_t strftime(char *s,size_t maxsize,char *format,conststruct tm *timeptr)
strftime函数对timeptr指向的tm结构所代表的时间和日期进行格式编排,其结果放在字符串s中。该字符串的长度被设置为(最少)maxsize个字符。格式字符串format用来对写入字符串的字符进行控制,它包含着将被传送到字符串里去的普通字符以及编排时间和日期格式的转换控制符。
输入:const struct tm *timeptr
输出:char *s,size_t maxsize,char *format
strftime函数库中原型:
/* Format TP into S according to FORMAT.Write no more than MAXSIZEcharacters and return the number of characters written, or 0 if it would exceedMAXSIZE. */extern size_t strftime (char *__restrict __s, size_t __maxsize, __const char *__restrict__format, __const struct tm *__restrict__tp) __THROW;
转换控制符见下表
转换控制符
说明
%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
当地格式的日期
%X
当地格式的时间
%y
年份中的最后两位数,00-99
%Y
年
%Z
地理时区名称
具体的控制转换符说明可以查看man.
l strptime
函数原型: char *strptime(const char *buf,const char*format,struct tm *timeptr)
format字符串的构建方式和strftime的format字符串完全一样,strptime返回一个指针,指向转换过程处理的最后一个字符后面的那个字符,
输入:const char *buf,const char *format
输出:struct tm *timeptr
strptime库函数原型
/* Parse Saccording to FORMAT and store binary time information in TP. The return value is a pointer to the firstunparsed character in S. */extern char*strptime (__const char *__restrict __s, __const char *__restrict __fmt,struct tm *__tp) __THROW;
实例代码:
#include <time.h>#include <stdio.h>#include <string.h>int main() { struct tm tm; char buf[255]; strptime("24/Aug/2011:09:42:35", "%d/%b/%Y:%H:%M:%S" , &tm); printf("asctime:%s\n",asctime(&tm)); memset(buf,0,sizeof(buf)); strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm); puts(buf); return 0; }
- linux 时间格式化函数strftime和strptime使用
- linux 时间格式化函数strftime和strptime使用
- strftime和strptime使用
- strftime和strptime使用
- strftime和strptime使用(时间日期的格式控制函数)
- strftime和strptime使用(时间日期的格式控制函数)
- strftime和strptime使用(时间日期的格式控制函数)
- c++时间格式转换--strftime和strptime使用
- strptime与strftime函数的使用
- 时间格式化函数strftime
- 时间格式化函数--strftime
- strftime、strptime和stime的使用
- 5.1.8 strftime()和strptime()的使用
- strftime,strptime系统函数
- strftime() 函数---> 时间格式化
- strftime() 函数将时间格式化
- strftime函数将时间格式化
- strftime函数将时间格式化
- 跟燕十八学习PHP-第十八天-开发项目(2)
- windows 下安装 eclipse for c /C++出现的问题
- java连接MYSQL
- 跟燕十八学习PHP-第十九天-热身项目完善
- 开源传感器网络平台OpenWSN
- linux 时间格式化函数strftime和strptime使用
- 设计模式-抽象工厂模式
- BUG:使用android:layout_weight权重导致对齐出错
- Ubuntu安装中文语言包
- Shell分析access_log
- 理解inode笔记
- 【小技巧】Select下拉列表覆盖DIV菜单的另类解决方案
- Sharepoint 性能考量
- AWStats+QQ纯真库IP解析插件安装