import (
"bytes"
+ "fmt"
"strconv"
"strings"
"time"
return time.Unix(secs, int64(nsecs)), nil
}
-// TODO(dsnet): Implement formatPAXTime.
+// formatPAXTime converts ts into a time of the form %d.%d as described in the
+// PAX specification. This function is capable of negative timestamps.
+func formatPAXTime(ts time.Time) (s string) {
+ secs, nsecs := ts.Unix(), ts.Nanosecond()
+ if nsecs == 0 {
+ return strconv.FormatInt(secs, 10)
+ }
+
+ // If seconds is negative, then perform correction.
+ sign := ""
+ if secs < 0 {
+ sign = "-" // Remember sign
+ secs = -(secs + 1) // Add a second to secs
+ nsecs = -(nsecs - 1E9) // Take that second away from nsecs
+ }
+ return strings.TrimRight(fmt.Sprintf("%s%d.%09d", sign, secs, nsecs), "0")
+}
// parsePAXRecord parses the input PAX record string into a key-value pair.
// If parsing is successful, it will slice off the currently read record and
}
}
+func TestFormatPAXTime(t *testing.T) {
+ vectors := []struct {
+ sec, nsec int64
+ want string
+ }{
+ {1350244992, 0, "1350244992"},
+ {1350244992, 300000000, "1350244992.3"},
+ {1350244992, 23960100, "1350244992.0239601"},
+ {1350244992, 23960108, "1350244992.023960108"},
+ {+1, +1E9 - 1E0, "1.999999999"},
+ {+1, +1E9 - 1E3, "1.999999"},
+ {+1, +1E9 - 1E6, "1.999"},
+ {+1, +0E0 - 0E0, "1"},
+ {+1, +1E6 - 0E0, "1.001"},
+ {+1, +1E3 - 0E0, "1.000001"},
+ {+1, +1E0 - 0E0, "1.000000001"},
+ {0, 1E9 - 1E0, "0.999999999"},
+ {0, 1E9 - 1E3, "0.999999"},
+ {0, 1E9 - 1E6, "0.999"},
+ {0, 0E0, "0"},
+ {0, 1E6 + 0E0, "0.001"},
+ {0, 1E3 + 0E0, "0.000001"},
+ {0, 1E0 + 0E0, "0.000000001"},
+ {-1, -1E9 + 1E0, "-1.999999999"},
+ {-1, -1E9 + 1E3, "-1.999999"},
+ {-1, -1E9 + 1E6, "-1.999"},
+ {-1, -0E0 + 0E0, "-1"},
+ {-1, -1E6 + 0E0, "-1.001"},
+ {-1, -1E3 + 0E0, "-1.000001"},
+ {-1, -1E0 + 0E0, "-1.000000001"},
+ {-1350244992, 0, "-1350244992"},
+ {-1350244992, -300000000, "-1350244992.3"},
+ {-1350244992, -23960100, "-1350244992.0239601"},
+ {-1350244992, -23960108, "-1350244992.023960108"},
+ }
+
+ for _, v := range vectors {
+ got := formatPAXTime(time.Unix(v.sec, v.nsec))
+ if got != v.want {
+ t.Errorf("formatPAXTime(%ds, %dns): got %q, want %q",
+ v.sec, v.nsec, got, v.want)
+ }
+ }
+}
+
func TestParsePAXRecord(t *testing.T) {
medName := strings.Repeat("CD", 50)
longName := strings.Repeat("AB", 100)
return err
}
- // TODO(dsnet): Add PAX timestamps with nanosecond support.
- hdrCpy := *hdr
- hdrCpy.ModTime = hdrCpy.ModTime.Truncate(time.Second)
-
- switch allowedFormats, paxHdrs := hdrCpy.allowedFormats(); {
+ switch allowedFormats, paxHdrs := hdr.allowedFormats(); {
case allowedFormats&formatUSTAR != 0:
- return tw.writeUSTARHeader(&hdrCpy)
+ return tw.writeUSTARHeader(hdr)
case allowedFormats&formatPAX != 0:
- return tw.writePAXHeader(&hdrCpy, paxHdrs)
+ return tw.writePAXHeader(hdr, paxHdrs)
case allowedFormats&formatGNU != 0:
- return tw.writeGNUHeader(&hdrCpy)
+ return tw.writeGNUHeader(hdr)
default:
return ErrHeader
}