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 (
 | 
			
		||||
	"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)
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user