非常教程

C参考手册

日期与时间 | Date and time

wcsftime

在头文件<wchar.h>中定义

size_t wcsftime(wchar_t * str,size_t count,const wchar_t * format,tm * time);

(自C95以来)

根据格式字符串将日期和时间信息从给定的日历时间转换time为以空字符结尾的宽str字符串format。最多count写入字节。

参数

海峡

-

指向wchar_t数组的第一个元素的输出

计数

-

要写入的最大宽字符数

格式

-

指向以空字符结尾的宽字符字符串指定转换格式的指针。格式字符串由零个或多个转换说明符和普通字符(除%以外)组成。所有普通字符(包括终止空字符)都将被复制到输出字符串而不进行修改。每个转换规范都以%字符开头,可以选择后跟E或O修饰符(如果语言环境不支持,则忽略),后面跟着决定说明符行为的字符。以下格式说明符可用:转换说明符说明使用的字段%写入文字%。完整的转换规范必须是%%。n(C99)写入换行符t(C99)写入水平制表符年份Y以十进制数字写年,例如 在IS0 8601周内从星期一开始,每年的第一周必须满足以下要求:包括1月4日包括今年的第一个星期四tm_year,tm_wday,tm_yday g(C99)写出ISO 8601以星期为基础的年份的最后2位数字,即包含指定周的年份(范围00,99)。在IS0 8601周内从星期一开始,并且一年中的第一周必须满足以下要求:包括1月4日包括今年的第一个星期四tm_year,tm_wday,tm_yday月份b写出缩写月份名称,例如Oct(区域依赖)tm_mon h( C99)b的同义词tm_mon B写完整的月份名称,例如10月(取决于语言环境)tm_mon m将月份写为十进制数字(范围01,12)tm_mon Om(C99)使用替代数字系统写月 十二而不是12 ja_JP locale tm_mon周U写一年中的一周作为十进制数(星期日是一周的第一天)(范围00,53)tm_year,tm_wday,tm_yday OU(C99)写一年的周,如%U,使用替代数字系统,例如五十二,而不是ja_JP语言环境中的52,tm_year,tm_wday,tm_yday W将年份的星期写为十进制数(星期一是一周中的第一天)(范围00 ,53)tm_year,tm_wday,tm_yday OW(C99)使用替代数字系统(例如五十二而不是52)在ja_JP locale tm_year,tm_wday,tm_yday V(C99)中写入ISO一年中的8601周(范围01,53)。在IS0 8601周内从星期一开始,一年的第一周必须满足以下要求:包括1月4日包括今年的第一个星期四tm_year,tm_wday,tm_yday OV(C99)写入年份的星期,如%V,使用替代数字系统,例如五十二,而不是ja_JP语言环境中的52,tm_year,tm_wday,tm_yday每年的月份/月份j作为十进制数字(范围001,366)写入一年中的某一天tm_yday d以十进制数字形式显示月份的日期(范围为01,31)tm_mday Od(C99)写入月份的零基日期使用替代数字系统,例如ja_JP语言环境中的二十七而不是23单个字符前面有一个空格。tm_mday e(C99)将月份中的某一天写为十进制数(范围为1,31)。单个数字前面有一个空格。tm_mday Oe(C99)使用替代数字系统编写一个月的一天,例如 二十七而不是ja_JP语言环境中的27单个字符前面有一个空格。tm_mday一周中的某天写一个缩写星期几名称,例如Fri(区域设置相关)tm_wday A写出完整的星期几名称,例如星期五(区域设置相关)tm_wday w写入星期几作为十进制数字,其中星期日为0(范围0-6)tm_wday Ow(C99)使用替代数字系统(例如ja_JP区域设置tm_wday u(C99)将工作日写为十进制数,其中星期一为1(ISO 8601格式)(范围1 -7)tm_wday Ou(C99)使用替代数字系统写入星期几,其中星期一为1,例如ja_JP语言环境中的二代替2代表tm_wday小时,分钟,秒H将小时数写为十进制数,

Conversion specifier

Explanation

Used fields

%

writes literal %. The full conversion specification must be %%.

n(C99)

writes newline character

t(C99)

writes horizontal tab character

Year

Y

writes year as a decimal number, e.g. 2017

tm_year

EY(C99)

writes year in the alternative representation, e.g.平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale

tm_year

y

writes last 2 digits of year as a decimal number (range 00,99)

tm_year

Oy(C99)

writes last 2 digits of year using the alternative numeric system, e.g. 十一 instead of 11 in ja_JP locale

tm_year

Ey(C99)

writes year as offset from locale's alternative calendar period %EC (locale-dependent)

tm_year

C(C99)

writes first 2 digits of year as a decimal number (range 00,99)

tm_year

EC(C99)

writes name of the base year (period) in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JP

tm_year

G(C99)

writes ISO 8601 week-based year, i.e. the year that contains the specified week. In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements: Includes January 4 Includes first Thursday of the year

tm_year, tm_wday, tm_yday

g(C99)

writes last 2 digits of ISO 8601 week-based year, i.e. the year that contains the specified week (range 00,99). In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements: Includes January 4 Includes first Thursday of the year

tm_year, tm_wday, tm_yday

Month

b

writes abbreviated month name, e.g. Oct (locale dependent)

tm_mon

h(C99)

synonym of b

tm_mon

B

writes full month name, e.g. October (locale dependent)

tm_mon

m

writes month as a decimal number (range 01,12)

tm_mon

Om(C99)

writes month using the alternative numeric system, e.g. 十二 instead of 12 in ja_JP locale

tm_mon

Week

U

writes week of the year as a decimal number (Sunday is the first day of the week) (range 00,53)

tm_year, tm_wday, tm_yday

OU(C99)

writes week of the year, as by %U, using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale

tm_year, tm_wday, tm_yday

W

writes week of the year as a decimal number (Monday is the first day of the week) (range 00,53)

tm_year, tm_wday, tm_yday

OW(C99)

writes week of the year, as by %W, using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale

tm_year, tm_wday, tm_yday

V(C99)

writes ISO 8601 week of the year (range 01,53). In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements: Includes January 4 Includes first Thursday of the year

tm_year, tm_wday, tm_yday

OV(C99)

writes week of the year, as by %V, using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale

tm_year, tm_wday, tm_yday

Day of the year/month

j

writes day of the year as a decimal number (range 001,366)

tm_yday

d

writes day of the month as a decimal number (range 01,31)

tm_mday

Od(C99)

writes zero-based day of the month using the alternative numeric system, e.g 二十七 instead of 23 in ja_JP locale Single character is preceded by a space.

tm_mday

e(C99)

writes day of the month as a decimal number (range 1,31). Single digit is preceded by a space.

tm_mday

Oe(C99)

writes one-based day of the month using the alternative numeric system, e.g. 二十七 instead of 27 in ja_JP locale Single character is preceded by a space.

tm_mday

Day of the week

a

writes abbreviated weekday name, e.g. Fri (locale dependent)

tm_wday

A

writes full weekday name, e.g. Friday (locale dependent)

tm_wday

w

writes weekday as a decimal number, where Sunday is 0 (range 0-6)

tm_wday

Ow(C99)

writes weekday, where Sunday is 0, using the alternative numeric system, e.g. 二 instead of 2 in ja_JP locale

tm_wday

u(C99)

writes weekday as a decimal number, where Monday is 1 (ISO 8601 format) (range 1-7)

tm_wday

Ou(C99)

writes weekday, where Monday is 1, using the alternative numeric system, e.g. 二 instead of 2 in ja_JP locale

tm_wday

Hour, minute, second

H

writes hour as a decimal number, 24 hour clock (range 00-23)

tm_hour

OH(C99)

writes hour from 24-hour clock using the alternative numeric system, e.g. 十八 instead of 18 in ja_JP locale

tm_hour

I

writes hour as a decimal number, 12 hour clock (range 01,12)

tm_hour

OI(C99)

writes hour from 12-hour clock using the alternative numeric system, e.g. 六 instead of 06 in ja_JP locale

tm_hour

M

writes minute as a decimal number (range 00,59)

tm_min

OM(C99)

writes minute using the alternative numeric system, e.g. 二十五 instead of 25 in ja_JP locale

tm_min

S

writes second as a decimal number (range 00,60)

tm_sec

OS(C99)

writes second using the alternative numeric system, e.g. 二十四 instead of 24 in ja_JP locale

tm_sec

Other

c

writes standard date and time string, e.g. Sun Oct 17 04:41:13 2010 (locale dependent)

all

Ec(C99)

writes alternative date and time string, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale

all

x

writes localized date representation (locale dependent)

all

Ex(C99)

writes alternative date representation, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale

all

X

writes localized time representation (locale dependent)

all

EX(C99)

writes alternative time representation (locale dependent)

all

D(C99)

equivalent to "%m/%d/%y"

tm_mon, tm_mday, tm_year

F(C99)

equivalent to "%Y-%m-%d" (the ISO 8601 date format)

tm_mon, tm_mday, tm_year

r(C99)

写本地化的12小时时钟时间(取决于语言环境)

tm_hour,tm_min,tm_sec

R(C99)

相当于“%H:%M”

tm_hour,tm_min

T(C99)

相当于“%H:%M:%S”(ISO 8601时间格式)

tm_hour,tm_min,tm_sec

p

写本地化的上午或下午(区域依赖)

tm_hour

与(C99)

以ISO 8601格式(例如-0430)写入UTC的偏移量,如果时区信息不可用,则不写入字符

tm_isdst的

写时区名称或缩写,或者如果时区信息不可用,则不包含字符(取决于语言环境)

tm_isdst的

转换说明符

说明

使用的字段

%

写文字%。完整的转换规范必须是%%。

N(C99)

写换行符

吨(C99)

写水平制表符

| 年份|

| Y | 将年份写为十进制数字,例如2017 | tm_year |

| EY(C99)| 在替代表示中写入年份,例如平成23年(平成23年)而不是2011年(2011年)在ja_JP地区| tm_year |

| y | 将年份的最后2位数字写为十进制数(范围为00,99)| tm_year |

| Oy(C99)| 使用替代数字系统写入年份的最后2位数字,例如ja_JP locale |中的十一而不是11 tm_year |

| Ey(C99)| 将年份写入与区域设置的替代日历期%EC(区域依赖)|的偏移量 tm_year |

| C(C99)| 写一年的前两位数作为十进制数(范围为00,99)| tm_year |

| EC(C99)| 在地区的替代表示中写入基准年(期间)的名称,例如平成(平成时代)in ja_JP | tm_year |

| G(C99)| 写ISO 8601以周为基础的年份,即包含指定周的年份。在IS0 8601周内从星期一开始,并且一年中的第一周必须满足以下要求:包括1月4日包括今年的第一个星期四| tm_year,tm_wday,tm_yday |

| g(C99)| 写出ISO 8601以周为单位的年份的最后2位数字,即包含指定周的年份(范围00,99)。在IS0 8601周内从星期一开始,并且一年中的第一周必须满足以下要求:包括1月4日包括今年的第一个星期四| tm_year,tm_wday,tm_yday |

| 月|

| b | 写出缩写的月份名称,例如Oct(区域依赖)| tm_mon |

| h(C99)| b |的同义词 tm_mon |

| B | 写完整的月份名称,例如10月(取决于语言环境)| tm_mon |

| m | 将月份作为十进制数字(范围为01,12)|写入 tm_mon |

| Om(C99)| 使用替代数字系统写月份,例如ja_JP locale |中的十二而不是12 tm_mon |

| 星期|

| U | 写一年中的一周作为十进制数(星期日是一周中的第一天)(范围00,53)| tm_year,tm_wday,tm_yday |

| OU(C99)| 写一年中的一周,如%U,使用替代数字系统,例如五十二而不是ja_JP语言环境中的52。tm_year,tm_wday,tm_yday |

| W | 写一年中的一周作为十进制数(星期一是一周中的第一天)(范围00,53)| tm_year,tm_wday,tm_yday |

| OW(C99)| 用%W写出一年中的第一周,使用替代数字系统,例如ja_JP语言环境中的五十二而不是52。tm_year,tm_wday,tm_yday |

| V(C99)| 写一年的ISO 8601周(范围01,53)。在IS0 8601周内从星期一开始,并且一年中的第一周必须满足以下要求:包括1月4日包括今年的第一个星期四| tm_year,tm_wday,tm_yday |

| OV(C99)| 写出一年中的一周,如%V所示,使用替代数字系统,例如ja_JP语言环境中的五十二而不是52。tm_year,tm_wday,tm_yday |

| 一年中的每月|

| j | 写一年中的一天作为十进制数(范围001,366)| tm_yday |

| d | 将月份的日期写为十进制数(范围为01,31)| tm_mday |

| Od(C99)| 使用替代数字系统写出从零开始的一天的日期,例如ja_JP语言环境中的二十七而不是23。单个字符前面有一个空格。| tm_mday |

| e(C99)| 将月份的日期写为十进制数(范围1,31)。单个数字前面有一个空格。| tm_mday |

| Oe(C99)| 使用替代数字系统写一个月的一天,例如ja_JP语言环境中的二十七而不是27。单个字符前面有一个空格。| tm_mday |

| 一周中的星期几|

| a | 写缩写的星期几名称,例如Fri(locale dependent)| tm_wday |

| A | 写完整的星期几名称,例如Friday(locale dependent)| tm_wday |

| w | 写星期几作为十进制数,星期日为0(范围0-6)| tm_wday |

| Ow(C99)| 写星期几,星期日为0,使用替代数字系统,例如ja_JP locale |中的二而不是2 tm_wday |

| 你(C99)| 写星期几作为十进制数,星期一为1(ISO 8601格式)(范围1-7)| tm_wday |

| Ou(C99)| 写星期几,星期一为1,使用替代数字系统,例如ja_JP locale |中的二而不是2 tm_wday |

| 小时,分钟,秒|

| H | 将小时数写为十进制数,24小时制(范围00-23)| tm_hour |

| OH(C99)| 使用替代数字系统从24小时制写小时,例如ja_JP locale |中的十八而不是18 tm_hour |

| 我| 将小时数写为十进制数,12小时制(范围01,12)| tm_hour |

| OI(C99)| 使用替代数字系统从12小时时钟写入小时,例如ja_JP locale中的六而不是06 tm_hour |

| M | 将分钟写入十进制数(范围为00,59)| tm_min |

| OM(C99)| 使用替代数字系统写分钟,例如ja_JP locale |中的二十五而不是25 tm_min |

| S | 写第二个作为十进制数(范围00,60)| tm_sec |

| OS(C99)| 使用替代数字系统写第二个,例如ja_JP locale |中的二十四而不是24 tm_sec |

| 其他|

| c | 写标准的日期和时间字符串,例如Sun 10月17日04:41:13 2010(取决于语言环境)| 全部|

| Ec(C99)| 写替代日期和时间字符串,例如在ja_JP语言环境中使用平成23年(平成23年)而不是2011年(2011年)全部|

| x | 写本地化的日期表示法(依赖于语言环境)| 全部|

| Ex(C99)| 写替代日期表示法,例如在ja_JP语言环境中使用平成23年(平成23年)而不是2011年(2011年)全部|

| X | 写本地化的时间表示(区域依赖)| 全部|

| EX(C99)| 写入替代时间表示(取决于语言环境)| 全部|

| D(C99)| 相当于“%m /%d /%y”| tm_mon,tm_mday,tm_year |

| F(C99)| 相当于“%Y-%m-%d”(ISO 8601日期格式)| tm_mon,tm_mday,tm_year |

| r(C99)| 写本地化的12小时时钟时间(取决于语言环境)| tm_hour,tm_min,tm_sec |

| R(C99)| 相当于“%H:%M”| tm_hour,tm_min |

| T(C99)| 相当于“%H:%M:%S”(ISO 8601时间格式)| tm_hour,tm_min,tm_sec |

| p | 写本地化的上午或下午(区域依赖)| tm_hour |

| z(C99)| 写入ISO 8601格式的UTC偏移量(例如-0430),如果时区信息不可用,则不显示字符| tm_isdst |

| Z | 写时区名称或缩写,或者如果时区信息不可用(取决于语言环境),则为无字符| tm_isdst |

  • 包括1月4日
  • 包括一年的第一个星期四

tm_year, tm_wday, tm_yday g

(C99)写出ISO 8601以周为单位的年份的后两位数字,即包含指定周(范围[00,99])的年份。在IS0 8601周内从星期一开始,一年的第一周必须满足以下要求:

  • 包括1月4日
  • 包括一年的第一个星期四

tm_yeartm_wdaytm_ydayb缩写的月份名称,例如Oct(位置有关) tm_mon h

(C99)b tm_mon B完整月份名称的同义词,例如October(区域设置相关) tm_mon m月份写为十进制数(范围[01,12]tm_mon Om

(C99)写道使用可替代数字系统,如十二代替12 Ja_JP表示区域 tm_monU了一年的星期为十进制数(星期日是一周的第一天)(范围[00,53]tm_yeartm_wdaytm_yday OU

(C99)写道一周年通过,%U使用替代的数字系统,如五十二,而不是在Ja_JP表示区域52 tm_yeartm_wdaytm_yday W了一年的星期为十进制数(星期一是一周的第一天)(范围[00,53]tm_yeartm_wdaytm_yday OW

(C99)写道星期的一年,通过%W使用替代的数字系统,如五十二,而不是在Ja_JP表示区域52 tm_yeartm_wdaytm_yday V

(C99)写入一年中的ISO 8601周(范围[01,53])。在IS0 8601周内从星期一开始,一年的第一周必须满足以下要求:

  • 包括1月4日
  • 包括一年的第一个星期四

tm_year, tm_wday, tm_yday OV

(C99)写入周一年中通过,%V使用替代的数字系统,例如五十二代替Ja_JP表示区域52 tm_yeartm_wdaytm_yday 年/月的日 j 写入一年中的哪一天为十进制数(范围[001,366]tm_yday d 写入作为十进制数(范围[01,31]的月份tm_mday Od

(C99)使用替代数字系统编写月份的零基日期,例如ja_JP语言环境中的二十七而不是23。单个字符前面有一个空格。

tm_mday e

(C99)以十进制数(范围)的形式写入当月的某天[1,31]。单个数字前面有一个空格。

tm_mday Oe

(C99)使用替代数字系统写一个月一天,例如ja_JP语言环境中的二十七而不是27。单个字符前面有一个空格。

tm_mday 星期 a简写的星期名称,例如Fri(位置有关) tm_wday A完整的星期几的名称,如Friday(位置有关) tm_wday w 写入平日的十进制数,星期日为0(范围[0-6]tm_wday Ow

(C99)使用替代数字系统写周日,其中星期日是0,例如ja_JP语言环境中的二而不是2 tm_wday u

(C99)将星期几写为十进制数,星期一为1(ISO 8601格式)(范围[1-7]tm_wday Ou

(C99)写入平日,其中星期一是1,使用备选数字系统,例如,二,而不是在区域设置Ja_JP表示2 tm_wday 时,分,秒 H 写入小时为十进制数,24小时时钟(范围[00-23]tm_hour OH

(C99)写入小时从24小时时钟使用备选数字系统,例如十八代替18的区域设置Ja_JP表示 tm_hour I 写入小时为十进制数,12个小时的时钟(范围[01,12]tm_hour OI

(C99)使用替代数字系统从12小时时钟写入小时,例如ja_JP语言环境中的六而不是06 tm_hour M 写入分钟数(范围[00,59]tm_min OM

(C99)使用替代数字系统写入分钟,例如ja_JP语言环境中的二十五而不是25 tm_min S 写入第二个作为十进制数(范围[00,60]tm_sec OS

(C99)使用替代数字系统写第二个,例如ja_JP语言环境中的二十四而不是24个 tm_sec 其他 c 写入标准日期和时间字符串,例如Sun Oct 17 04:41:13 2010(取决于语言环境)全部 Ec

(C99)写入备选日期和时间字符串,例如在ja_JP语言环境中使用平成23年(年平成23年)而不是2011年(2011年),全部 x 写入本地化日期表示(与语言环境有关)全部 Ex

(C99)在ja_JP语言环境中使用平成23年(年平23)而不是2011年(2011年)编写替代日期表示法,全部 X 写入本地时间表示(与语言环境有关)全部 EX

(C99)全部写入替代时间表示(区域依赖) D

(C99),相当于“%米/%d /%Y” ,tm_montm_mdaytm_year F

(C99),相当于“%Y-%间- %d” (在ISO 8601的日期格式) tm_montm_mdaytm_year r

(C99)写入局部12小时制的时间(当前位置有关) tm_hourtm_mintm_sec R

(C99)相当于“%H:%M” tm_hourtm_min T

(C99),相当于“%H:%M:%S” (在ISO 8601时间格式) tm_hourtm_mintm_sec p 写入局部上午或下午(当前位置有关) tm_hour z

(C99)以ISO 8601格式(例如)写入UTC的偏移量,-0430如果时区信息不可用,则不tm_isdst Z 写入字符; 写入时区名称或缩写 ; 如果时区信息不可用,则 写入无字符(取决于语言环境) tm_isdst

返回值

指向宽字符数组的宽字符数str不包括终止L'\0'成功。如果count在整个字符串可以存储之前达到,​0​则返回并且内容未定义。

#include <stdio.h>
#include <time.h>
#include <wchar.h>
#include <locale.h>
 
int main(void)
{
    wchar_t buff[40];
    struct tm my_time = { .tm_year=112, // = year 2012
                          .tm_mon=9,    // = 10th month
                          .tm_mday=9,   // = 9th day
                          .tm_hour=8,   // = 8 hours
                          .tm_min=10,   // = 10 minutes
                          .tm_sec=20    // = 20 secs
    };
 
    if (wcsftime(buff, sizeof buff, L"%A %c", &my_time)) {
        printf("%ls\n", buff);
    } else {
        puts("wcsftime failed");
    }
 
    setlocale(LC_ALL, "ja_JP.utf8");
 
    if (wcsftime(buff, sizeof buff, L"%A %c", &my_time)) {
        printf("%ls\n", buff);
    } else {
        puts("wcsftime failed");
    }
}

输出:

Sunday Sun Oct  9 08:10:20 2012
日曜日 2012年10月09日 08時10分20秒

参考

  • C11标准(ISO / IEC 9899:2011):
    • 7.29.5.1 wcsftime函数(p:439-440)
  • C99标准(ISO / IEC 9899:1999):
    • 7.24.5.1 wcsftime函数(p:385-386)
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。