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

# time.9

`boottime` — 系统时间变量

## 名称

`boottime`, `time_second`, `time_uptime`

## 概要

`#include <sys/time.h>`

`extern struct timeval boottime;`

`extern time_t time_second;`

`extern time_t time_uptime;`

## 描述

`boottime` 变量保存系统启动时间的估计值。此时间在系统启动时最初设置，可来自 RTC，也可来自根据系统根文件系统估计的时间。当设置当前系统时间、由 ntpd(8) 步进或在系统恢复时从 RTC 读取新时间时，`boottime` 将重新计算为 new\_time - uptime。[sysctl(8)](/man/man8/sysctl.8.md) `kern.boottime` 返回此值。

`time_second` 变量是系统到秒级精度的"墙上时间"时钟。

`time_uptime` 变量是自启动以来的秒数。

bintime(9)、getbintime(9)、[microtime(9)](/man/man9/microtime.9.md)、getmicrotime(9)、nanotime(9) 和 getnanotime(9) 函数可用于以更精确和原子的方式获取当前时间。类似地，binuptime(9)、getbinuptime(9)、[microuptime(9)](/man/man9/microuptime.9.md)、getmicrouptime(9)、nanouptime(9) 和 getnanouptime(9) 函数可用于以更精确和原子的方式获取自启动以来经过的时间。`boottime` 变量可以在不加特殊保护的情况下读写。它会在系统时间相位改变时调整。

## 参见

clock\_settime(2), ntp\_adjtime(2), settimeofday(2), bintime(9), binuptime(9), getbintime(9), getbinuptime(9), getmicrotime(9), getmicrouptime(9), getnanotime(9), getnanouptime(9), [microtime(9)](/man/man9/microtime.9.md), [microuptime(9)](/man/man9/microuptime.9.md), nanotime(9), nanouptime(9)

> Poul-Henning Kamp, "Timecounters: Efficient and precise timekeeping in SMP kernels", *Proceedings of EuroBSDCon 2002, Amsterdam*, **/usr/share/doc/papers/timecounter.ascii.gz**.

> Marshall Kirk McKusick, George V. Neville-Neil, *The Design and Implementation of the FreeBSD Operating System*, pp. 57-61,65-66, Addison-Wesley, July 2004.


---

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