]> Cypherpunks repositories - gostls13.git/commitdiff
archive/zip: add SetModTime method to FileHeader
authorAndrew Gerrand <adg@golang.org>
Mon, 19 Dec 2011 03:59:41 +0000 (14:59 +1100)
committerAndrew Gerrand <adg@golang.org>
Mon, 19 Dec 2011 03:59:41 +0000 (14:59 +1100)
Fixes #2574.

R=golang-dev, bradfitz, adg, bradfitz
CC=golang-dev
https://golang.org/cl/5494072

src/pkg/archive/zip/struct.go
src/pkg/archive/zip/zip_test.go

index c53a83c4e788cd01d5799dd92cd3e24ab4643908..34a87fae5b330596cf05d7221b939a7c8078fee6 100644 (file)
@@ -96,12 +96,28 @@ func msDosTimeToTime(dosDate, dosTime uint16) time.Time {
        )
 }
 
+// timeToMsDosTime converts a time.Time to an MS-DOS date and time.
+// The resolution is 2s.
+// See: http://msdn.microsoft.com/en-us/library/ms724274(v=VS.85).aspx
+func timeToMsDosTime(t time.Time) (fDate uint16, fTime uint16) {
+       t = t.In(time.UTC)
+       fDate = uint16(t.Day() + int(t.Month())<<5 + (t.Year()-1980)<<9)
+       fTime = uint16(t.Second()/2 + t.Minute()<<5 + t.Hour()<<11)
+       return
+}
+
 // ModTime returns the modification time.
 // The resolution is 2s.
 func (h *FileHeader) ModTime() time.Time {
        return msDosTimeToTime(h.ModifiedDate, h.ModifiedTime)
 }
 
+// SetModTime sets the ModifiedTime and ModifiedDate fields to the given time.
+// The resolution is 2s.
+func (h *FileHeader) SetModTime(t time.Time) {
+       h.ModifiedDate, h.ModifiedTime = timeToMsDosTime(t)
+}
+
 // traditional names for Unix constants
 const (
        s_IFMT  = 0xf000
index 2075715f3e01de96b42f30f99109729798236013..8aab2b68123f704c4e1a255f99a3daf8f49d59b2 100644 (file)
@@ -11,6 +11,7 @@ import (
        "fmt"
        "io"
        "testing"
+       "time"
 )
 
 type stringReaderAt string
@@ -55,3 +56,13 @@ func TestOver65kFiles(t *testing.T) {
                }
        }
 }
+
+func TestModTime(t *testing.T) {
+       var testTime = time.Date(2009, time.November, 10, 23, 45, 58, 0, time.UTC)
+       fh := new(FileHeader)
+       fh.SetModTime(testTime)
+       outTime := fh.ModTime()
+       if !outTime.Equal(testTime) {
+               t.Errorf("times don't match: got %s, want %s", outTime, testTime)
+       }
+}