> 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/man9/inittodr.9.md).

# inittodr.9

`inittodr` — 初始化系统时间

## 名称

`inittodr`

## 概要

```c
#include <sys/types.h>
```

```c
#include <sys/systm.h>
```

```c
void
inittodr(time_t base)
```

## 描述

`inittodr` 函数确定时间并设置系统时钟。它尝试使用一组启发式方法，通过检查系统的电池后备时钟和从根文件系统获得的时间（如 `base` 所给）来选取正确的时间。如何获得 `base` 值因根文件系统类型而异。所使用的启发式方法包括：

* 如果电池后备时钟具有有效时间，则使用该时间。
* 如果电池后备时钟没有有效时间，则使用 `base` 中提供的时间。

一旦确定了系统时间，就将其存储在 `time` 变量中。

## 诊断

如果 `inittodr` 函数在确定系统时间时遇到困难，会打印诊断消息。可能导致打印诊断消息的情况包括：

* 电池后备时钟的时间看起来不合理。

## 参见

[resettodr(9)](/man/man9/resettodr.9.md), [time(9)](/man/man9/time.9.md)

## 缺陷

在许多系统上，`inittodr` 必须将以年、月、日、时、分和秒表示的时间转换为以秒表示的 `time`。许多实现本可以共享代码，但实际上并没有。

每个系统选取正确时间的启发式方法略有不同。

FreeBSD 实现在电池后备时钟不可用时，应该更好地验证 `base` 中提供的时间。目前它会无条件地将系统时钟设置为此值。


---

# 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/man9/inittodr.9.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.
