-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module zeroboo/serverutils | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/testify v1.8.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package serverutils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package timeutils | ||
|
||
import "time" | ||
|
||
func GetTimeBeginOfDay(t time.Time) time.Time { | ||
year, month, day := t.Date() | ||
return time.Date(year, month, day, 0, 0, 0, 0, t.Location()) | ||
} | ||
|
||
func GetTimeBeginOfYesterday(t time.Time) time.Time { | ||
year, month, day := t.Date() | ||
|
||
return time.Date(year, month, day, 0, 0, 0, 0, t.Location()).Add(time.Hour * 24 * -1) | ||
} | ||
|
||
func GetSecondsSinceBeginOfDay(t time.Time) int64 { | ||
bod := GetTimeBeginOfDay(t) | ||
result := t.Sub(bod).Seconds() | ||
return int64(result) | ||
} | ||
|
||
func GetTimeBeginOfNextDay(t time.Time) time.Time { | ||
year, month, day := t.Date() | ||
bod := time.Date(year, month, day, 0, 0, 0, 0, t.Location()) | ||
return bod.Add(time.Duration(24) * time.Hour) | ||
} | ||
|
||
func IsDifferentDay(t1 time.Time, t2 time.Time) bool { | ||
day1 := time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, t1.Location()) | ||
day2 := time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, t2.Location()) | ||
return !day2.Equal(day1) | ||
} | ||
|
||
func CalculateDayPassed(from time.Time, to time.Time) int { | ||
dayFrom := time.Date(from.Year(), from.Month(), from.Day(), 0, 0, 0, 0, from.Location()) | ||
dayTo := time.Date(to.Year(), to.Month(), to.Day(), 0, 0, 0, 0, to.Location()) | ||
return int(dayTo.Sub(dayFrom).Hours() / 24) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package serverutils | ||
|
||
import ( | ||
"log" | ||
"testing" | ||
"time" | ||
"zeroboo/serverutils/timeutils" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetTimeBeginOfDay_CurrentTime_CorrectValue(t *testing.T) { | ||
log.Println("TestGetTimeBeginOfDay_CurrentTime_CorrectValue") | ||
currentTime := time.Now() | ||
bod := timeutils.GetTimeBeginOfDay(currentTime) | ||
log.Printf("BeginOfDay of %v is %v", currentTime, bod) | ||
|
||
assert.Equal(t, 0, bod.Hour(), "BOD: 0 hour") | ||
assert.Equal(t, 0, bod.Minute(), "BOD: 0 minute") | ||
assert.Equal(t, 0, bod.Second(), "BOD: 0 second") | ||
|
||
} | ||
|
||
const TestTimeLayout = "2006-01-02T15:04:05.000" | ||
|
||
func TestGetTimeBeginOfDay_PresetDate_CorrectValue(t *testing.T) { | ||
log.Println("TestGetTimeBeginOfDay_CorrectValue") | ||
|
||
testTime, errTestTime := time.Parse(TestTimeLayout, "1945-09-02T04:30:19.750") | ||
bodTime, errBODTime := time.Parse(TestTimeLayout, "1945-09-02T00:00:00.000") | ||
log.Printf("Prepare test done, errTestTime=%v, errBODTime=%v", errTestTime, errBODTime) | ||
assert.Equal(t, nil, errTestTime, "No parsing error") | ||
assert.Equal(t, nil, errBODTime, "No parsing error") | ||
|
||
bod := timeutils.GetTimeBeginOfDay(testTime) | ||
log.Printf("BeginOfDay of %v is %v", testTime, bodTime) | ||
|
||
assert.Equal(t, bod, bodTime, "Expected bod") | ||
} | ||
|
||
func TestGetSecondsSinceBeginOfDay_PresetDate_CorrectValue(t *testing.T) { | ||
log.Println("TestGetTimeBeginOfDay_CorrectValue") | ||
log.Println("TestGetTimeBeginOfDay_CorrectValue") | ||
|
||
testTime, errTestTime := time.Parse(TestTimeLayout, "1945-09-02T04:30:19.750") | ||
log.Printf("Prepare test done, errTestTime=%v", errTestTime) | ||
secondsSinceBOD := timeutils.GetSecondsSinceBeginOfDay(testTime) | ||
log.Printf("Seconds since begin of day of `%v` is %v", testTime, secondsSinceBOD) | ||
|
||
assert.Equal(t, int64(16219), secondsSinceBOD, "Expected seconds") | ||
} |