Skip to content
Merged
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
22 changes: 20 additions & 2 deletions scripts/generate-man/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
package main

import (
"fmt"
"log"
"os"
"strconv"
"time"

"github.com/spf13/cobra/doc"
Expand All @@ -23,11 +25,14 @@ func main() {
log.Fatal(err)
}

now := time.Now()
date, err := buildDate()
if err != nil {
log.Fatal(err)
}
header := &doc.GenManHeader{
Title: "PIN",
Section: "1",
Date: &now,
Date: &date,
Source: "pin",
Manual: "Pin Manual",
}
Expand All @@ -36,3 +41,16 @@ func main() {
log.Fatal(err)
}
}

// UTC so the header date does not depend on the build machine's timezone.
func buildDate() (time.Time, error) {
epoch := os.Getenv("SOURCE_DATE_EPOCH")
if epoch == "" {
return time.Now().UTC(), nil
}
seconds, err := strconv.ParseInt(epoch, 10, 64)
if err != nil {
return time.Time{}, fmt.Errorf("invalid SOURCE_DATE_EPOCH: %w", err)
}
return time.Unix(seconds, 0).UTC(), nil
}
40 changes: 40 additions & 0 deletions scripts/generate-man/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"testing"
"time"
)

func TestBuildDateUsesSourceDateEpoch(t *testing.T) {
t.Setenv("SOURCE_DATE_EPOCH", "1764547200")
t.Setenv("TZ", "America/Los_Angeles")

got, err := buildDate()
if err != nil {
t.Fatal(err)
}
want := time.Date(2025, time.December, 1, 0, 0, 0, 0, time.UTC)
if !got.Equal(want) || got.Location() != time.UTC {
t.Errorf("buildDate() = %v, want %v", got, want)
}
}

func TestBuildDateRejectsInvalidEpoch(t *testing.T) {
t.Setenv("SOURCE_DATE_EPOCH", "yesterday")

if _, err := buildDate(); err == nil {
t.Error("buildDate() accepted a non-numeric SOURCE_DATE_EPOCH")
}
}

func TestBuildDateDefaultsToNow(t *testing.T) {
t.Setenv("SOURCE_DATE_EPOCH", "")

got, err := buildDate()
if err != nil {
t.Fatal(err)
}
if time.Since(got) > time.Minute || got.Location() != time.UTC {
t.Errorf("buildDate() = %v, want the current time in UTC", got)
}
}
Loading