2023-05-07 05:36:34 +03:00
|
|
|
package timez_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/timez"
|
2024-01-15 04:45:34 +03:00
|
|
|
"github.com/neilotoole/sq/testh/tu"
|
2023-05-07 05:36:34 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mar1UTC, _ = timez.ParseDateUTC("2023-03-01")
|
|
|
|
denver *time.Location
|
|
|
|
nov12denver time.Time
|
|
|
|
mar1denver time.Time
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() { //nolint:gochecknoinits
|
|
|
|
var err error
|
|
|
|
if denver, err = time.LoadLocation("America/Denver"); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
nov12denver = time.Date(2020, 11, 12, 13, 14, 15, 12345678, denver)
|
|
|
|
mar1denver = mar1UTC.In(denver)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTimestampUTC(t *testing.T) {
|
|
|
|
tm := time.Date(2021, 0o1, 0o1, 7, 7, 7, 0, time.UTC)
|
|
|
|
s := timez.TimestampUTC(tm)
|
|
|
|
t.Log(s)
|
|
|
|
require.Equal(t, "2021-01-01T07:07:07.000Z", s)
|
|
|
|
|
|
|
|
s = timez.TimestampUTC(time.Now().UTC())
|
|
|
|
t.Log(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseDateOrTimestampUTC(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
in string
|
|
|
|
want int64
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{in: "", wantErr: true},
|
|
|
|
{in: "not_a_time", wantErr: true},
|
|
|
|
{in: "2021-01-16T18:18:49.348-0700", want: 1610846329},
|
|
|
|
{in: "2021-01-16T18:26:39.216-07:00", want: 1610846799},
|
|
|
|
{in: "2021-01-16T18:26:39-07:00", want: 1610846799},
|
|
|
|
{in: "2021-01-17T01:26:39.216Z", want: 1610846799},
|
|
|
|
{in: "2021-01-17", want: 1610841600},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
tc := tc
|
2024-01-15 04:45:34 +03:00
|
|
|
t.Run(tu.Name(i, tc.in), func(t *testing.T) {
|
2023-05-07 05:36:34 +03:00
|
|
|
tm, err := timez.ParseDateOrTimestampUTC(tc.in)
|
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Logf("Got: %s", timez.TimestampUTC(tm))
|
|
|
|
ut := tm.Unix()
|
|
|
|
require.Equal(t, tc.want, ut)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestZuluTimestamp(t *testing.T) {
|
|
|
|
const (
|
|
|
|
input = `2022-12-30T09:36:31-0700`
|
|
|
|
want = `2022-12-30T16:36:31Z`
|
|
|
|
)
|
|
|
|
got := timez.TimestampToRFC3339(input)
|
|
|
|
require.Equal(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormatFunc(t *testing.T) {
|
|
|
|
layouts := timez.NamedLayouts()
|
|
|
|
// Add some custom layouts
|
|
|
|
layouts = append(layouts, "%Y/%m/%d", "%s")
|
|
|
|
|
|
|
|
for _, layout := range layouts {
|
|
|
|
fn := timez.FormatFunc(layout)
|
|
|
|
got := fn(nov12denver)
|
|
|
|
t.Logf("%16s: %s", layout, got)
|
|
|
|
}
|
|
|
|
}
|
2023-08-16 18:09:50 +03:00
|
|
|
|
|
|
|
func TestExcelLongDate(t *testing.T) {
|
|
|
|
s := mar1UTC.Format(timez.ExcelLongDate)
|
|
|
|
require.Equal(t, "Wednesday, March 1, 2023", s)
|
|
|
|
}
|