initialize

This commit is contained in:
Pavel Merzlyakov 2023-05-10 21:17:42 +03:00
commit 677087cf8b
4 changed files with 338 additions and 0 deletions

11
go.mod Normal file
View File

@ -0,0 +1,11 @@
module git.bit5.ru/gomodules/timeutils
go 1.20
require github.com/stretchr/testify v1.8.2
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

17
go.sum Normal file
View File

@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

71
timeutils.go Normal file
View File

@ -0,0 +1,71 @@
package timeutils
import "time"
const (
Second int64 = 1
Minute = 60 * Second
Hour = 60 * Minute
Day = 24 * Hour
Week = 7 * Day
)
var (
// Europe/Moscow (UTC+03:00) time zone
MoscowLocation = time.FixedZone("Europe/Moscow", int(Hour)*3)
// Europe/Moscow (UTC+03:00) time zone
DefaultLocation = MoscowLocation
// 2000-12-31
DefaultDateLayout = "2002-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"
// Example: 2000-12-31__13-01-59
DefaultFilenameLayout = "2002-01-02__15-04-05"
)
func Now() time.Time {
return time.Now().In(DefaultLocation)
}
func StartOfDay(now time.Time) time.Time {
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
}
func StartOfPreviousDay(now time.Time) time.Time {
return StartOfDay(now).AddDate(0, 0, -1)
}
func StartOfNextDay(now time.Time) time.Time {
return StartOfDay(now).AddDate(0, 0, 1)
}
func StartOfWeek(now time.Time) time.Time {
offset := -convertWeekday(now.Weekday())
return StartOfDay(now).AddDate(0, 0, offset)
}
func StartOfPreviousWeek(now time.Time) time.Time {
offset := -7 - convertWeekday(now.Weekday())
return StartOfDay(now).AddDate(0, 0, offset)
}
func StartOfNextWeek(now time.Time) time.Time {
offset := 7 - convertWeekday(now.Weekday())
return StartOfDay(now).AddDate(0, 0, offset)
}
// Monday - 0
// Tuesday - 1
// Wednesday - 2
// Thursday - 3
// Friday - 4
// Saturday - 5
// Sunday - 6
func convertWeekday(weekday time.Weekday) int {
return (int(weekday) + 6) % 7
}

239
timeutils_test.go Normal file
View File

@ -0,0 +1,239 @@
package timeutils_test
import (
"testing"
"time"
"git.bit5.ru/gomodules/timeutils"
"github.com/stretchr/testify/assert"
)
func TestStartOfDay(t *testing.T) {
cases := []struct {
now time.Time
expectedTime time.Time
}{
{
now: time.Date(2023, time.March, 14, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 14, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 14, 23, 35, 42, 101, timeutils.DefaultLocation),
expectedTime: time.Date(2023, time.March, 14, 0, 0, 0, 0, timeutils.DefaultLocation),
},
{
now: time.Date(2023, time.March, 14, 0, 0, 0, 0, time.UTC),
expectedTime: time.Date(2023, time.March, 14, 0, 0, 0, 0, time.UTC),
},
}
for i, c := range cases {
actualTime := timeutils.StartOfDay(c.now)
assert.EqualValues(t, c.expectedTime, actualTime, "case #%d", i+1)
}
}
func TestStartOfPreviousDay(t *testing.T) {
cases := []struct {
now time.Time
expectedTime time.Time
}{
{
now: time.Date(2023, time.March, 14, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 14, 23, 35, 42, 101, timeutils.DefaultLocation),
expectedTime: time.Date(2023, time.March, 13, 0, 0, 0, 0, timeutils.DefaultLocation),
},
{
now: time.Date(2023, time.March, 14, 0, 0, 0, 0, time.UTC),
expectedTime: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
},
}
for i, c := range cases {
actualTime := timeutils.StartOfPreviousDay(c.now)
assert.EqualValues(t, c.expectedTime, actualTime, "case #%d", i+1)
}
}
func TestStartOfNextDay(t *testing.T) {
cases := []struct {
now time.Time
expectedTime time.Time
}{
{
now: time.Date(2023, time.March, 14, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 15, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 14, 23, 35, 42, 101, timeutils.DefaultLocation),
expectedTime: time.Date(2023, time.March, 15, 0, 0, 0, 0, timeutils.DefaultLocation),
},
{
now: time.Date(2023, time.March, 14, 0, 0, 0, 0, time.UTC),
expectedTime: time.Date(2023, time.March, 15, 0, 0, 0, 0, time.UTC),
},
}
for i, c := range cases {
actualTime := timeutils.StartOfNextDay(c.now)
assert.EqualValues(t, c.expectedTime, actualTime, "case #%d", i+1)
}
}
func TestStartOfWeek(t *testing.T) {
cases := []struct {
now time.Time
expectedTime time.Time
}{
{
now: time.Date(2023, time.March, 13, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 14, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 15, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 16, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 17, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 18, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 19, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 14, 23, 35, 42, 101, timeutils.DefaultLocation),
expectedTime: time.Date(2023, time.March, 13, 0, 0, 0, 0, timeutils.DefaultLocation),
},
{
now: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
expectedTime: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
},
}
for i, c := range cases {
actualTime := timeutils.StartOfWeek(c.now)
assert.EqualValues(t, c.expectedTime, actualTime, "case #%d", i+1)
}
}
func TestStartOfPreviousWeek(t *testing.T) {
cases := []struct {
now time.Time
expectedTime time.Time
}{
{
now: time.Date(2023, time.March, 13, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 6, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 14, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 6, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 15, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 6, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 16, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 6, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 17, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 6, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 18, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 6, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 19, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 6, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 6, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.February, 27, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 14, 23, 35, 42, 101, timeutils.DefaultLocation),
expectedTime: time.Date(2023, time.March, 6, 0, 0, 0, 0, timeutils.DefaultLocation),
},
{
now: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
expectedTime: time.Date(2023, time.March, 6, 0, 0, 0, 0, time.UTC),
},
}
for i, c := range cases {
actualTime := timeutils.StartOfPreviousWeek(c.now)
assert.EqualValues(t, c.expectedTime, actualTime, "case #%d", i+1)
}
}
func TestStartOfNextWeek(t *testing.T) {
cases := []struct {
now time.Time
expectedTime time.Time
}{
{
now: time.Date(2023, time.March, 13, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 20, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 14, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 20, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 15, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 20, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 16, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 20, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 17, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 20, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 18, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 20, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 19, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 20, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.February, 27, 23, 35, 42, 101, time.UTC),
expectedTime: time.Date(2023, time.March, 6, 0, 0, 0, 0, time.UTC),
},
{
now: time.Date(2023, time.March, 14, 23, 35, 42, 101, timeutils.DefaultLocation),
expectedTime: time.Date(2023, time.March, 20, 0, 0, 0, 0, timeutils.DefaultLocation),
},
{
now: time.Date(2023, time.March, 13, 0, 0, 0, 0, time.UTC),
expectedTime: time.Date(2023, time.March, 20, 0, 0, 0, 0, time.UTC),
},
}
for i, c := range cases {
actualTime := timeutils.StartOfNextWeek(c.now)
assert.EqualValues(t, c.expectedTime, actualTime, "case #%d", i+1)
}
}