package common import ( "fmt" "math" "strconv" "strings" "time" ) func ConvertTime(t string) string { ts := strings.Split(t, "-") for i := 1; i < len(ts); i++ { if s, err := strconv.Atoi(ts[i]); err == nil { if s < 10 { fmt.Println(s) ts[i] = "0" + strconv.Itoa(s) fmt.Println(ts[i]) } } } return strings.Join(ts, "") } func ConvertCurrentDayAndWorkTime(t string) time.Time { t = ConvertTime(t) time.Now().Format("2006-01-02") tt, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02")+" "+t+":00", time.Local) return tt } func ConvertActionTime(t string) time.Time { tt, _ := time.ParseInLocation("2006-01-02 15:04:05", t+":00", time.Local) return tt } func ConvertWorkDateAndActionTime(workDate, actionTime string) time.Time { tt, _ := time.ParseInLocation("2006-01-02 15:04:05", workDate+" "+actionTime+":00", time.Local) return tt } func ConvertWorkDateAndWorkTime(workDate, workTime string) time.Time { workTime = ConvertTime(workTime) tt, _ := time.ParseInLocation("2006-01-02 15:04:05", workDate+" "+workTime+":00", time.Local) return tt } // ConvertWorkDateToTimeFour 将工作日期转换为时间 日期格式为 2006-01-02 时分秒为 04:00:00 func ConvertWorkDateToTimeFour(workDate string) time.Time { tt, _ := time.ParseInLocation("2006-01-02 15:04:05", workDate+" "+"04:00:00", time.Local) return tt } func HandleHourLeaveASeat(originHour float64) float64 { var hour float64 if originHour > 0 && originHour < 0.1 { hour = 0.1 } else { hour, _ = strconv.ParseFloat(strconv.FormatFloat(originHour-0.05, 'f', 1, 64), 10) } return hour } func HandleHourLeaveASeatNotRounding(originHour float64) float64 { var hour float64 if originHour > 0 && originHour < 0.1 { hour = 0.1 } else { hour, _ = strconv.ParseFloat(strconv.FormatFloat(originHour, 'f', 1, 64), 10) } return hour } func HandleHourLeaveFive(originHour float64) float32 { var hour float64 hour = math.Floor(originHour / 1) if math.Mod(originHour, 1) >= 0.5 { hour = hour + 0.5 } return float32(hour) } // CheckTimeInApplyTime 判断 考勤时间点 是否在申请时间段内 /* 1、 开始时间 为 上午 结束时间 下午 则 包含 上班卡 和 下班卡 2、 开始时间 为 上午 结束时间 上午 则 包含 上班卡 不包含 下班卡 3、 开始时间 为 下午 结束时间 下午 则 不包含 上班卡 包含 下班卡 */ func CheckTimeInApplyTime(applyTime, workStartTime, workEndTime time.Time, startInclude, endInclude bool) bool { if startInclude && endInclude { // 包含 上班卡 和 下班卡 if !applyTime.Before(workStartTime) && !applyTime.After(workEndTime) { return true } } else if startInclude && !endInclude { // 包含 上班卡 不包含 下班卡 if !applyTime.Before(workStartTime) && applyTime.Before(workEndTime) { return true } } else if !startInclude && endInclude { // 不包含 上班卡 包含 下班卡 if applyTime.Before(workStartTime) && !applyTime.After(workEndTime) { return true } } return false } func ConvertApplyTimeToTime(applyDate, applyTime string) time.Time { applyTime = ConvertTime(applyTime) tt, _ := time.ParseInLocation("2006-01-02 15:04:05", applyDate+" "+applyTime+":00", time.Local) return tt } // ConvertApplyStartDateToTimeFour 将申请日期转换为时间 申请日期格式为 2006-01-02 时分秒为 04:00:00 func ConvertApplyStartDateToTimeFour(applyDate string) time.Time { tt, _ := time.ParseInLocation("2006-01-02 15:04:05", applyDate+" "+"04:00:00", time.Local) return tt } // ConvertApplyStartDateToTimeZero 将申请日期转换为时间 申请日期格式为 2006-01-02 时分秒为 23:59:59 夜里 func ConvertApplyStartDateToTimeZero(applyDate string) time.Time { tt, _ := time.ParseInLocation("2006-01-02 15:04:05", applyDate+" "+"23:59:59", time.Local) return tt } // ConvertApplyEndDateToTimeFour 将申请日期转换为时间 申请日期格式为 2006-01-02 时分秒为 04:00:01 func ConvertApplyEndDateToTimeFour(applyDate string) time.Time { tt, _ := time.ParseInLocation("2006-01-02 15:04:05", applyDate+" "+"04:00:00", time.Local) return tt.AddDate(0, 0, 1) } // ConvertApplyEndDateToTimeMorningZero 将申请日期转换为时间 申请日期格式为 2006-01-02 时分秒为 00:00:00 凌晨 func ConvertApplyEndDateToTimeMorningZero(applyDate string) time.Time { tt, _ := time.ParseInLocation("2006-01-02 15:04:05", applyDate+" "+"00:00:00", time.Local) return tt } // ConvertApplyStartDateToTimeWithMinute 将申请日期转换为时间 申请日期格式为 2006-01-02 时分秒为 23: + 开始时间的 分钟 + :00 func ConvertApplyStartDateToTimeWithMinute(applyDate string, minute string) time.Time { var minuteStr string addDay := false m, _ := strconv.Atoi(minute) if m >= 30 { minuteStr = fmt.Sprintf("23:%v:00", minute) } else { minuteStr = "00:00:00" addDay = true } fmt.Println("================ ConvertApplyStartDateToTimeWithMinute minute +++++++++++++++++++++++++++++++++++++ ") fmt.Printf("strconv.Atoi(minute) : %+v \n", m) fmt.Printf("minute : %+v\n", minute) fmt.Printf("applyDate : %+v\n", applyDate) fmt.Printf("addDay : %+v\n", addDay) fmt.Println("================ ConvertApplyStartDateToTimeWithMinute minute +++++++++++++++++++++++++++++++++++++ ") tt, _ := time.ParseInLocation("2006-01-02 15:04:05", applyDate+" "+minuteStr, time.Local) if addDay == true { tt = tt.AddDate(0, 0, 1) fmt.Println("================ ConvertApplyStartDateToTimeWithMinute minute +++++++++++++++++++++++++++++++++++++ ") fmt.Printf("tt : %+v\n", tt) fmt.Println("================ ConvertApplyStartDateToTimeWithMinute minute +++++++++++++++++++++++++++++++++++++ ") } return tt } // ConvertApplyEndDateToTimeEarlyMorningZero 将申请日期转换为时间 申请日期格式为 2006-01-02 时分秒为 00:00:00 凌晨 func ConvertApplyEndDateToTimeEarlyMorningZero(applyDate string) time.Time { tt, _ := time.ParseInLocation("2006-01-02 15:04:05", applyDate+" "+"00:00:00", time.Local) return tt.AddDate(0, 0, 1) } // DelTimeDash 剔除日期中的横杠 func DelTimeDash(dates []string) []string { delDashDates := make([]string, 0) if len(dates) > 0 { for i := 0; i < len(dates); i++ { delDashDates = append(delDashDates, strings.Replace(dates[i], "-", "", -1)) } } return delDashDates } // CheckIsBeforeFour 当日的 四点之前 为 前一天的 下班卡 func CheckIsBeforeFour() string { if time.Now().Local().Hour() < 4 { return time.Now().AddDate(0, 0, -1).Local().Format(YYMMDD) } return time.Now().Local().Format(YYMMDD) } func CheckIsBeforeFourTime() time.Time { if time.Now().Local().Hour() < 4 { return time.Now().AddDate(0, 0, -1).Local() } return time.Now().Local() } // CurrentTimeSubOneMinute 当前时间 减一分钟 func CurrentTimeSubOneMinute() time.Time { return time.Now().Local().Add(-time.Minute * 1) } // CurrentDateStr 当前日期 func CurrentDateStr() string { return time.Now().Local().Format(YYMMDD) } // CurrentTimeStr 当前日期 func CurrentTimeStr() string { return time.Now().Local().Format(YYMMDDHHmmss) } // CurrentDateTime 当前日期 func CurrentDateTime() time.Time { return time.Now().Local() } // NextDate 后一天 func NextDate() string { return time.Now().Local().AddDate(0, 0, 1).Format(YYMMDD) } // preDate 前一天 func PreDate() time.Time { return time.Now().Local().AddDate(0, 0, -1) } // PreDateFour 前一天四点 func PreDateFour() time.Time { preDate := time.Now().AddDate(0, 0, -1) return time.Date(preDate.Year(), preDate.Month(), preDate.Day(), 4, 0, 0, 0, time.Local) } // NotWorkDayWorkTimeOn 非工作日 开始时间 为 凌晨 04:00 func NotWorkDayWorkTimeOn() string { return "04:00" } // NotWorkDayWorkTimeOff 非工作日 结束时间 为 次日凌晨 04:00 func NotWorkDayWorkTimeOff() string { return "59:59" } func CurrentMonth() string { t := time.Now() var month string // 25号之后属于下个月 if t.Day() > 25 { tt, _ := time.ParseInLocation("2006-01", t.Format("2006-01"), time.Local) month = tt.AddDate(0, 1, 0).Format("2006-01") } else if t.Day() <= 25 { month = t.Format("2006-01") } return month } func PreMonth(month string) string { //根据传入的 月份 获取 上个月份 if len(strings.Split(month, "-")) == 3 { month = strings.Split(month, "-")[0] + "-" + strings.Split(month, "-")[1] } t, _ := time.ParseInLocation("2006-01", month, time.Local) preMonth := t.AddDate(0, -1, 0).Format("2006-01") return preMonth } // ApplyStartTime 申请开始时间 为 四点之后 func ApplyStartTime(startTime time.Time) time.Time { if startTime.Hour() < 4 { // 获取日期 startDate := time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 0, 0, 0, time.Local) startTime = startDate.Add(4 * time.Hour) } return startTime } // ApplyEndTime 申请结束时间 为 四点之前 func ApplyEndTime(endTime time.Time) time.Time { if endTime.Hour() > 4 { // 获取日期 endDate := time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, time.Local) endTime = endDate.Add(4 * time.Hour) } return endTime } // ApplyEndTimeDefault 申请结束时间 为 四点之前 默认 func ApplyEndTimeDefault(startTime, endTime time.Time) time.Time { // 第二天的 四点前 endDateFour := time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 4, 0, 0, 0, time.Local).AddDate(0, 0, 1) if !endTime.After(endDateFour) { return endTime } return endDateFour } // IsInMonth 判断 传入的日期是否在 当前考勤周期 func IsInMonth(date string, month string) bool { dateT, _ := time.ParseInLocation("2006-01-02", date, time.Local) monthEndT, _ := time.ParseInLocation("2006-01-02", month+"-25", time.Local) monthStartT := monthEndT.AddDate(0, -1, 1) if !dateT.Before(monthStartT) && !dateT.After(monthEndT) { return true } return false } func CalcDuration(start, end time.Time) float64 { return end.Sub(start).Minutes() } // IsAfterMonth 判断传入month 是否在当前周期之后 func IsAfterMonth(date string) bool { monthEndT, _ := time.ParseInLocation("2006-01-02", CurrentMonth()+"-25", time.Local) dateT, _ := time.ParseInLocation("2006-01-02", date, time.Local) if dateT.After(monthEndT) { return true } return false } func IsInArray(str string, arr []string) bool { for _, v := range arr { if v == str { return true } } return false } func ConvertResignationDateForMonth(date string) time.Month { dateT, _ := time.ParseInLocation("2006-01-02", date, time.Local) return dateT.Month() } func ConvertCurrentTimeForMonth() time.Month { return time.Now().Month() } func ConvertCurrentTimeWithoutSecond() string { return time.Now().Format(YYMMDDHHmm) } func ConvertTimeStrToTime(t string) time.Time { tt, _ := time.ParseInLocation("2006-01-02", t, time.Local) return tt } // CheckAttendanceMonth 根据 传入的时间 确定 考勤的周期 func CheckAttendanceMonth(date string) string { t := ConvertTimeStrToTime(date) var month string // 25号之后属于下个月 if t.Day() > 25 { tt, _ := time.ParseInLocation("2006-01", t.Format("2006-01"), time.Local) month = tt.AddDate(0, 1, 0).Format("2006-01") } else if t.Day() <= 25 { month = t.Format("2006-01") } return month } // Add25ForMonth 为月份添加 25号 func Add25ForMonth(month string) string { return month + "-25" } // IsInDates 判断 传入的日期是否在 当前日期内 func IsInDates(date string, dates []string) bool { dateT, _ := time.ParseInLocation("2006-01-02", date, time.Local) if len(dates) > 1 { endT, _ := time.ParseInLocation("2006-01-02", dates[len(dates)-1], time.Local) startT, _ := time.ParseInLocation("2006-01-02", dates[0], time.Local) if !dateT.Before(startT) && !dateT.After(endT) { return true } } else if len(dates) == 1 { if date == dates[0] { return true } } return false } func MakeDatesMap(dates []string) map[string]string { datesMap := make(map[string]string) for _, v := range dates { datesMap[v] = v } return datesMap } func BeforeStartDate(date string, dates []string) bool { dateT, _ := time.ParseInLocation("2006-01-02", date, time.Local) if len(dates) > 0 { startT, _ := time.ParseInLocation("2006-01-02", dates[0], time.Local) if dateT.Before(startT) { return true } } return false } func AfterEndDate(date string, dates []string) bool { dateT, _ := time.ParseInLocation("2006-01-02", date, time.Local) if len(dates) > 0 { if len(dates) == 1 { endT, _ := time.ParseInLocation("2006-01-02", dates[0], time.Local) if dateT.After(endT) { return true } } else if len(dates) > 1 { endT, _ := time.ParseInLocation("2006-01-02", dates[len(dates)-1], time.Local) if dateT.After(endT) { return true } } } return false }