Skip to content

Commit

Permalink
text: NewTimeTransformer: support strfmt.DateTime without dependency (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t authored May 27, 2020
1 parent efc0c98 commit fccf2a0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
20 changes: 17 additions & 3 deletions text/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ var (
colorsNumberNegative = Colors{FgHiRed}
colorsNumberZero = Colors{}
colorsURL = Colors{Underline, FgBlue}
rfc3339Milli = "2006-01-02T15:04:05.000Z07:00"
rfc3339Micro = "2006-01-02T15:04:05.000000Z07:00"

possibleTimeLayouts = []string{
time.RFC3339,
rfc3339Milli, // strfmt.DateTime.String()'s default layout
rfc3339Micro,
time.RFC3339Nano,
}
)

// Transformer helps format the contents of an object to the user's liking.
Expand Down Expand Up @@ -138,9 +147,14 @@ func NewTimeTransformer(layout string, location *time.Location) Transformer {
rsp := fmt.Sprint(val)
if valTime, ok := val.(time.Time); ok {
rsp = formatTime(valTime)
} else if valStr, ok := val.(string); ok {
if valTime, err := time.Parse(time.RFC3339, valStr); err == nil {
rsp = formatTime(valTime)
} else {
// cycle through some supported layouts to see if the string form
// of the object matches any of these layouts
for _, possibleTimeLayout := range possibleTimeLayouts {
if valTime, err := time.Parse(possibleTimeLayout, rsp); err == nil {
rsp = formatTime(valTime)
break
}
}
}
return rsp
Expand Down
9 changes: 9 additions & 0 deletions text/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,29 @@ func TestNewTimeTransformer(t *testing.T) {
expected := "2010-11-12T12:14:15-08:00"
assert.Equal(t, expected, transformer(inStr))
assert.Equal(t, expected, transformer(inTime))
for _, possibleTimeLayout := range possibleTimeLayouts {
assert.Equal(t, expected, transformer(inTime.Format(possibleTimeLayout)), possibleTimeLayout)
}

location, err = time.LoadLocation("Asia/Singapore")
assert.Nil(t, err)
transformer = NewTimeTransformer(time.UnixDate, location)
expected = "Sat Nov 13 04:14:15 +08 2010"
assert.Equal(t, expected, transformer(inStr))
assert.Equal(t, expected, transformer(inTime))
for _, possibleTimeLayout := range possibleTimeLayouts {
assert.Equal(t, expected, transformer(inTime.Format(possibleTimeLayout)), possibleTimeLayout)
}

location, err = time.LoadLocation("Europe/London")
assert.Nil(t, err)
transformer = NewTimeTransformer(time.RFC3339, location)
expected = "2010-11-12T20:14:15Z"
assert.Equal(t, expected, transformer(inStr))
assert.Equal(t, expected, transformer(inTime))
for _, possibleTimeLayout := range possibleTimeLayouts {
assert.Equal(t, expected, transformer(inTime.Format(possibleTimeLayout)), possibleTimeLayout)
}
}

func TestNewUnixTimeTransformer(t *testing.T) {
Expand Down

0 comments on commit fccf2a0

Please sign in to comment.