diff options
author | Martin Polden <mpolden@mpolden.no> | 2018-06-17 12:39:37 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2018-06-17 12:39:37 +0200 |
commit | 2854a63758779bdc647fc6e5b286af20edb9fe8a (patch) | |
tree | 1e687ecc427b58998a4ae14e6b894de7643eb52f | |
parent | f1282ea78bd28f8ab4f15a8633653acb4a58c146 (diff) |
Simplify time parsing
-rw-r--r-- | lftp/client.go | 2 | ||||
-rw-r--r-- | lftp/client_test.go | 32 | ||||
-rw-r--r-- | lftp/file.go | 15 | ||||
-rw-r--r-- | lftp/file_test.go | 12 |
4 files changed, 31 insertions, 30 deletions
diff --git a/lftp/client.go b/lftp/client.go index 24190ab..cb3f3ce 100644 --- a/lftp/client.go +++ b/lftp/client.go @@ -67,6 +67,6 @@ func parseDirList(r io.Reader) ([]os.FileInfo, error) { } func listArgs(name, path string) []string { - script := "cls -1 --classify --date --time-style='%F %T %z %Z' " + path + " && exit" + script := "cls -1 --classify --date --time-style='%s' " + path + " && exit" return []string{"-e", script, name} } diff --git a/lftp/client_test.go b/lftp/client_test.go index 919238c..249f7ca 100644 --- a/lftp/client_test.go +++ b/lftp/client_test.go @@ -8,30 +8,30 @@ import ( ) func TestParseDirList(t *testing.T) { - ls := `2014-06-25 14:15:16 +0200 CEST dir1/ - 2015-02-02 23:01:15 +0100 CET dir2/ - 2015-03-15 08:28:30 +0100 CET dir3@` - expected := []file{ - file{modTime: time.Date(2014, 6, 25, 14, 15, 16, 0, time.FixedZone("CEST", 7200)), path: "dir1"}, - file{modTime: time.Date(2015, 2, 2, 23, 1, 15, 0, time.FixedZone("CET", 3600)), path: "dir2"}, - file{modTime: time.Date(2015, 3, 15, 8, 28, 30, 0, time.FixedZone("CET", 3600)), path: "dir3"}, + ls := `1403705716 dir1/ + 1422918075 dir2/ + 1426408110 dir3@` + want := []file{ + file{modTime: time.Date(2014, 6, 25, 14, 15, 16, 0, time.UTC), path: "dir1"}, + file{modTime: time.Date(2015, 2, 2, 23, 1, 15, 0, time.UTC), path: "dir2"}, + file{modTime: time.Date(2015, 3, 15, 8, 28, 30, 0, time.UTC), path: "dir3"}, } - actual, err := parseDirList(strings.NewReader(ls)) + got, err := parseDirList(strings.NewReader(ls)) if err != nil { t.Fatal(err) } - for i, e := range expected { - a := actual[i] - if !e.ModTime().Equal(a.ModTime()) || e.Name() != a.Name() { - t.Fatalf("Expected %+v, got %+v", e, a) + for i, w := range want { + g := got[i] + if !w.ModTime().Equal(g.ModTime()) || w.Name() != g.Name() { + t.Fatalf("want %+v, got %+v", w, g) } } } func TestListArgs(t *testing.T) { - expected := []string{"-e", "cls -1 --classify --date --time-style='%F %T %z %Z' /foo && exit", "bar"} - args := listArgs("bar", "/foo") - if !reflect.DeepEqual(expected, args) { - t.Fatalf("Expected %q, got %q", expected, args) + want := []string{"-e", "cls -1 --classify --date --time-style='%s' /foo && exit", "bar"} + got := listArgs("bar", "/foo") + if !reflect.DeepEqual(want, got) { + t.Fatalf("want %q, got %s", want, got) } } diff --git a/lftp/file.go b/lftp/file.go index 791d617..8bdcf39 100644 --- a/lftp/file.go +++ b/lftp/file.go @@ -3,6 +3,7 @@ package lftp import ( "fmt" "os" + "strconv" "strings" "time" ) @@ -21,16 +22,16 @@ func (f file) IsDir() bool { return f.Mode().IsDir() } func (f file) Sys() interface{} { return nil } func ParseFile(s string) (file, error) { - parts := strings.SplitN(s, " ", 5) - if len(parts) != 5 { - return file{}, fmt.Errorf("failed to parse file: %s", s) + parts := strings.SplitN(s, " ", 2) + if len(parts) != 2 { + return file{}, fmt.Errorf("invalid file: %q", s) } - t := strings.Join(parts[:4], " ") - modified, err := time.Parse("2006-01-02 15:04:05 -0700 MST", t) + secs, err := strconv.ParseInt(parts[0], 10, 64) if err != nil { - return file{}, err + return file{}, fmt.Errorf("invalid time: %q: %s", parts[0], err) } - path := parts[4] + modified := time.Unix(secs, 0) + path := parts[1] var fileMode os.FileMode if strings.HasSuffix(path, "@") { diff --git a/lftp/file_test.go b/lftp/file_test.go index d3727ab..04a4e4d 100644 --- a/lftp/file_test.go +++ b/lftp/file_test.go @@ -7,8 +7,8 @@ import ( ) func TestParseFile(t *testing.T) { - t1 := time.Date(2014, 12, 16, 0, 4, 30, 0, time.FixedZone("CET", 3600)) - t2 := time.Date(2015, 2, 3, 15, 12, 30, 0, time.FixedZone("CET", 3600)) + t1 := time.Date(2014, 12, 16, 0, 4, 30, 0, time.UTC) + t2 := time.Date(2015, 2, 3, 15, 12, 30, 0, time.UTC) var tests = []struct { in string out file @@ -16,13 +16,13 @@ func TestParseFile(t *testing.T) { IsDir bool IsRegular bool }{ - {"2014-12-16 00:04:30 +0100 CET /bar/foo/", file{modTime: t1, path: "/bar/foo"}, + {"1418688270 /bar/foo/", file{modTime: t1, path: "/bar/foo"}, false /* IsDir */, true, false}, - {"2015-02-03 15:12:30 +0100 CET /foo/bar@", + {"1422976350 /foo/bar@", file{modTime: t2, path: "/foo/bar"} /* IsSymlink */, true, false, false}, - {"2014-12-16 00:04:30 +0100 CET /foo/bar baz/", + {"1418688270 /foo/bar baz/", file{modTime: t1, path: "/foo/bar baz"}, false /* IsDir */, true, false}, - {"2014-12-16 00:04:30 +0100 CET /foo/baz", + {"1418688270 /foo/baz", file{modTime: t1, path: "/foo/baz"}, false, false /* IsRegular */, true}, } for _, tt := range tests { |