# How Fastfetch Detects System Uptime: Cross-Platform Implementation Explained

> Discover how Fastfetch detects system uptime by accessing platform-specific kernel interfaces. Learn about its cross-platform implementation for Linux, Windows, and BSD.

- Repository: [fastfetch-cli/fastfetch](https://github.com/fastfetch-cli/fastfetch)
- Tags: internals
- Published: 2026-03-30

---

**Fastfetch detects system uptime through platform-specific kernel interfaces that measure elapsed milliseconds since boot, normalizing the results into a unified data structure across Linux, Windows, BSD, and other operating systems.**

The fastfetch-cli/fastfetch repository implements uptime detection as a modular component that abstracts OS-level differences behind a common API. When users run the uptime module, the application delegates to compiled platform-specific detectors that query the kernel directly, ensuring accurate measurements regardless of the underlying architecture.

## The Uptime Detection Architecture

The detection flow begins in [`src/modules/uptime/uptime.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/modules/uptime/uptime.c) where the `ffPrintUptime` function serves as the entry point. This function invokes the platform-agnostic detector `ffDetectUptime`, declared in [`src/detection/uptime/uptime.h`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/uptime/uptime.h), which resolves at compile time to an OS-specific implementation.

Each detector populates an `FFUptimeResult` struct with two critical fields:
- **`uptime`** – elapsed milliseconds since system boot
- **`bootTime`** – absolute epoch time in milliseconds when the system started

The common calculation pattern across all platforms derives boot time by subtracting uptime from the current epoch time:

```c
result->uptime = /* platform-specific elapsed ms */;
result->bootTime = ffTimeGetNow() - result->uptime;

```

The `ffTimeGetNow()` function, implemented in [`src/common/impl/time.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/common/impl/time.c), provides the current epoch time in milliseconds, enabling consistent temporal representation across disparate operating systems.

## Platform-Specific Detection Strategies

Fastfetch maintains separate detection files under `src/detection/uptime/` for each supported platform, selecting the most reliable kernel interface available.

### Linux: /proc/uptime and clock_gettime

On Linux systems, [`src/detection/uptime/uptime_linux.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/uptime/uptime_linux.c) implements a two-tier detection strategy. The primary method reads the first field from `/proc/uptime`, which reports seconds since boot as a floating-point value, converting this to milliseconds. If this file is unavailable (such as on Android) or parsing fails, the detector falls back to `clock_gettime(CLOCK_BOOTTIME)`, which directly returns the elapsed time since boot with nanosecond precision.

### Windows: SharedUserData InterruptTime

The Windows implementation in [`src/detection/uptime/uptime_windows.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/uptime/uptime_windows.c) accesses the kernel-shared `SharedUserData->InterruptTime` structure. This value stores uptime in 100-nanosecond units, which the detector divides by 10,000 to obtain milliseconds. The boot time is then calculated by subtracting this uptime from the current time returned by `ffTimeGetNow()`.

### BSD Systems: sysctl KERN_BOOTTIME

For FreeBSD, NetBSD, OpenBSD, and derivatives, [`src/detection/uptime/uptime_bsd.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/uptime/uptime_bsd.c) utilizes the `sysctl` system call with the `CTL_KERN` and `KERN_BOOTTIME` parameters to retrieve the absolute boot time. The detector calculates uptime by computing the difference between the current time and this boot timestamp.

### SunOS: utmpx Database

On SunOS systems, [`src/detection/uptime/uptime_sunos.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/uptime/uptime_sunos.c) iterates through the `utmpx` database searching for an entry with type `BOOT_TIME`. The timestamp from this record establishes the boot time, with uptime derived by comparing it against the current epoch time.

### Haiku: system_time()

The Haiku implementation in [`src/detection/uptime/uptime_haiku.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/uptime/uptime_haiku.c) calls `system_time()`, which returns elapsed microseconds since boot. The detector converts this value to milliseconds and applies the standard boot time calculation pattern.

## Normalizing and Formatting Results

After detection, `ffPrintUptime` processes the raw `FFUptimeResult` data for display. For standard output, the function uses `ffDurationAppendNum` to format the millisecond count into human-readable days, hours, minutes, and seconds. When users specify custom output formats, the module breaks down the millisecond value into individual temporal components for template substitution.

To display uptime using fastfetch:

```bash
fastfetch --module uptime

```

Typical output format:

```

Uptime: 5 days, 3 hours, 12 minutes, 45 seconds

```

For programmatic access to raw values:

```bash
fastfetch --json --module uptime

```

This outputs structured data including the `uptime` field (milliseconds) and ISO-8601 formatted `bootTime`.

## Summary

- **Fastfetch detects system uptime** through platform-specific implementations compiled conditionally for Linux, Windows, BSD, SunOS, and Haiku.
- **Linux systems** prioritize `/proc/uptime` with a fallback to `clock_gettime(CLOCK_BOOTTIME)` as implemented in [`src/detection/uptime/uptime_linux.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/uptime/uptime_linux.c).
- **Windows detection** leverages `SharedUserData->InterruptTime` from the kernel-shared page, converting 100-nanosecond units to milliseconds.
- **BSD platforms** use `sysctl` to query `KERN_BOOTTIME`, while **SunOS** queries the `utmpx` database for `BOOT_TIME` entries.
- **All detectors** normalize results using `ffTimeGetNow()` from [`src/common/impl/time.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/common/impl/time.c) to calculate absolute boot times from elapsed uptime.
- **Output formatting** supports both human-readable strings and raw millisecond values for scripting and JSON consumption.

## Frequently Asked Questions

### What file does fastfetch read for uptime on Linux?

Fastfetch first attempts to read `/proc/uptime` to obtain the seconds-since-boot value. If this file is inaccessible or parsing fails, it falls back to the `clock_gettime` system call with `CLOCK_BOOTTIME`, which directly queries the kernel's monotonic boot clock without filesystem dependencies.

### How does fastfetch calculate the system boot time?

Fastfetch calculates boot time by subtracting the detected uptime (elapsed milliseconds since boot) from the current epoch time in milliseconds. This calculation uses `ffTimeGetNow()` from [`src/common/impl/time.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/common/impl/time.c) to ensure consistency across all supported operating systems.

### Why does fastfetch use different detection methods on different platforms?

Each operating system exposes boot timing information through distinct kernel interfaces—Linux via procfs or clock syscalls, Windows via shared memory structures, BSD via sysctl, and SunOS via user accounting databases. Fastfetch implements platform-specific detectors to utilize the most reliable and performant native API available on each system.

### Can I extract raw uptime milliseconds from fastfetch for scripting?

Yes, use the JSON output flag to obtain raw numerical values. The command `fastfetch --json --module uptime` returns an object containing the `uptime` field as an integer representing milliseconds since boot, alongside an ISO-8601 formatted `bootTime` string for absolute temporal reference.