141 lines
3.6 KiB
Go
141 lines
3.6 KiB
Go
|
package tools
|
||
|
|
||
|
import (
|
||
|
"kefu/types"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// 当前时间戳/秒
|
||
|
func Now() int64 {
|
||
|
return time.Now().Unix()
|
||
|
}
|
||
|
func GetNowTime() string {
|
||
|
return time.Now().Format("2006-01-02 15:04:05")
|
||
|
}
|
||
|
|
||
|
// 时间戳转时间字符串
|
||
|
func IntToTimeStr(sourceTime int64, formatStr string) string {
|
||
|
return time.Unix(sourceTime, 0).Format(formatStr)
|
||
|
}
|
||
|
|
||
|
// 时间戳转时间类型
|
||
|
func IntToTime(sourceTime int64) time.Time {
|
||
|
return time.Unix(sourceTime, 0)
|
||
|
}
|
||
|
|
||
|
// 时间字符串转时间戳
|
||
|
func TimeStrToInt(sourceTime string) int64 {
|
||
|
timeLayout := "2006-01-02 15:04:05" //转化所需模板
|
||
|
loc, _ := time.LoadLocation("Local") //重要:获取时区
|
||
|
theTime, _ := time.ParseInLocation(timeLayout, sourceTime, loc) //使用模板在对应时区转化为time.time类型
|
||
|
return theTime.Unix()
|
||
|
}
|
||
|
|
||
|
// 时间字符串转时间类型
|
||
|
func TimeStrToTime(sourceTime string) time.Time {
|
||
|
timeLayouts := []string{
|
||
|
"2006-01-02 15:04:05",
|
||
|
"2006-01-02",
|
||
|
// 添加更多的时间格式模板,按照需要进行扩展
|
||
|
}
|
||
|
var parsedTime time.Time
|
||
|
var err error
|
||
|
|
||
|
for _, layout := range timeLayouts {
|
||
|
parsedTime, err = time.Parse(layout, sourceTime)
|
||
|
if err == nil {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
return parsedTime
|
||
|
}
|
||
|
|
||
|
// 获取多长天之前的日期
|
||
|
func GetPastDate(daysAgo int) string {
|
||
|
pastDate := time.Now().AddDate(0, 0, -daysAgo)
|
||
|
return pastDate.Format("2006-01-02 15:04:05")
|
||
|
}
|
||
|
|
||
|
// 格式化日期
|
||
|
func Date(layout string, timeStr string) string {
|
||
|
return TimeStrToTime(timeStr).Format(layout)
|
||
|
}
|
||
|
|
||
|
// 格式化日期
|
||
|
func DateDefault(myTime time.Time) string {
|
||
|
return myTime.Format("2006-01-02 15:04:05")
|
||
|
}
|
||
|
|
||
|
// 格式化日期
|
||
|
func DateFormat(layout string, timeStr types.Time) string {
|
||
|
return timeStr.Format(layout)
|
||
|
}
|
||
|
|
||
|
// 是否在工作时间
|
||
|
func IsWorkTime(weekDays []string, monringRange, afternoonRange, otherRange []string) bool {
|
||
|
if !IsInWeekRange(weekDays) {
|
||
|
return false
|
||
|
}
|
||
|
if len(monringRange) == 2 && IsInTimeRange(monringRange[0], monringRange[1]) {
|
||
|
return true
|
||
|
}
|
||
|
if len(afternoonRange) == 2 && IsInTimeRange(afternoonRange[0], afternoonRange[1]) {
|
||
|
return true
|
||
|
}
|
||
|
if len(otherRange) == 2 && IsInTimeRange(otherRange[0], otherRange[1]) {
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// 判断当前星期几是否在给定的星期范围内容,["星期一", "星期二"]
|
||
|
func IsInWeekRange(weekDays []string) bool {
|
||
|
// 获取当前时间
|
||
|
now := time.Now()
|
||
|
// 获取当前星期几
|
||
|
weekday := now.Weekday()
|
||
|
chineseWeekdays := []string{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}
|
||
|
nowWeekDay := chineseWeekdays[weekday]
|
||
|
if !IsInArray(nowWeekDay, weekDays) {
|
||
|
return false
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// 判断当前时间(小时分钟)是否在给定范围内
|
||
|
func IsInTimeRange(startTime, endTime string) bool {
|
||
|
// 获取当前时间
|
||
|
now := time.Now()
|
||
|
// 获取当前时间的小时和分钟
|
||
|
currentHour, currentMinute, _ := now.Clock()
|
||
|
|
||
|
startHour, startMinute, _ := GetHourAndMinute(startTime)
|
||
|
endHour, endMinute, _ := GetHourAndMinute(endTime)
|
||
|
|
||
|
// 判断当前时间是否在给定范围内
|
||
|
if currentHour > startHour && currentHour < endHour {
|
||
|
return true
|
||
|
} else if currentHour == startHour && currentMinute >= startMinute {
|
||
|
return true
|
||
|
} else if currentHour == endHour && currentMinute < endMinute {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// 获取给定时间字符串的小时和分钟
|
||
|
func GetHourAndMinute(timeStr string) (int, int, error) {
|
||
|
// 定义时间格式
|
||
|
layout := "2006-01-02 15:04"
|
||
|
// 将字符串解析为时间
|
||
|
t, err := time.Parse(layout, timeStr)
|
||
|
if err != nil {
|
||
|
return 0, 0, err
|
||
|
}
|
||
|
// 获取小时和分钟
|
||
|
hour := t.Hour()
|
||
|
minute := t.Minute()
|
||
|
return hour, minute, nil
|
||
|
}
|