mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Add specs for /wtf/datetime
This commit is contained in:
parent
681dd85ce6
commit
d6254ebba6
@ -2,6 +2,7 @@ package bamboohr
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
"github.com/wtfutil/wtf/wtf"
|
"github.com/wtfutil/wtf/wtf"
|
||||||
@ -36,8 +37,8 @@ func (widget *Widget) Refresh() {
|
|||||||
|
|
||||||
todayItems := client.Away(
|
todayItems := client.Away(
|
||||||
"timeOff",
|
"timeOff",
|
||||||
wtf.Now().Format(wtf.DateFormat),
|
time.Now().Local().Format(wtf.DateFormat),
|
||||||
wtf.Now().Format(wtf.DateFormat),
|
time.Now().Local().Format(wtf.DateFormat),
|
||||||
)
|
)
|
||||||
|
|
||||||
widget.Redraw(widget.CommonSettings().Title, widget.contentFrom(todayItems), false)
|
widget.Redraw(widget.CommonSettings().Title, widget.contentFrom(todayItems), false)
|
||||||
|
@ -5,31 +5,36 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DateFormat defines the format we expect to receive dates from BambooHR in
|
const (
|
||||||
const DateFormat = "2006-01-02"
|
// DateFormat defines the format we expect to receive dates from BambooHR in
|
||||||
const TimeFormat = "15:04"
|
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 {
|
func IsToday(date time.Time) bool {
|
||||||
now := Now()
|
now := time.Now().Local()
|
||||||
|
|
||||||
return (date.Year() == now.Year()) &&
|
return (date.Year() == now.Year()) &&
|
||||||
(date.Month() == now.Month()) &&
|
(date.Month() == now.Month()) &&
|
||||||
(date.Day() == now.Day())
|
(date.Day() == now.Day())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Now() time.Time {
|
// PrettyDate takes a programmer-style date string and converts it
|
||||||
return time.Now().Local()
|
// in a friendlier-to-read format
|
||||||
}
|
|
||||||
|
|
||||||
func PrettyDate(dateStr string) string {
|
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"))
|
return fmt.Sprint(newTime.Format("Jan 2, 2006"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Tomorrow() time.Time {
|
// UnixTime takes a Unix epoch time (in seconds) and returns a
|
||||||
return Now().AddDate(0, 0, 1)
|
// time.Time instance
|
||||||
}
|
|
||||||
|
|
||||||
func UnixTime(unix int64) time.Time {
|
func UnixTime(unix int64) time.Time {
|
||||||
return time.Unix(unix, 0)
|
return time.Unix(unix, 0)
|
||||||
}
|
}
|
||||||
|
@ -3,18 +3,100 @@ package wtf
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIsToday(t *testing.T) {
|
func Test_IsToday(t *testing.T) {
|
||||||
Equal(t, true, IsToday(time.Now().Local()))
|
tests := []struct {
|
||||||
Equal(t, false, IsToday(time.Now().AddDate(0, 0, -1)))
|
name string
|
||||||
Equal(t, false, IsToday(time.Now().AddDate(0, 0, +1)))
|
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) {
|
for _, tt := range tests {
|
||||||
Equal(t, "Oct 21, 1999", PrettyDate("1999-10-21"))
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user