Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions presets/sysage-preset.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json",
// This preset demonstrates using sysage to show the system install date.
// Install sysage first:
// Shell version: cp scripts/sysage.sh ~/.local/bin/sysage && chmod +x ~/.local/bin/sysage
// Go version: cd scripts/sysage-go && go build -o ~/.local/bin/sysage .
"modules": [
"title",
"separator",
"os",
"host",
"kernel",
"uptime",
{
"type": "command",
"key": "System Age",
"text": "sysage"
},
"packages",
"shell",
"terminal",
"cpu",
"gpu",
"memory",
"disk",
"break",
"colors"
]
}
3 changes: 3 additions & 0 deletions scripts/sysage-go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/davcyril/sysage

go 1.24.7
165 changes: 165 additions & 0 deletions scripts/sysage-go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package main

import (
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
)

// Sanity threshold: Linux 0.01 release (1991-09-17).
// Timestamps older than this are bogus (epoch defaults, broken clocks).
const linuxEpoch int64 = 685065600

// getBirthTime gets the birth time of a single file via stat(1).
// Returns 0 if birth time is unavailable, unsupported, or the file doesn't exist.
func getBirthTime(path string) int64 {
var args []string
switch runtime.GOOS {
case "linux":
// GNU stat: -c %W = birth time as seconds since epoch (0 if unknown)
args = []string{"-c", "%W", path}
case "darwin", "freebsd", "dragonfly":
// BSD stat: -f %B = birth time as seconds since epoch
args = []string{"-f", "%B", path}
default:
return 0
}

out, err := exec.Command("stat", args...).Output()
if err != nil {
return 0
}
sec, err := strconv.ParseInt(strings.TrimSpace(string(out)), 10, 64)
if err != nil || sec <= 0 {
return 0
}
return sec
}

// getMtime returns the mtime of a file via os.Stat (platform-independent).
func getMtime(path string) int64 {
info, err := os.Stat(path)
if err != nil {
return 0
}
return info.ModTime().Unix()
}

// findOldest probes all candidates and returns the oldest valid timestamp.
// Uses a single stat(1) call for birth times, then individual syscall.Stat for mtime.
func findOldest(paths []string) int64 {
now := time.Now().Unix()
var oldest int64

update := func(ts int64) {
if ts > linuxEpoch && ts <= now && (oldest == 0 || ts < oldest) {
oldest = ts
}
}

// Birth-time lookups via stat(1)
for _, p := range paths {
update(getBirthTime(p))
}

// Individual mtime lookups (pure syscall, no subprocess)
for _, p := range paths {
update(getMtime(p))
}

return oldest
}

// candidates returns OS-specific paths likely created at install time.
func candidates() []string {
switch runtime.GOOS {
case "linux":
return []string{
"/etc/machine-id", // systemd — created at install
"/etc/hostname", // non-systemd fallback
"/etc/localtime", // almost universal
"/var/log/installer", // Debian/Ubuntu installer log dir
"/var/log/installer/syslog", // Debian/Ubuntu installer syslog
"/var/log/anaconda", // Fedora/RHEL installer log dir
"/", // root filesystem mount
}
case "darwin":
return []string{
"/var/db/.AppleSetupDone",
"/var/db/receipts",
"/private/var/db/.AppleSetupDone",
"/System/Library/CoreServices/SystemVersion.plist",
}
case "freebsd", "dragonfly":
return []string{
"/entropy",
"/etc/hostid",
"/etc/rc.conf",
}
default:
return nil
}
}

// formatAge formats a duration into human-readable "X years, Y months, Z days" style.
func formatAge(d time.Duration, installDate time.Time) string {
days := int(d.Hours() / 24)
years := days / 365
rem := days % 365
months := rem / 30
daysLeft := rem % 30

dateFmt := installDate.Format("2006-01-02")

plural := func(n int, word string) string {
if n == 1 {
return fmt.Sprintf("1 %s", word)
}
return fmt.Sprintf("%d %ss", n, word)
}

if years > 0 {
if months > 0 {
return fmt.Sprintf("%s, %s, %s (since %s)",
plural(years, "year"), plural(months, "month"),
plural(daysLeft, "day"), dateFmt)
}
return fmt.Sprintf("%s, %s (since %s)",
plural(years, "year"), plural(daysLeft, "day"), dateFmt)
}
if months > 0 {
return fmt.Sprintf("%s, %s (since %s)",
plural(months, "month"), plural(daysLeft, "day"), dateFmt)
}
return fmt.Sprintf("%s (since %s)", plural(days, "day"), dateFmt)
}

