目录

size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)

描述 (Description)

C库函数size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)根据格式定义的格式化规则格式化结构timeptr表示的时间并存储到str

声明 (Declaration)

以下是strftime()函数的声明。

size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)

参数 (Parameters)

  • str - 这是指向目标数组的指针,其中复制了生成的C字符串。

  • maxsize - 这是要复制到str的最大字符数。

  • format - 这是包含常规字符和特殊格式说明符的任意组合的C字符串。 这些格式说明符由函数替换为相应的值,以表示tm中指定的时间。 格式说明符是 -

取而代之
%a 缩写的工作日名称 Sun
%A 完整的工作日名称 Sunday
%b 缩写的月份名称 Mar
%B 全月名称 March
%c 日期和时间表示 Sun Aug 19 02:56:02 2012
%d 每月的某一天(01-31) 19
%H 小时24小时格式(00-23) 14
%I 小时12h格式(01-12) 05
%j 一年中的某一天(001-366) 231
%m 月份为十进制数字(01-12) 08
%MMinute (00-59)55
%pAM or PM designationPM
%SSecond (00-61)02
%U 第一个星期日作为第一周的第一天的周数(00-53) 33
%w 工作日为十进制数,周日为0(0-6) 4
%W 第一周一作为第一周第一天的周数(00-53) 34
%x 日期表示 08/19/12
%XTime representation02:50:06
%y 年份,最后两位数(00-99) 01
%YYear2012
%ZTimezone name or abbreviationCDT
%%A % sign%
  • timeptr - 这是指向tm结构的指针,该结构包含分解为其组件的日历时间,如下所示 -

struct tm {
   int tm_sec;         /* seconds,  range 0 to 59          */
   int tm_min;         /* minutes, range 0 to 59           */
   int tm_hour;        /* hours, range 0 to 23             */
   int tm_mday;        /* day of the month, range 1 to 31  */
   int tm_mon;         /* month, range 0 to 11             */
   int tm_year;        /* The number of years since 1900   */
   int tm_wday;        /* day of the week, range 0 to 6    */
   int tm_yday;        /* day in the year, range 0 to 365  */
   int tm_isdst;       /* daylight saving time             */	
};

返回值 (Return Value)

如果生成的C字符串适合小于大小的字符(包括终止空字符),则返回复制到str的字符总数(不包括终止空字符),否则返回零。

例子 (Example)

以下示例显示了strftime()函数的用法。

#include <stdio.h>
#include <time.h>
int main () {
   time_t rawtime;
   struct tm *info;
   char buffer[80];
   time( &rawtime );
   info = localtime( &rawtime );
   strftime(buffer,80,"%x - %I:%M%p", info);
   printf("Formatted date & time : |%s|\n", buffer );
   return(0);
}

让我们编译并运行上面的程序,它将产生以下结果 -

Formatted date & time : |08/23/12 - 12:40AM|
↑回到顶部↑
WIKI教程 @2018