> 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/tzset.3.md).

# tzset.3

`daylight` — 初始化时间转换信息

## 名称

`daylight`, `timezone`, `tzname`, `tzset`

## 库

Lb libc

## 概要

`#include <time.h>`

`extern int daylight; extern long timezone; extern char *tzname[2]; Ft void Fn tzset void`

## 描述

`tzset` 函数初始化库例程 [localtime(3)](/man/stdtime/ctime.3.md) 所使用的时间转换信息。环境变量 `TZ` 指定具体的初始化方式。

若环境中不存在 `TZ`，则使用由 tzfile(5) 格式文件 **/etc/localtime** 所指定的、对本地挂钟时间的最佳可用近似。

若环境中存在 `TZ` 但其值为空字符串，则使用协调世界时（UTC）（不进行闰秒修正）。

若环境中存在 `TZ` 且其值以冒号（`:`）开头，则该值的其余部分作为 tzfile(5) 格式文件的路径名，从中读取时间转换信息。若路径名的首字符为斜杠（`/`），则视为绝对路径名；否则视为相对于系统时间转换信息目录的路径名。

若其值不以冒号开头，则首先将其作为文件路径名（如上所述）以读取时间转换信息。若该文件无法读取，则将该值解释为时间转换信息的直接规范（格式见下文）。

若 `TZ` 环境变量既未指定 tzfile(5) 格式文件，也无法解释为直接规范，则使用 UTC。

首次调用 `tzset` 之后，`timezone` 变量被设置为协调世界时（UTC）与本地时间之间的差值（以秒计）。若本地时区采用标准时间，`daylight` 变量被设置为 0。若本地时区采用替代时间（如夏令时），则设置为 1。`tzname` 数组的第一个元素是标准时间的时区名，第二个元素是替代时区的名称。

## 规范格式

当 `TZ` 直接用作时间转换信息的规范时，必须采用以下语法（为清晰起见插入了空格）：

```sh
std offset[dst[offset][,rule]]
```

其中：

**`std`** 和 **`dst`**

作为标准（`std`）或夏令（`dst`）时区名称的三个或更多字节。仅 `std` 是必需的；若缺少 `dst`，则此 locale 不适用夏令时。明确允许大小写字母。除前导冒号（`:`）、数字、逗号（`,`）、减号（`-`）、加号（`+`）和 ASCII `NUL` 之外的任何字符均允许。

**`offset`**

指示为得到协调世界时须加到本地时间上的值。`offset` 的形式为：

```sh
hh[:mm[:ss]]
```

分钟（`mm`）和秒（`ss`）是可选的。小时（`hh`）是必需的，可以是单个数字。`std` 之后的 `offset` 是必需的。若 `dst` 之后没有 `offset`，则假定夏令时比标准时间早一小时。可使用一位或多位数字；该值始终被解释为十进制数。小时必须在 0 到 24 之间，分钟（和秒）——若存在——必须在 0 到 59 之间。若前面有（`-`），该时区应位于本初子午线以东；否则应位于以西（可由可选的前导（`+`）表示）。

**`rule`**

指示何时切换到夏令时以及何时切回。`rule` 的形式为：

```sh
date/time,date/time
```

其中第一个 `date` 描述从标准时间切换到夏令时的时间，第二个 `date` 描述切回的时间。每个 `time` 字段描述在当前本地时间下何时切换到另一时间。

`date` 的格式为以下之一：

**`J`** *n* 儒略日 *n*（1 ≤ *n* ≤ 365）。闰日不计入；即在所有年份中——包括闰年——2 月 28 日是第 59 天，3 月 1 日是第 60 天。无法显式引用偶尔出现的 2 月 29 日。

*n* 从零开始的儒略日（0 ≤ *n* ≤ 365）。闰日计入，可以引用 2 月 29 日。

**`M`** *m.n.d* 一年中第 *m* 月（1 ≤ *m* ≤ 12）第 *n* 周（1 ≤ *n* ≤ 5）的第 *d* 天（0 ≤ *d* ≤ 6），其中第 5 周表示“第 *m* 月中最后一个第 *d* 天”（可能出现在第四周或第五周）。第 1 周是该第 *d* 天首次出现的周。第零天是星期日。`time` 与 `offset` 格式相同，但不允许前导符号（`-`）或（`+`）。若未给出 `time`，默认为 **02:00:00**。

若 `TZ` 规范中不存在 `rule`，则使用系统时间转换信息目录中 tzfile(5) 格式文件 `posixrules` 所指定的规则，其中 UTC 的标准和夏令时偏移量由 `TZ` 中的 `offset` 值替换。

为兼容 System V Release 3.1，可使用分号（`;`）将 `rule` 与规范的其余部分分隔。

## 文件

**/etc/localtime** 本地时区文件

**/usr/share/zoneinfo** 时区目录

**/usr/share/zoneinfo/posixrules** POSIX 风格 TZ 的规则

**/usr/share/zoneinfo/Etc/GMT** 用于 UTC 闰秒

若文件 **/usr/share/zoneinfo/UTC** 不存在，则从 **/usr/share/zoneinfo/posixrules** 加载 UTC 闰秒。

## 参见

[date(1)](/man/man1/date.1.md), [gettimeofday(2)](/man/sys/gettimeofday.2.md), [ctime(3)](/man/stdtime/ctime.3.md), [getenv(3)](/man/stdlib/getenv.3.md), [time(3)](/man/gen/time.3.md), tzfile(5)

## 历史

`tzset` 函数首次出现于 4.4BSD。


---

# 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/tzset.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.
