How to Handle JSON Dates and Times in Go

Handle JSON dates in Go tar archives by using Header time fields and setting Format to PAX or GNU for precision.

Use the time.Time fields in archive/tar.Header and specify FormatPAX to preserve sub-second precision or FormatGNU for full compatibility. The default FormatUnknown rounds ModTime to the nearest second and ignores AccessTime and ChangeTime.

hdr := &tar.Header{
    Name:     "file.txt",
    Size:     1024,
    ModTime:  time.Now(),
    AccessTime: time.Now(),
    ChangeTime: time.Now(),
    Format:   tar.FormatPAX,
}
err := tw.WriteHeader(hdr)