> For the complete documentation index, see [llms.txt](https://man.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://man.bsdcn.org/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)](/man9/resettodr.9.md), [time(9)](/man9/time.9.md)

## 缺陷

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

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

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