From d6254ebba6b3ae93367447ed7e83a2caf65f8d87 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sat, 3 Aug 2019 18:54:04 -0700 Subject: [PATCH] Add specs for /wtf/datetime --- modules/bamboohr/widget.go | 5 +- wtf/datetime.go | 31 +++++++----- wtf/datetime_test.go | 100 +++++++++++++++++++++++++++++++++---- 3 files changed, 112 insertions(+), 24 deletions(-) diff --git a/modules/bamboohr/widget.go b/modules/bamboohr/widget.go index dac3f928..5e3d5a2c 100644 --- a/modules/bamboohr/widget.go +++ b/modules/bamboohr/widget.go @@ -2,6 +2,7 @@ package bamboohr import ( "fmt" + "time" "github.com/rivo/tview" "github.com/wtfutil/wtf/wtf" @@ -36,8 +37,8 @@ func (widget *Widget) Refresh() { todayItems := client.Away( "timeOff", - wtf.Now().Format(wtf.DateFormat), - wtf.Now().Format(wtf.DateFormat), + time.Now().Local().Format(wtf.DateFormat), + time.Now().Local().Format(wtf.DateFormat), ) widget.Redraw(widget.CommonSettings().Title, widget.contentFrom(todayItems), false) diff --git a/wtf/datetime.go b/wtf/datetime.go index b2d1f2bd..0af16412 100644 --- a/wtf/datetime.go +++ b/wtf/datetime.go @@ -5,31 +5,36 @@ import ( "time" ) -// DateFormat defines the format we expect to receive dates from BambooHR in -const DateFormat = "2006-01-02" -const TimeFormat = "15:04" +const ( + // DateFormat defines the format we expect to receive dates from BambooHR in + DateFormat = "2006-01-02" + // TimeFormat defines the format we expect to receive times from BambooHR in + TimeFormat = "15:04" +) + +// IsToday returns TRUE if the date is today, FALSE if the date is not today func IsToday(date time.Time) bool { - now := Now() + now := time.Now().Local() return (date.Year() == now.Year()) && (date.Month() == now.Month()) && (date.Day() == now.Day()) } -func Now() time.Time { - return time.Now().Local() -} - +// PrettyDate takes a programmer-style date string and converts it +// in a friendlier-to-read format func PrettyDate(dateStr string) string { - newTime, _ := time.Parse(DateFormat, dateStr) + newTime, err := time.Parse(DateFormat, dateStr) + if err != nil { + return dateStr + } + return fmt.Sprint(newTime.Format("Jan 2, 2006")) } -func Tomorrow() time.Time { - return Now().AddDate(0, 0, 1) -} - +// UnixTime takes a Unix epoch time (in seconds) and returns a +// time.Time instance func UnixTime(unix int64) time.Time { return time.Unix(unix, 0) } diff --git a/wtf/datetime_test.go b/wtf/datetime_test.go index cfa80380..e9a725d4 100644 --- a/wtf/datetime_test.go +++ b/wtf/datetime_test.go @@ -3,18 +3,100 @@ package wtf import ( "testing" "time" - - . "github.com/stretchr/testify/assert" ) -func TestIsToday(t *testing.T) { - Equal(t, true, IsToday(time.Now().Local())) - Equal(t, false, IsToday(time.Now().AddDate(0, 0, -1))) - Equal(t, false, IsToday(time.Now().AddDate(0, 0, +1))) +func Test_IsToday(t *testing.T) { + tests := []struct { + name string + date time.Time + expected bool + }{ + { + name: "when yesterday", + date: time.Now().Local().AddDate(0, 0, -1), + expected: false, + }, + { + name: "when today", + date: time.Now().Local(), + expected: true, + }, + { + name: "when tomorrow", + date: time.Now().Local().AddDate(0, 0, +1), + expected: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual := IsToday(tt.date) + + if tt.expected != actual { + t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) + } + }) + } } -/* -------------------- PrettyDate() -------------------- */ +func Test_PrettyDate(t *testing.T) { + tests := []struct { + name string + date string + expected string + }{ + { + name: "with empty date", + date: "", + expected: "", + }, + { + name: "with invalid date", + date: "10-21-1999", + expected: "10-21-1999", + }, + { + name: "with valid date", + date: "1999-10-21", + expected: "Oct 21, 1999", + }, + } -func TestPrettyDate(t *testing.T) { - Equal(t, "Oct 21, 1999", PrettyDate("1999-10-21")) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual := PrettyDate(tt.date) + + if tt.expected != actual { + t.Errorf("\nexpected: %s\n got: %s", tt.expected, actual) + } + }) + } +} +func Test_UnixTime(t *testing.T) { + tests := []struct { + name string + unixVal int64 + expected string + }{ + { + name: "with 0 time", + unixVal: 0, + expected: "1969-12-31 16:00:00 -0800 PST", + }, + { + name: "with explicit time", + unixVal: 1564883266, + expected: "2019-08-03 18:47:46 -0700 PDT", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual := UnixTime(tt.unixVal) + + if tt.expected != actual.String() { + t.Errorf("\nexpected: %s\n got: %s", tt.expected, actual) + } + }) + } }