github.com/tickstep/library-go@v0.1.1/time/time.go (about) 1 package time 2 3 import ( 4 "fmt" 5 "time" 6 ) 7 8 var ( 9 // CSTLocation 东八区时区 10 CSTLocation = time.FixedZone("CST", 8*3600) 11 ) 12 13 /* 14 BeijingTimeOption 根据给定的 get 返回时间格式. 15 16 get: 时间格式 17 18 "Refer": 2017-7-21 12:02:32.000 19 "printLog": 2017-7-21_12:02:32 20 "day": 21 21 "ymd": 2017-7-21 22 "hour": 12 23 默认时间戳: 1500609752 24 */ 25 func BeijingTimeOption(get string) string { 26 //获取北京(东八区)时间 27 now := time.Now().In(CSTLocation) 28 year, mon, day := now.Date() 29 hour, min, sec := now.Clock() 30 millisecond := now.Nanosecond() / 1e6 31 switch get { 32 case "Refer": 33 return fmt.Sprintf("%d-%d-%d %02d:%02d:%02d.%03d", year, mon, day, hour, min, sec, millisecond) 34 case "printLog": 35 return fmt.Sprintf("%d-%d-%d_%02dh%02dm%02ds", year, mon, day, hour, min, sec) 36 case "day": 37 return fmt.Sprint(day) 38 case "ymd": 39 return fmt.Sprintf("%d-%d-%d", year, mon, day) 40 case "hour": 41 return fmt.Sprint(hour) 42 default: 43 return fmt.Sprint(time.Now().Unix()) 44 } 45 } 46 47 // FormatTime 将 Unix 时间戳, 转换为字符串 48 func FormatTime(t int64) string { 49 tt := time.Unix(t, 0).In(CSTLocation) 50 year, mon, day := tt.Date() 51 hour, min, sec := tt.Clock() 52 return fmt.Sprintf("%d-%02d-%02d %02d:%02d:%02d", year, mon, day, hour, min, sec) 53 }