From 6416febc89d94ae02642c6ed68ea2ccf54c1cda4 Mon Sep 17 00:00:00 2001 From: SunRunAway Date: Mon, 8 Jul 2013 13:21:43 +0800 Subject: [PATCH] add Time for Entry --- ftp.go | 14 ++++++++++++++ parse_test.go | 30 ++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/ftp.go b/ftp.go index 6e8b0f8..c24306c 100644 --- a/ftp.go +++ b/ftp.go @@ -10,6 +10,7 @@ import ( "net/textproto" "strconv" "strings" + "time" ) // EntryType describes the different types of an Entry. @@ -33,6 +34,7 @@ type Entry struct { Name string Type EntryType Size uint64 + Time time.Time } // response represent a data-connection @@ -237,6 +239,18 @@ func parseListLine(line string) (*Entry, error) { } e.Size = size } + var timeStr string + if strings.Contains(fields[7], ":") { // this year + thisYear, _, _ := time.Now().Date() + timeStr = fields[6] + " " + fields[5] + " " + strconv.Itoa(thisYear)[2:4] + " " + fields[7] + " GMT" + } else { // not this year + timeStr = fields[6] + " " + fields[5] + " " + fields[7][2:4] + " " + "00:00" + " GMT" + } + t, err := time.Parse("_2 Jan 06 15:04 MST", timeStr) + if err != nil { + return nil, err + } + e.Time = t e.Name = strings.Join(fields[8:], " ") return e, nil diff --git a/parse_test.go b/parse_test.go index b9a16c7..c853e59 100644 --- a/parse_test.go +++ b/parse_test.go @@ -1,31 +1,38 @@ package ftp -import "testing" +import ( + "time" + "testing" +) + + +var thisYear, _, _ = time.Now().Date() type line struct { line string name string size uint64 entryType EntryType + time time.Time } var listTests = []line{ // UNIX ls -l style - line{"drwxr-xr-x 3 110 1002 3 Dec 02 2009 pub", "pub", 0, EntryTypeFolder}, - line{"drwxr-xr-x 3 110 1002 3 Dec 02 2009 p u b", "p u b", 0, EntryTypeFolder}, - line{"-rwxr-xr-x 3 110 1002 1234567 Dec 02 2009 fileName", "fileName", 1234567, EntryTypeFile}, - line{"lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin", "bin -> usr/bin", 0, EntryTypeLink}, + line{"drwxr-xr-x 3 110 1002 3 Dec 02 2009 pub", "pub", 0, EntryTypeFolder, time.Date(2009, time.December, 2, 0, 0, 0, 0, time.UTC)}, + line{"drwxr-xr-x 3 110 1002 3 Dec 02 2009 p u b", "p u b", 0, EntryTypeFolder, time.Date(2009, time.December, 2, 0, 0, 0, 0, time.UTC)}, + line{"-rwxr-xr-x 3 110 1002 1234567 Dec 02 2009 fileName", "fileName", 1234567, EntryTypeFile, time.Date(2009, time.December, 2, 0, 0, 0, 0, time.UTC)}, + line{"lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin", "bin -> usr/bin", 0, EntryTypeLink, time.Date(thisYear, time.January, 25, 0, 17, 0, 0, time.UTC)}, // Microsoft's FTP servers for Windows - line{"---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z", "ls-lR.Z", 1803128, EntryTypeFile}, - line{"d--------- 1 owner group 0 May 9 19:45 Softlib", "Softlib", 0, EntryTypeFolder}, + line{"---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z", "ls-lR.Z", 1803128, EntryTypeFile, time.Date(thisYear, time.July, 10, 10, 18, 0, 0, time.UTC)}, + line{"d--------- 1 owner group 0 May 9 19:45 Softlib", "Softlib", 0, EntryTypeFolder, time.Date(thisYear, time.May, 9, 19, 45, 0, 0, time.UTC)}, // WFTPD for MSDOS - line{"-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp", "message.ftp", 322, EntryTypeFile}, + line{"-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp", "message.ftp", 322, EntryTypeFile, time.Date(1996, time.August, 19, 0, 0, 0, 0, time.UTC)}, } // Not supported, at least we should properly return failure var listTestsFail = []line{ - line{"d [R----F--] supervisor 512 Jan 16 18:53 login", "login", 0, EntryTypeFolder}, - line{"- [R----F--] rhesus 214059 Oct 20 15:27 cx.exe", "cx.exe", 0, EntryTypeFile}, + line{"d [R----F--] supervisor 512 Jan 16 18:53 login", "login", 0, EntryTypeFolder, time.Date(thisYear, time.January, 16, 18, 53, 0, 0, time.UTC)}, + line{"- [R----F--] rhesus 214059 Oct 20 15:27 cx.exe", "cx.exe", 0, EntryTypeFile, time.Date(thisYear, time.October, 20, 15, 27, 0, 0, time.UTC)}, } func TestParseListLine(t *testing.T) { @@ -44,6 +51,9 @@ func TestParseListLine(t *testing.T) { if entry.Size != lt.size { t.Errorf("parseListLine(%v).Size = %v, want %v", lt.line, entry.Size, lt.size) } + if entry.Time.Unix() != lt.time.Unix() { + t.Errorf("parseListLine(%v).Time = %v, want %v", lt.line, entry.Time, lt.time) + } } for _, lt := range listTestsFail { _, err := parseListLine(lt.line)