How to Truncate and Round Time in Go

Truncate rounds time down to a duration, while Round snaps it to the nearest duration in Go.

Use time.Truncate to round down to a specific duration and time.Round to round to the nearest duration.

import "time"

// Truncate to the nearest minute (rounds down)
truncated := time.Now().Truncate(time.Minute)

// Round to the nearest hour (rounds to nearest)
rounded := time.Now().Round(time.Hour)

In archive/tar, ModTime is automatically rounded to the nearest second by Writer.WriteHeader unless Format is set to FormatPAX or FormatGNU to preserve sub-second resolution.