Browse Source

Merge branch 'refs/heads/eta_2.1.9_index_phase_shift_0909@guomengyuan' into debug

gmy 6 months ago
parent
commit
46482b941a
1 changed files with 20 additions and 7 deletions
  1. 20 7
      utils/common.go

+ 20 - 7
utils/common.go

@@ -1519,16 +1519,29 @@ func CalculateTradingDays(baseDate time.Time, tradingDays int) int {
 	return totalDays
 }
 
-// CalculateEndOfMonth 计算从当前到未来的月末的天数差
+// getLastDayOfMonth 获取某个月的最后一天
+func getLastDayOfMonth(t time.Time) time.Time {
+	// 移动到下个月的第一天,然后回退一天得到当前月的最后一天
+	return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location()).AddDate(0, 1, -1)
+}
+
+// CalculateEndOfMonth 使用天数计算未来月末的天数差
 func CalculateEndOfMonth(baseDate time.Time, months int) int {
-	// Move forward `months` months
-	newDate := baseDate.AddDate(0, months, 0)
+	// 假设每个月28天,然后算到目标月的下个月
+	daysToAdd := 28 * (months + 1)
+	// 计算目标月的下个月月初
+	nextMonth := baseDate.AddDate(0, 0, daysToAdd)
+
+	// 获取目标月月初的第一天
+	firstDayOfNextMonth := time.Date(nextMonth.Year(), nextMonth.Month(), 1, 0, 0, 0, 0, nextMonth.Location())
+
+	// 获取目标月的最后一天(即月初减去1天)
+	lastDayOfTargetMonth := firstDayOfNextMonth.AddDate(0, 0, -1)
 
-	// Find the last day of the month
-	lastDay := time.Date(newDate.Year(), newDate.Month()+1, 1, 0, 0, 0, 0, newDate.Location()).AddDate(0, 0, -1)
+	// 计算天数差
+	daysDifference := int(lastDayOfTargetMonth.Sub(baseDate).Hours() / 24)
 
-	// Calculate the difference in days
-	return int(lastDay.Sub(baseDate).Hours() / 24)
+	return daysDifference
 }
 
 // CalculateEndOfQuarter 计算从当前到未来的季度末的天数差