> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/stdtime/strftime.3.md).

# strftime.3

`strftime` — 格式化日期和时间

## 名称

`strftime`

## 库

Lb libc

## 概要

`#include <time.h>`

`Ft size_t Fo strftime char * restrict buf size_t maxsize const char * restrict format const struct tm * restrict timeptr Fc Ft size_t Fn strftime_l char *restrict buf size_t maxsize const char * restrict format const struct tm *restrict timeptr locale_t loc`

## 描述

`strftime` 函数按照 `format` 所指向的字符串，将 `timeptr` 中的信息格式化到缓冲区 `buf` 中。`strftime_l` 函数与 `strftime` 功能相同，但使用显式指定的 locale 而非当前 locale。

`format` 字符串由零个或多个转换说明和普通字符组成。所有普通字符直接复制到缓冲区。转换说明由百分号“`%`”和另一个字符组成。

放置到数组中的字符数不超过 `maxsize`。若结果字符总数（包括结尾的 NUL 字符）不超过 `maxsize`，`strftime` 返回数组中的字符数（不计结尾的 NUL）。否则，返回零，缓冲区内容不确定。

转换说明按如下方式展开后复制到缓冲区：

**`%A`** 替换为星期的完整名称的本地化表示。

**`%a`** 替换为星期的缩写名称的本地化表示。

**`%B`** 替换为月份的完整名称的本地化表示。

**`%b`** 替换为月份的缩写名称的本地化表示。

**`%C`** 替换为（年份 / 100）的十进制数；个位数前补零。

**`%c`** 替换为时间和日期的本地化表示。

**`%D`** 等价于“`%m/%d/%y`”。

**`%d`** 替换为月中日期的十进制数（01-31）。

**`%E* %O*`** POSIX locale 扩展。序列 %Ec %EC %Ex %EX %Ey %EY %Od %Oe %OH %OI %Om %OM %OS %Ou %OU %OV %Ow %OW %Oy 应提供替代表示。此外，%OB 用于表示替代月份名称（单独使用，不提及日期）。

**`%e`** 替换为月中日期的十进制数（1-31）；个位数前补空格。

**`%F`** 等价于“`%Y-%m-%d`”。

**`%G`** 替换为带世纪的年份十进制数。该年为包含本周大部分天数的年份（周一作为每周的第一天）。

**`%g`** 替换为与“`%G`”相同的年份，但不带世纪的十进制数（00-99）。

**`%H`** 替换为小时（24 小时制）的十进制数（00-23）。

**`%h`** 同 `%b`。

**`%I`** 替换为小时（12 小时制）的十进制数（01-12）。

**`%j`** 替换为年中日期的十进制数（001-366）。

**`%k`** 替换为小时（24 小时制）的十进制数（0-23）；个位数前补空格。

**`%l`** 替换为小时（12 小时制）的十进制数（1-12）；个位数前补空格。

**`%M`** 替换为分钟的十进制数（00-59）。

**`%m`** 替换为月份的十进制数（01-12）。

**`%n`** 替换为换行符。

**`%O*`** 同 `%E*`。

**`%p`** 替换为“ante meridiem”（a.m.）或“post meridiem”（p.m.）的本地化表示。

**`%R`** 等价于“`%H:%M`”。

**`%r`** 等价于“`%I:%M:%S %p`”。

**`%S`** 替换为秒的十进制数（00-60）。

**`%s`** 替换为自 Epoch UTC 以来的秒数（参见 mktime(3)）。

**`%T`** 等价于“`%H:%M:%S`”。

**`%t`** 替换为制表符。

**`%U`** 替换为年中周数的十进制数（周日作为每周的第一天）（00-53）。

**`%u`** 替换为星期几的十进制数（周一作为每周的第一天）（1-7）。

**`%V`** 替换为年中周数的十进制数（周一作为每周的第一天）（01-53）。若包含 1 月 1 日的周在新年中有四天或更多，则为第 1 周；否则为上一年的最后一周，下一周为第 1 周。

**`%v`** 等价于“`%e-%b-%Y`”。

**`%W`** 替换为年中周数的十进制数（周一作为每周的第一天）（00-53）。

**`%w`** 替换为星期几的十进制数（周日作为每周的第一天）（0-6）。

**`%X`** 替换为时间的本地化表示。

**`%x`** 替换为日期的本地化表示。

**`%Y`** 替换为带世纪的年份十进制数。

**`%y`** 替换为不带世纪的年份十进制数（00-99）。

**`%Z`** 替换为时区名称。

**`%z`** 替换为与 UTC 的时区偏移量；前导加号表示 UTC 以东，减号表示 UTC 以西，小时和分钟各两位数字，中间无分隔符（RFC 822 日期头的常见格式）。

**`%+`** 替换为日期和时间的本地化表示（格式类似于 [date(1)](/man/man1/date.1.md) 产生的格式）。

**`%-*`** GNU libc 扩展。数值输出时不进行任何填充。

**`%_*`** GNU libc 扩展。显式指定使用空格填充。

**`%0*`** GNU libc 扩展。显式指定使用零填充。

**`%%`** 替换为 `%`。

## 参见

[date(1)](/man/man1/date.1.md), printf(1), [ctime(3)](/man/stdtime/ctime.3.md), [printf(3)](https://github.com/FreeBSD-Ask/freebsd-man-sc/tree/main/stdtime/printf.3.md), [strptime(3)](/man/stdtime/strptime.3.md), [wcsftime(3)](https://github.com/FreeBSD-Ask/freebsd-man-sc/tree/main/stdtime/wcsftime.3.md)

## 标准

`strftime` 函数遵循 ISO/IEC 9899:1990 ("ISO C89")，并包含大量扩展，包括 `%C`、`%D`、`%E*`、`%e`、`%G`、`%g`、`%h`、`%k`、`%l`、`%n`、`%O*`、`%R`、`%r`、`%s`、`%T`、`%t`、`%u`、`%V`、`%z`、`%+`。

`%G`、`%g` 和 `%V` 替换中特有的周数和年份定义于 ISO 8601。`strftime_l` 函数遵循 IEEE Std 1003.1-2008 ("POSIX.1")。

## 缺陷

没有月相的转换说明。

`strftime` 函数不能正确处理 `format` 参数中的多字节字符。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/stdtime/strftime.3.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
