~gbmor/getwtxt

24e86fbcc7d8c4332290428959228d1df23f8872 — Ben Morrison 11 days ago 3278fe2 v0.4.14
conditionally parsing datestamp as either rfc3339Nano or rfc3339
2 files changed, 7 insertions(+), 19 deletions(-)

M registry/fetch.go
M registry/fetch_test.go
M registry/fetch.go => registry/fetch.go +7 -8
@@ 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) {

M registry/fetch_test.go => registry/fetch_test.go +0 -11
@@ 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)
			}
		})
	}
}