Skip to content

Commit

Permalink
add time and random utils
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroboo committed Sep 23, 2022
1 parent 3501c3e commit ea8399e
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 10 deletions.
32 changes: 32 additions & 0 deletions serverutils.go
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
/*
Common utilities
*/
package serverutils

import (
"log"
"os"
"strings"
)

var Time timeUtil
var Random randomUtil

// Return environment value or default value if not exists
func GetEnvOrDefault(key string, defaultValue string) string {
value := os.Getenv(key)
if len(value) == 0 {
value = defaultValue
}
return strings.TrimSpace(value)
}

func GetHostName() string {
hostName, _ := os.Hostname()
return hostName
}

func PrintEnv() {
for _, env := range os.Environ() {
log.Printf("Environment: %v", env)
}
}
24 changes: 21 additions & 3 deletions timeutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestGetTimeBeginOfDay_CurrentTime_CorrectValue(t *testing.T) {
log.Println("TestGetTimeBeginOfDay_CurrentTime_CorrectValue")
currentTime := time.Now()
bod := GetTimeBeginOfDay(currentTime)
bod := Time.GetTimeBeginOfDay(currentTime)
log.Printf("BeginOfDay of %v is %v", currentTime, bod)

assert.Equal(t, 0, bod.Hour(), "BOD: 0 hour")
Expand All @@ -31,7 +31,7 @@ func TestGetTimeBeginOfDay_PresetDate_CorrectValue(t *testing.T) {
assert.Equal(t, nil, errTestTime, "No parsing error")
assert.Equal(t, nil, errBODTime, "No parsing error")

bod := GetTimeBeginOfDay(testTime)
bod := Time.GetTimeBeginOfDay(testTime)
log.Printf("BeginOfDay of %v is %v", testTime, bodTime)

assert.Equal(t, bod, bodTime, "Expected bod")
Expand All @@ -43,8 +43,26 @@ func TestGetSecondsSinceBeginOfDay_PresetDate_CorrectValue(t *testing.T) {

testTime, errTestTime := time.Parse(TestTimeLayout, "1945-09-02T04:30:19.750")
log.Printf("Prepare test done, errTestTime=%v", errTestTime)
secondsSinceBOD := GetSecondsSinceBeginOfDay(testTime)
secondsSinceBOD := Time.GetSecondsSinceBeginOfDay(testTime)
log.Printf("Seconds since begin of day of `%v` is %v", testTime, secondsSinceBOD)

assert.Equal(t, int64(16219), secondsSinceBOD, "Expected seconds")
}

func TestFormatLogTime(t *testing.T) {
log.Println("TestFormatLogTime")
testTime, _ := time.Parse(TestTimeLayout, "1945-09-02T04:30:19.750")

formattedTime := Time.FormatTimeLogFile(testTime)

//formatted
assert.Equal(t, "19450902_043019", formattedTime, "Correct value")
}

// go test -timeout 30s -run ^TestFormatLogTime$ github.com/zeroboo/serverutils
func TestRandomString_CorrectSize(t *testing.T) {
log.Println("TestRandomString_CorrectSize")
assert.Equal(t, 4, len(Random.GetRandomString(4)), "Correct size")

assert.Equal(t, 16, len(Random.GetRandomString(16)), "Correct size")
}
45 changes: 45 additions & 0 deletions utilRandom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package serverutils

import (
"math/rand"
"time"
)

const capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const normalLetters = "abcdefghijklmnopqrstuvwxyz"
const allLetters = normalLetters + capitalLetters

var seed int64 = time.Now().UnixNano()

type randomUtil struct {
}

func init() {
rand.Seed(seed)
}

func (*randomUtil) GetRandomString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = allLetters[rand.Intn(len(allLetters))]
}
return string(b)
}

func (*randomUtil) GetRandomStringFromString(n int, letters string) string {

b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}

func (*randomUtil) GetRandomStringCapitalize(n int) string {

b := make([]byte, n)
for i := range b {
b[i] = capitalLetters[rand.Intn(len(capitalLetters))]
}
return string(b)
}
31 changes: 24 additions & 7 deletions timeutils.go → utilTime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,55 @@ package serverutils

import "time"

const TIME_FORMAT_CLIENT string = "2006-01-02 03:04:05"
const TIME_FORMAT_LOG_FILE string = "20060102_030405"

type timeUtil struct {
}

//Return the time at 00:00:00 of the same day as givenTime
func GetTimeBeginOfDay(givenTime time.Time) time.Time {
func (*timeUtil) GetTimeBeginOfDay(givenTime time.Time) time.Time {
year, month, day := givenTime.Date()
return time.Date(year, month, day, 0, 0, 0, 0, givenTime.Location())
}

//Return the time at 00:00:00 of the day before givenTime
func GetTimeBeginOfYesterday(givenTime time.Time) time.Time {
func (*timeUtil) GetTimeBeginOfYesterday(givenTime time.Time) time.Time {
year, month, day := givenTime.Date()
return time.Date(year, month, day, 0, 0, 0, 0, givenTime.Location()).Add(time.Hour * 24 * -1)
}

func GetSecondsSinceBeginOfDay(t time.Time) int64 {
bod := GetTimeBeginOfDay(t)
func (u *timeUtil) GetSecondsSinceBeginOfDay(t time.Time) int64 {
bod := u.GetTimeBeginOfDay(t)
result := t.Sub(bod).Seconds()
return int64(result)
}

func GetTimeBeginOfNextDay(t time.Time) time.Time {
func (*timeUtil) 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 {
func (*timeUtil) 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 {
func (*timeUtil) 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)
}

func (*timeUtil) FormatTimeLogTime(from time.Time) string {
return from.Format(TIME_FORMAT_LOG_FILE)
}

/*
Return formatted time for log file, only has digits and _
*/
func (*timeUtil) FormatTimeLogFile(from time.Time) string {
return from.Format(TIME_FORMAT_LOG_FILE)
}

0 comments on commit ea8399e

Please sign in to comment.