From 24e86fbcc7d8c4332290428959228d1df23f8872 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Sun, 21 Jun 2020 01:24:23 -0400 Subject: conditionally parsing datestamp as either rfc3339Nano or rfc3339 --- registry/fetch.go | 15 +++++++-------- registry/fetch_test.go | 11 ----------- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/registry/fetch.go b/registry/fetch.go index 9adf4ec..efa9fcf 100644 --- a/registry/fetch.go +++ b/registry/fetch.go @@ -25,7 +25,6 @@ import ( "fmt" "io/ioutil" "net/http" - "regexp" "strings" "sync" "time" @@ -185,8 +184,13 @@ func ParseUserTwtxt(twtxt []byte, nickname, urlKey string) (TimeMap, error) { return nil, fmt.Errorf("improperly formatted data in twtxt file") } - normalizedDatestamp := fixTimestamp(columns[0]) - thetime, err := time.Parse(time.RFC3339, normalizedDatestamp) + var thetime time.Time + var err error + if strings.Contains(columns[0], ".") { + thetime, err = time.Parse(time.RFC3339Nano, columns[0]) + } else { + thetime, err = time.Parse(time.RFC3339, columns[0]) + } if err != nil { erz = append(erz, []byte(fmt.Sprintf("unable to retrieve date: %v\n", err))...) } @@ -200,11 +204,6 @@ func ParseUserTwtxt(twtxt []byte, nickname, urlKey string) (TimeMap, error) { return timemap, fmt.Errorf("%v", string(erz)) } -func fixTimestamp(ts string) string { - normalizeTimestamp := regexp.MustCompile(`[\+][\d][\d][:][\d][\d]`) - return strings.TrimSpace(normalizeTimestamp.ReplaceAllString(ts, "Z")) -} - // ParseRegistryTwtxt takes output from a remote registry and outputs // the accessible user data via a slice of Users. func ParseRegistryTwtxt(twtxt []byte) ([]*User, error) { diff --git a/registry/fetch_test.go b/registry/fetch_test.go index 4eab2a4..0bf7920 100644 --- a/registry/fetch_test.go +++ b/registry/fetch_test.go @@ -273,14 +273,3 @@ var timestampCases = []struct { expected: "2020-01-14T00:19:45.092344Z", }, } - -func Test_fixTimestamp(t *testing.T) { - for _, tt := range timestampCases { - t.Run(tt.name, func(t *testing.T) { - tsout := fixTimestamp(tt.orig) - if tsout != tt.expected { - t.Errorf("Failed :: %s :: got %s expected %s", tt.name, tsout, tt.expected) - } - }) - } -} -- cgit 1.4.1-2-gfad0