utils/timex.go
@@ -1,6 +1,9 @@
package utils
import "time"
import (
   "slices"
   "time"
)
// IsOverlap 判断两个时间段是否有重叠
func IsOverlap(start1, end1, start2, end2 string) bool {
@@ -22,6 +25,10 @@
   return time.Now().AddDate(0, offset, 0)
}
func GetDayByOffset(offset int) time.Time {
   return time.Now().AddDate(0, 0, offset)
}
// GetLastMonthPeriod 返回上个月的月初时间和月末时间
func GetLastMonthPeriod(now time.Time) (firstDay time.Time, lastDay time.Time) {
   // 获取本个月的第一天的日期(即本月月初)
@@ -40,3 +47,21 @@
func GetMonthDuration(d time.Time) (duration int) {
   return d.AddDate(0, 1, -1).Day()
}
func CalcWorkHour(begin, end time.Time, dayOff []time.Weekday, cellHour float32) (workHour float32, workingCount int) {
   var currentTime = begin
   for {
      if currentTime.After(end) {
         break
      }
      if slices.Contains(dayOff, currentTime.Weekday()) {
         // nothing
      } else {
         workHour += cellHour
         workingCount++
      }
      currentTime = currentTime.AddDate(0, 0, 1) // .Add(24 * time.Hour)
   }
   return float32(workHour), workingCount
}