Files
coin/trade/times.go
2026-01-09 15:48:31 +08:00

51 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package trade
import "time"
// IsTradingTime 判断当前是否为交易时间
// 交易时间星期一至星期六早上8点
func IsTradingTime() bool {
now := time.Now()
// 获取星期几 (Sunday = 0, Monday = 1, ..., Saturday = 6)
weekday := now.Weekday()
// 获取小时数
hour := now.Hour()
// 判断是否为星期一至星期五
if weekday >= time.Monday && weekday <= time.Friday {
return true
} else if weekday == time.Saturday && hour <= 8 { // 星期6并且早上8点前
return true
}
return false
}
func Int64ToTime(ms int64) time.Time {
milliseconds := int64(ms) // 2021-10-01 00:00:00 UTC
seconds := milliseconds / 1000
nanos := (milliseconds % 1000) * 1000000
return time.Unix(seconds, nanos)
}
// IsCurrentHourInRange 判断当前小时是否在指定的开始和结束小时区间内
// startHour: 开始小时 (0-23)
// endHour: 结束小时 (0-23)
// 返回: 如果在区间内返回true否则返回false
func IsCurrentHourInRange(startHour, endHour int) bool {
now := time.Now()
currentHour := now.Hour()
// 处理同一天的情况
if startHour <= endHour {
return currentHour >= startHour && currentHour < endHour
}
// 处理跨天的情况(例如 22:00 - 06:00
return currentHour >= startHour || currentHour < endHour
}