|
@@ -1532,10 +1532,13 @@ func getLastDayOfMonth(t time.Time) time.Time {
|
|
|
}
|
|
|
|
|
|
// CalculateEndOfMonth 使用天数计算未来月末的天数差
|
|
|
-func CalculateEndOfMonth(baseDate time.Time, months int) int {
|
|
|
+func CalculateEndOfMonth(baseDate time.Time, months, moveType int) int {
|
|
|
// 假设每个月28天,然后算到目标月的下个月
|
|
|
daysToAdd := 28 * (months + 2)
|
|
|
// 计算目标月的下个月月初
|
|
|
+ if moveType == 2 {
|
|
|
+ daysToAdd = -(daysToAdd - 28)
|
|
|
+ }
|
|
|
nextMonth := baseDate.AddDate(0, 0, daysToAdd)
|
|
|
|
|
|
// 获取目标月月初的第一天
|
|
@@ -1545,25 +1548,76 @@ func CalculateEndOfMonth(baseDate time.Time, months int) int {
|
|
|
lastDayOfTargetMonth := firstDayOfNextMonth.AddDate(0, 0, -1)
|
|
|
|
|
|
// 计算天数差
|
|
|
- daysDifference := int(lastDayOfTargetMonth.Sub(baseDate).Hours() / 24)
|
|
|
+ daysDifference := int(math.Abs(lastDayOfTargetMonth.Sub(baseDate).Hours() / 24))
|
|
|
|
|
|
return daysDifference
|
|
|
}
|
|
|
|
|
|
+// CalculateDekadTime 计算旬度时间
|
|
|
+func CalculateDekadTime(baseDate time.Time, tradingDays, moveType int) int {
|
|
|
+ // 记录原始日期
|
|
|
+ oldDate := baseDate
|
|
|
+
|
|
|
+ // 计算移动的旬数,1 旬为 10 天
|
|
|
+ var moveDekads int
|
|
|
+ if moveType != 2 {
|
|
|
+ moveDekads = tradingDays
|
|
|
+ } else {
|
|
|
+ moveDekads = -tradingDays
|
|
|
+ }
|
|
|
+
|
|
|
+ // 移动的天数为旬数 * 10,初步移动日期
|
|
|
+ baseDate = baseDate.AddDate(0, 0, moveDekads*10)
|
|
|
+
|
|
|
+ // 调整日期到最近的旬:10号、20号或月末
|
|
|
+ baseDate = adjustToNearestDekad(baseDate)
|
|
|
+
|
|
|
+ // 计算时间差
|
|
|
+ subDays := baseDate.Sub(oldDate)
|
|
|
+ days := int(subDays.Hours() / 24)
|
|
|
+
|
|
|
+ fmt.Printf("最终日期: %s, 总天数差: %d 天\n", baseDate.Format("2006-01-02"), days)
|
|
|
+
|
|
|
+ return days
|
|
|
+}
|
|
|
+
|
|
|
+// adjustToNearestDekad 调整日期到最近的旬
|
|
|
+func adjustToNearestDekad(date time.Time) time.Time {
|
|
|
+ day := date.Day()
|
|
|
+ lastDayOfMonth := getLastDayOfMonth(date).Day()
|
|
|
+
|
|
|
+ if day <= 10 {
|
|
|
+ return time.Date(date.Year(), date.Month(), 10, 0, 0, 0, 0, date.Location())
|
|
|
+ } else if day <= 20 {
|
|
|
+ return time.Date(date.Year(), date.Month(), 20, 0, 0, 0, 0, date.Location())
|
|
|
+ } else {
|
|
|
+ return time.Date(date.Year(), date.Month(), lastDayOfMonth, 0, 0, 0, 0, date.Location())
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*// getLastDayOfMonth 返回指定日期所在月份的最后一天
|
|
|
+func getLastDayOfMonth(date time.Time) time.Time {
|
|
|
+ // 获取下个月的第一天
|
|
|
+ nextMonth := date.AddDate(0, 1, -date.Day()+1)
|
|
|
+ // 下个月第一天减去一天即为当前月的最后一天
|
|
|
+ lastDay := nextMonth.AddDate(0, 0, -1)
|
|
|
+ return lastDay
|
|
|
+}*/
|
|
|
+
|
|
|
// CalculateEndOfQuarter 计算从当前到未来的季度末的天数差
|
|
|
-func CalculateEndOfQuarter(baseDate time.Time, quarters int) int {
|
|
|
+func CalculateEndOfQuarter(baseDate time.Time, quarters, moveType int) int {
|
|
|
// Move forward `quarters` quarters (3 months per quarter)
|
|
|
- return CalculateEndOfMonth(baseDate, quarters*3)
|
|
|
+ return CalculateEndOfMonth(baseDate, quarters*3, moveType)
|
|
|
}
|
|
|
|
|
|
// CalculateEndOfYear 计算从当前到未来的年末的天数差
|
|
|
-func CalculateEndOfYear(baseDate time.Time, years int) int {
|
|
|
+func CalculateEndOfYear(baseDate time.Time, years, moveType int) int {
|
|
|
// Move forward `years` years
|
|
|
- return CalculateEndOfMonth(baseDate, years*12)
|
|
|
+ return CalculateEndOfMonth(baseDate, years*12, moveType)
|
|
|
}
|
|
|
|
|
|
// CalculateEndOfHalfYear 计算从当前到未来的半年的天数差
|
|
|
-func CalculateEndOfHalfYear(baseDate time.Time, years int) int {
|
|
|
+func CalculateEndOfHalfYear(baseDate time.Time, years, moveType int) int {
|
|
|
// Move forward `half years` years
|
|
|
- return CalculateEndOfMonth(baseDate, years*6)
|
|
|
+ return CalculateEndOfMonth(baseDate, years*6, moveType)
|
|
|
}
|