gitee.com/quant1x/gox@v1.7.6/api/datetime.go (about) 1 package api 2 3 import ( 4 "errors" 5 "gitee.com/quant1x/gox/util" 6 "strings" 7 "time" 8 ) 9 10 const ( 11 YearOnly = "2006" // 日期格式仅输出"年" 12 ) 13 14 // ParseTime 解析时间差 15 func ParseTime(timestr string) (time.Time, error) { 16 s := strings.TrimSpace(timestr) 17 switch len(s) { 18 case len(util.DateFormat): 19 return time.ParseInLocation(util.DateFormat, s, time.Local) 20 case len(util.DateFormat2): 21 return time.ParseInLocation(util.DateFormat2, s, time.Local) 22 case len(util.DateFormat3): 23 return time.ParseInLocation(util.DateFormat3, s, time.Local) 24 case len(util.TimeFormat2): 25 return time.ParseInLocation(util.TimeFormat2, s, time.Local) 26 case len(util.TimeFormat): 27 return time.ParseInLocation(util.TimeFormat, s, time.Local) 28 case len(util.Timestamp): 29 return time.ParseInLocation(util.Timestamp, s, time.Local) 30 default: 31 return time.Time{}, errors.New("日期格式无法确定") 32 } 33 } 34 35 // DifferDays 计算天数差 36 // 37 // 从 t1 回到 t2 需要多少天 38 func DifferDays(t1, t2 time.Time) int { 39 t1 = time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, time.Local) 40 t2 = time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, time.Local) 41 42 return int(t1.Sub(t2).Hours() / 24) 43 } 44 45 // DateZero t 的0点0分0秒 46 func DateZero(t time.Time) time.Time { 47 return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local) 48 } 49 50 // IsWorkday 是否工作日 51 func IsWorkday(t time.Time) bool { 52 weekDay := t.Weekday() 53 if weekDay == time.Sunday || weekDay == time.Saturday { 54 return false 55 } 56 return true 57 } 58 59 // GetMonthDay 获得当前月的初始和结束日期 60 func GetMonthDay() (string, string) { 61 now := time.Now() 62 currentYear, currentMonth, _ := now.Date() 63 currentLocation := now.Location() 64 65 firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation) 66 lastOfMonth := firstOfMonth.AddDate(0, 1, -1) 67 f := firstOfMonth.Unix() 68 l := lastOfMonth.Unix() 69 return time.Unix(f, 0).Format("2006-01-02") + " 00:00:00", time.Unix(l, 0).Format("2006-01-02") + " 23:59:59" 70 } 71 72 // GetWeekDay 获得当前周的初始和结束日期 73 func GetWeekDay() (string, string) { 74 now := time.Now() 75 offset := int(time.Monday - now.Weekday()) 76 //周日做特殊判断 因为time.Monday = 0 77 if offset > 0 { 78 offset = -6 79 } 80 81 lastoffset := int(time.Saturday - now.Weekday()) 82 //周日做特殊判断 因为time.Monday = 0 83 if lastoffset == 6 { 84 lastoffset = -1 85 } 86 87 firstOfWeek := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset) 88 lastOfWeeK := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, lastoffset+1) 89 f := firstOfWeek.Unix() 90 l := lastOfWeeK.Unix() 91 return time.Unix(f, 0).Format("2006-01-02") + " 00:00:00", time.Unix(l, 0).Format("2006-01-02") + " 23:59:59" 92 } 93 94 // GetQuarterDay 获得当前季度的初始和结束日期 95 func GetQuarterDay(months ...int) (string, string) { 96 mn := 0 97 if len(months) > 0 { 98 mn = months[0] 99 } 100 now := time.Now().AddDate(0, -mn, 0) 101 year := now.Format(YearOnly) 102 month := int(now.Month()) 103 var firstOfQuarter string 104 var lastOfQuarter string 105 if month >= 1 && month <= 3 { 106 //1月1号 107 firstOfQuarter = year + "-01-01 00:00:00" 108 lastOfQuarter = year + "-03-31 23:59:59" 109 } else if month >= 4 && month <= 6 { 110 firstOfQuarter = year + "-04-01 00:00:00" 111 lastOfQuarter = year + "-06-30 23:59:59" 112 } else if month >= 7 && month <= 9 { 113 firstOfQuarter = year + "-07-01 00:00:00" 114 lastOfQuarter = year + "-09-30 23:59:59" 115 } else { 116 firstOfQuarter = year + "-10-01 00:00:00" 117 lastOfQuarter = year + "-12-31 23:59:59" 118 } 119 return firstOfQuarter, lastOfQuarter 120 } 121 122 // GetQuarterDayByDate 通过给定的日期 获得日期所在上一个季度的初始和结束日期 123 // 124 // diffQuarters 季度偏移数, 大于0前移diffQuarters个季度, 小于0后移diffQuarters个季度, 默认为当前季度 125 func GetQuarterDayByDate(date string, diffQuarters ...int) (firstOfQuarter, lastOfQuarter string) { 126 diff := 1 127 if len(diffQuarters) > 0 { 128 diff = diffQuarters[0] 129 } 130 _, firstOfQuarter, lastOfQuarter = GetQuarterByDate(date, diff) 131 return firstOfQuarter, lastOfQuarter 132 } 133 134 // GetQuarterByDate 通过给定的日期 获得日期所在财报的季度、初始以及结束日期 135 // 136 // diffQuarters 季度偏移数, 大于0前移diffQuarters个季度, 小于0后移diffQuarters个季度, 默认为当前季度 137 func GetQuarterByDate(date string, diffQuarters ...int) (quarter, first, last string) { 138 diff := 0 139 if len(diffQuarters) > 0 { 140 diff = diffQuarters[0] 141 } 142 now, _ := ParseTime(date) 143 now = now.AddDate(0, -3*diff, 0) 144 year := now.Format(YearOnly) 145 month := int(now.Month()) 146 var firstOfQuarter string 147 var lastOfQuarter string 148 if month >= 1 && month <= 3 { 149 //1月1号 150 firstOfQuarter = year + "-01-01 00:00:00" 151 lastOfQuarter = year + "-03-31 23:59:59" 152 quarter = year + "Q1" 153 } else if month >= 4 && month <= 6 { 154 firstOfQuarter = year + "-04-01 00:00:00" 155 lastOfQuarter = year + "-06-30 23:59:59" 156 quarter = year + "Q2" 157 } else if month >= 7 && month <= 9 { 158 firstOfQuarter = year + "-07-01 00:00:00" 159 lastOfQuarter = year + "-09-30 23:59:59" 160 quarter = year + "Q4" 161 } else { 162 firstOfQuarter = year + "-10-01 00:00:00" 163 lastOfQuarter = year + "-12-31 23:59:59" 164 quarter = year + "Q4" 165 } 166 return quarter, firstOfQuarter, lastOfQuarter 167 }