func main() {
paths := candidates()
if paths == nil {
fmt.Fprintf(os.Stderr, "Unsupported platform: %s\n", runtime.GOOS)
os.Exit(1)
}

// Probe all candidates — oldest wins
oldest := findOldest(paths)

if oldest == 0 {
fmt.Println("Unknown")
os.Exit(1)
}

now := time.Now()
installTime := time.Unix(oldest, 0)

if installTime.After(now) {
fmt.Println("Unknown")
os.Exit(1)
}

fmt.Println(formatAge(now.Sub(installTime), installTime))
}
136 changes: 136 additions & 0 deletions scripts/sysage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/bin/sh
# sysage - Detect system install date and display age
# For use with fastfetch's Command module:
# { "type": "command", "key": "System Age", "text": "sysage" }
#
# Supports Linux, macOS, FreeBSD, DragonFly BSD.
# SPDX-License-Identifier: MIT

LINUX_EPOCH=685065600 # 1991-09-17, birth of Linux

# get_birth_time <path>
# Prints inode birth time (seconds since epoch) or nothing.
get_birth_time() {
case "$(uname -s)" in
Linux) ts=$(stat -c '%W' "$1" 2>/dev/null) ;;
Darwin) ts=$(stat -f '%B' "$1" 2>/dev/null) ;;
FreeBSD|DragonFly) ts=$(stat -f '%B' "$1" 2>/dev/null) ;;
*) return ;;
esac
case "$ts" in
''|0|-1) return ;;
esac
echo "$ts"
}

# get_mtime <path>
# Prints file modification time (seconds since epoch) or nothing.
get_mtime() {
case "$(uname -s)" in
Linux) ts=$(stat -L -c '%Y' "$1" 2>/dev/null) ;;
*) ts=$(stat -L -f '%m' "$1" 2>/dev/null) ;;
esac
[ -n "$ts" ] && [ "$ts" -gt 0 ] 2>/dev/null && echo "$ts"
}

# update_oldest <ts>
# Updates $oldest if ts is valid and older.
update_oldest() {
_ts=$1
[ -z "$_ts" ] && return
[ "$_ts" -gt "$LINUX_EPOCH" ] 2>/dev/null || return
[ "$_ts" -le "$_now" ] 2>/dev/null || return
if [ -z "$oldest" ] || [ "$_ts" -lt "$oldest" ]; then
oldest=$_ts
fi
}

# format_age <seconds_elapsed> <install_epoch>
format_age() {
_elapsed=$1
_install_epoch=$2

_days=$((_elapsed / 86400))
_years=$((_days / 365))
_rem=$((_days % 365))
_months=$((_rem / 30))
_days_left=$((_rem % 30))

# Format install date as YYYY-MM-DD
if date -d "@$_install_epoch" +%Y-%m-%d >/dev/null 2>&1; then
_date_fmt=$(date -d "@$_install_epoch" +%Y-%m-%d)
else
_date_fmt=$(date -r "$_install_epoch" +%Y-%m-%d 2>/dev/null || echo "unknown")
fi

_plural() {
if [ "$1" -eq 1 ]; then
printf '1 %s' "$2"
else
printf '%d %ss' "$1" "$2"
fi
}

if [ "$_years" -gt 0 ]; then
if [ "$_months" -gt 0 ]; then
printf '%s, %s, %s (since %s)\n' \
"$(_plural "$_years" year)" "$(_plural "$_months" month)" \
"$(_plural "$_days_left" day)" "$_date_fmt"
else
printf '%s, %s (since %s)\n' \
"$(_plural "$_years" year)" "$(_plural "$_days_left" day)" "$_date_fmt"
fi
elif [ "$_months" -gt 0 ]; then
printf '%s, %s (since %s)\n' \
"$(_plural "$_months" month)" "$(_plural "$_days_left" day)" "$_date_fmt"
else
printf '%s (since %s)\n' "$(_plural "$_days" day)" "$_date_fmt"
fi
}

# Main
_now=$(date +%s)
oldest=""

# Probe candidates — inline, no subshell
case "$(uname -s)" in
Linux)
for _p in /etc/machine-id /etc/hostname /etc/localtime \
/var/log/installer /var/log/installer/syslog \
/var/log/anaconda /; do
update_oldest "$(get_birth_time "$_p")"
update_oldest "$(get_mtime "$_p")"
done
;;
Darwin)
for _p in /var/db/.AppleSetupDone /var/db/receipts \
/private/var/db/.AppleSetupDone \
/System/Library/CoreServices/SystemVersion.plist; do
update_oldest "$(get_birth_time "$_p")"
update_oldest "$(get_mtime "$_p")"
done
;;
FreeBSD|DragonFly)
for _p in /entropy /etc/hostid /etc/rc.conf; do
update_oldest "$(get_birth_time "$_p")"
update_oldest "$(get_mtime "$_p")"
done
;;
*)
printf 'Unsupported platform: %s\n' "$(uname -s)" >&2
exit 1
;;
esac

if [ -z "$oldest" ]; then
echo "Unknown"
exit 1
fi

if [ "$oldest" -gt "$_now" ]; then
echo "Unknown"
exit 1
fi

_elapsed=$((_now - oldest))
format_age "$_elapsed" "$oldest"