Detail the various time operations in Golang
- 2020-11-20 06:08:07
- OfStack
demand
Time format conversion is more troublesome, I wrote a tool, you can through these methods in the tool call each other into the format you want, the code is as follows, the new function will be added later
The implementation code
package utils
import "time"
const (
TIMEFORMAT = "20060102150405"
NORMALTIMEFORMAT = "2006-01-02 15:04:05"
)
// The current time
func GetTime() time.Time{
return time.Now()
}
// Formatted as :20060102150405
func GetTimeString(t time.Time) string {
return t.Format(TIMEFORMAT)
}
// Formatted as :2006-01-02 15:04:05
func GetNormalTimeString(t time.Time) string {
return t.Format(NORMALTIMEFORMAT)
}
// Convert to timestamp -> Number of seconds
func GetTimeUnix(t time.Time) int64 {
return t.Unix()
}
// Convert to timestamp -> Number of milliseconds
func GetTimeMills(t time.Time) int64 {
return t.UnixNano() / 1e6
}
// The timestamp turns the time
func GetTimeByInt(t1 int64) time.Time{
return time.Unix(t1,0)
}
// String rotation time
func GetTimeByString(timestring string) (time.Time,error){
if timestring == "" {
return time.Time{},nil
}
return time.ParseInLocation(TIMEFORMAT, timestring, time.Local)
}
// Standard string rotation time
func GetTimeByNormalString(timestring string) (time.Time,error){
if timestring == "" {
return time.Time{},nil
}
return time.ParseInLocation(NORMALTIMEFORMAT, timestring, time.Local)
}
// Compare the two time sizes
func CompareTime(t1 ,t2 time.Time) bool {
return t1.Before(t2)
}
// n Hours after the time string
func GetNextHourTime(s string, n int64) string {
t2, _ := time.ParseInLocation(TIMEFORMAT, s, time.Local)
t1 := t2.Add(time.Hour * time.Duration(n))
return GetTimeString(t1)
}
// Calculate the time difference between the two
func GetHourDiffer(start_time, end_time string) float32 {
var hour float32
t1, err := time.ParseInLocation(TIMEFORMAT, start_time, time.Local)
t2, err := time.ParseInLocation(TIMEFORMAT, end_time, time.Local)
if err == nil && CompareTime(t1,t2) {
diff := GetTimeUnix(t2) - GetTimeUnix(t1)
hour = float32(diff) / 3600
return hour
}
return hour
}
// Determine if the current time is on the hour
func Checkhours() bool {
_, m, s := GetTime().Clock()
if m == s && m == 0 && s == 0 {
return true
}
return false
}
// The time string is converted to a standard string
func StringToNormalString(t string) string {
if !(len(TIMEFORMAT) == len(t) || len(SHORTTIMESTRING) == len(t)) {
return t
}
if len(SHORTTIMESTRING) == len(t) {
t += "000000"
}
if len(TIMEFORMAT) == len(t) {
t1,err:= GetTimeByString(t)
if err != nil {
return t
}
t = GetTimeString(t1)
}
return t
}
Determine whether a point in time is after a point in time
Procedure: This is very simple for timestamps, but for others you need to convert to time to compare
stringToTime, _ := time.Parse("2006-01-02 15:04:05", "2017-12-12 12:00:00")
beforeOrAfter := stringToTime.After(time.Now())
if beforeOrAfter == true {
fmt.Println("2017-12-12 12:00:00 in tNow after !")
} else {
fmt.Println("2017-12-12 12:00:00 in tNow before !")
}
Judge how much time has passed between one time and the other
Process: This is compared in Time's way
beginTime :=time.Now()
time.Sleep(time.Second*1)
durtime:= time.Since(beginTime)
fmt.Println(" Gone from the present: ",durtime)