github.com/dubbogo/gost@v1.14.0/time/time.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 // Package gxtime encapsulates some golang.time functions 19 package gxtime 20 21 import ( 22 "strconv" 23 "time" 24 ) 25 26 func TimeDayDuration(day float64) time.Duration { 27 return time.Duration(day * 24 * float64(time.Hour)) 28 } 29 30 func TimeHourDuration(hour float64) time.Duration { 31 return time.Duration(hour * float64(time.Hour)) 32 } 33 34 func TimeMinuteDuration(minute float64) time.Duration { 35 return time.Duration(minute * float64(time.Minute)) 36 } 37 38 func TimeSecondDuration(sec float64) time.Duration { 39 return time.Duration(sec * float64(time.Second)) 40 } 41 42 func TimeMillisecondDuration(m float64) time.Duration { 43 return time.Duration(m * float64(time.Millisecond)) 44 } 45 46 func TimeMicrosecondDuration(m float64) time.Duration { 47 return time.Duration(m * float64(time.Microsecond)) 48 } 49 50 func TimeNanosecondDuration(n float64) time.Duration { 51 return time.Duration(n * float64(time.Nanosecond)) 52 } 53 54 // desc: convert year-month-day-hour-minute-second to int in second 55 // @month: 1 ~ 12 56 // @hour: 0 ~ 23 57 // @minute: 0 ~ 59 58 func YMD(year int, month int, day int, hour int, minute int, sec int) int { 59 return int(time.Date(year, time.Month(month), day, hour, minute, sec, 0, time.Local).Unix()) 60 } 61 62 // @YMD in UTC timezone 63 func YMDUTC(year int, month int, day int, hour int, minute int, sec int) int { 64 return int(time.Date(year, time.Month(month), day, hour, minute, sec, 0, time.UTC).Unix()) 65 } 66 67 func YMDPrint(sec int, nsec int) string { 68 return time.Unix(int64(sec), int64(nsec)).Format("2006-01-02 15:04:05.99999") 69 } 70 71 func Future(sec int, f func()) { 72 time.AfterFunc(TimeSecondDuration(float64(sec)), f) 73 } 74 75 func Unix2Time(unix int64) time.Time { 76 return time.Unix(unix, 0) 77 } 78 79 func UnixNano2Time(nano int64) time.Time { 80 return time.Unix(nano/1e9, nano%1e9) 81 } 82 83 func UnixString2Time(unix string) time.Time { 84 i, err := strconv.ParseInt(unix, 10, 64) 85 if err != nil { 86 panic(err) 87 } 88 89 return time.Unix(i, 0) 90 } 91 92 // 注意把time转换成unix的时候有精度损失,只返回了秒值,没有用到纳秒值 93 func Time2Unix(t time.Time) int64 { 94 return t.Unix() 95 } 96 97 func Time2UnixNano(t time.Time) int64 { 98 return t.UnixNano() 99 } 100 101 func GetEndTime(format string) time.Time { 102 timeNow := time.Now() 103 switch format { 104 case "day": 105 year, month, _ := timeNow.Date() 106 nextDay := timeNow.AddDate(0, 0, 1).Day() 107 t := time.Date(year, month, nextDay, 0, 0, 0, 0, time.Local) 108 return time.Unix(t.Unix()-1, 0) 109 110 case "week": 111 year, month, _ := timeNow.Date() 112 weekday := int(timeNow.Weekday()) 113 weekendday := timeNow.AddDate(0, 0, 8-weekday).Day() 114 t := time.Date(year, month, weekendday, 0, 0, 0, 0, time.Local) 115 return time.Unix(t.Unix()-1, 0) 116 117 case "month": 118 year := timeNow.Year() 119 nextMonth := timeNow.AddDate(0, 1, 0).Month() 120 t := time.Date(year, nextMonth, 1, 0, 0, 0, 0, time.Local) 121 return time.Unix(t.Unix()-1, 0) 122 123 case "year": 124 nextYear := timeNow.AddDate(1, 0, 0).Year() 125 t := time.Date(nextYear, 1, 1, 0, 0, 0, 0, time.Local) 126 return time.Unix(t.Unix()-1, 0) 127 } 128 129 return timeNow 130 }