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