|
@@ -1540,7 +1540,6 @@ func daysInMonth(year int, month time.Month) int {
|
|
|
}
|
|
|
return 28
|
|
|
}
|
|
|
- // 其他月份
|
|
|
if month == time.April || month == time.June || month == time.September || month == time.November {
|
|
|
return 30
|
|
|
}
|
|
@@ -1548,12 +1547,14 @@ func daysInMonth(year int, month time.Month) int {
|
|
|
}
|
|
|
|
|
|
// CalculateEndOfMonth 使用天数计算未来月末的天数差
|
|
|
-func CalculateEndOfMonth(baseDate time.Time, months, moveType int) int {
|
|
|
+/*func CalculateEndOfMonth(baseDate time.Time, months, moveType int) int {
|
|
|
// 假设每个月28天,然后算到目标月的下个月
|
|
|
- daysToAdd := 28 * (months + 2)
|
|
|
+ var daysToAdd int
|
|
|
// 计算目标月的下个月月初
|
|
|
if moveType == 2 {
|
|
|
- daysToAdd = -(daysToAdd - 56)
|
|
|
+ daysToAdd = -(28 * months)
|
|
|
+ } else {
|
|
|
+ daysToAdd = 28 * (months + 2)
|
|
|
}
|
|
|
nextMonth := baseDate.AddDate(0, 0, daysToAdd)
|
|
|
|
|
@@ -1567,43 +1568,50 @@ func CalculateEndOfMonth(baseDate time.Time, months, moveType int) int {
|
|
|
daysDifference := int(math.Abs(lastDayOfTargetMonth.Sub(baseDate).Hours() / 24))
|
|
|
|
|
|
return daysDifference
|
|
|
-}
|
|
|
+}*/
|
|
|
|
|
|
// CalculateEndOfMonth 计算从 baseDate 开始经过 months 后目标月的最后一天距离 baseDate 的天数差
|
|
|
-/*func CalculateEndOfMonth(baseDate time.Time, months, moveType int) int {
|
|
|
- // 初始化目标日期
|
|
|
+func CalculateEndOfMonth(baseDate time.Time, months, moveType int) int {
|
|
|
+ // 初始化目标日期为当前日期
|
|
|
targetDate := baseDate
|
|
|
|
|
|
- // 如果 moveType == 2,倒退月份;否则前进月份
|
|
|
+ // 如果 moveType == 2,表示倒退月份;否则为前进月份
|
|
|
if moveType == 2 {
|
|
|
months = -months
|
|
|
}
|
|
|
|
|
|
- // 手动计算目标月份,通过加月份天数循环处理
|
|
|
+ // 手动通过天数加减月份
|
|
|
for i := 0; i < int(math.Abs(float64(months))); i++ {
|
|
|
- // 获取当前月份的天数
|
|
|
-
|
|
|
- days := daysInMonth(targetDate.Year(), targetDate.Month()+1)
|
|
|
+ // 首先将日期调整到当前月份的第一天
|
|
|
+ targetDate = time.Date(targetDate.Year(), targetDate.Month(), 1, 0, 0, 0, 0, targetDate.Location())
|
|
|
|
|
|
- // 根据 moveType 来前进或后退天数
|
|
|
+ // 根据 moveType 来前进或倒退到下一个月的第一天
|
|
|
if months > 0 {
|
|
|
- targetDate = targetDate.AddDate(0, 0, days) // 前进一个月
|
|
|
+ // 前进到下一个月的第一天
|
|
|
+ targetDate = targetDate.AddDate(0, 1, 0)
|
|
|
} else {
|
|
|
- targetDate = targetDate.AddDate(0, 0, -days) // 倒退一个月
|
|
|
+ // 倒退到上一个月的第一天
|
|
|
+ targetDate = targetDate.AddDate(0, -1, 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果是倒退,调整为目标月的最后一天
|
|
|
+ if months < 0 {
|
|
|
+ daysInCurrentMonth := daysInMonth(targetDate.Year(), targetDate.Month())
|
|
|
+ targetDate = time.Date(targetDate.Year(), targetDate.Month(), daysInCurrentMonth, 0, 0, 0, 0, targetDate.Location())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 获取目标月的下个月月初第一天
|
|
|
firstDayOfNextMonth := time.Date(targetDate.Year(), targetDate.Month()+1, 1, 0, 0, 0, 0, targetDate.Location())
|
|
|
|
|
|
- // 获取目标月的最后一天
|
|
|
+ // 获取目标月的最后一天(即下个月月初减去一天)
|
|
|
lastDayOfTargetMonth := firstDayOfNextMonth.AddDate(0, 0, -1)
|
|
|
|
|
|
// 计算天数差
|
|
|
daysDifference := int(math.Abs(lastDayOfTargetMonth.Sub(baseDate).Hours() / 24))
|
|
|
|
|
|
return daysDifference
|
|
|
-}*/
|
|
|
+}
|
|
|
|
|
|
// CalculateDekadTime 计算旬度时间
|
|
|
func CalculateDekadTime(baseDate time.Time, tradingDays, moveType int) int {
|