fix format layouts

This commit is contained in:
Pavel Merzlyakov 2024-03-04 20:17:24 +03:00
parent 677087cf8b
commit 07ee56e8dd
2 changed files with 42 additions and 3 deletions

View File

@ -17,15 +17,15 @@ var (
DefaultLocation = MoscowLocation
// 2000-12-31
DefaultDateLayout = "2002-01-02"
DefaultDateLayout = "2006-01-02"
// 23:59:59
DefaultTimeLayout = "15:04:05"
// 23:59
DefaultTimeShortLayout = "15:04"
// Example: 2000-12-31 13:01:59
DefaultDatetimeLayout = "2002-01-02 15:04:05"
DefaultDatetimeLayout = "2006-01-02 15:04:05"
// Example: 2000-12-31__13-01-59
DefaultFilenameLayout = "2002-01-02__15-04-05"
DefaultFilenameLayout = "2006-01-02__15-04-05"
)
func Now() time.Time {

View File

@ -237,3 +237,42 @@ func TestStartOfNextWeek(t *testing.T) {
assert.EqualValues(t, c.expectedTime, actualTime, "case #%d", i+1)
}
}
func TestFormat(t *testing.T) {
cases := []struct {
time time.Time
layout string
expectedString string
}{
{
time: time.Date(2023, time.March, 13, 23, 35, 42, 101, time.UTC),
layout: timeutils.DefaultDateLayout,
expectedString: "2023-03-13",
},
{
time: time.Date(2023, time.March, 13, 23, 35, 42, 101, time.UTC),
layout: timeutils.DefaultTimeLayout,
expectedString: "23:35:42",
},
{
time: time.Date(2023, time.March, 13, 23, 35, 42, 101, time.UTC),
layout: timeutils.DefaultTimeShortLayout,
expectedString: "23:35",
},
{
time: time.Date(2023, time.March, 13, 23, 35, 42, 101, time.UTC),
layout: timeutils.DefaultDatetimeLayout,
expectedString: "2023-03-13 23:35:42",
},
{
time: time.Date(2023, time.March, 13, 23, 35, 42, 101, time.UTC),
layout: timeutils.DefaultFilenameLayout,
expectedString: "2023-03-13__23-35-42",
},
}
for i, c := range cases {
actualString := c.time.Format(c.layout)
assert.EqualValues(t, c.expectedString, actualString, "case #%d", i+1)
}
}