-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
61 lines (49 loc) · 1.51 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
Some tools including getting today, getting location dsn setting.
*/
package utils
import (
"strconv"
"strings"
"time"
)
const localLocation = "Local"
// GetToday is a function to get today's date
func GetToday() time.Time {
// get the location setting from the environment
location, err := time.LoadLocation(EnvLocation())
// if failed to load location time, it will use local time to return
if err != nil {
now := time.Now()
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
return today
}
// return the time with location setting
now := time.Now().In(location)
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, location)
return today
}
// GetSqlDsnLocation is to edit SQL DSN setting
func GetSqlDsnLocation() string {
// get the location and replace word "/" into "%2F"
location := EnvLocation()
if strings.ContainsAny(location, "/") {
return strings.Replace(location, "/", "%2F", 1)
}
return localLocation
}
// GetCacheKey is to get the key for the cache
func GetCacheKey(age int, country string, gender string, platform string) string {
return strconv.Itoa(age) + country + gender + platform
}
// GetNow is the function to get now
func GetNow() time.Time {
// get the location setting from the environment
location, err := time.LoadLocation(EnvLocation())
// if failed to load location time, it will use local time to return
if err != nil {
return time.Now()
}
// return the time with location in environment
return time.Now().In(location)
}