16c62a97d5
- 新增时标、ΔT 模型、质心时间与 UT1 支持 - 改进日月食、月掩、行星事件及路径边界计算 - 完善恒星三维自行与动态距离传播 - 扩展 SVG、GeoJSON、KML 输出与底层距离换算工具 - 整理中英文手册、示例资源及回归测试
37 lines
1.2 KiB
Go
37 lines
1.2 KiB
Go
package civiltime
|
|
|
|
import "time"
|
|
|
|
// Event 合并变长民用日的固定偏移候选,handled=false 表示普通日期 / merges fixed-offset candidates on a civil day with an offset change.
|
|
func Event(date time.Time, missing error, calculate func(time.Time) (time.Time, error)) (result time.Time, err error, handled bool) {
|
|
year, month, day := date.Date()
|
|
start := time.Date(year, month, day, 0, 0, 0, 0, date.Location())
|
|
end := time.Date(year, month, day+1, 0, 0, 0, 0, date.Location())
|
|
_, firstOffset := start.Zone()
|
|
_, lastOffset := end.Add(-time.Nanosecond).Zone()
|
|
if firstOffset == lastOffset {
|
|
return time.Time{}, nil, false
|
|
}
|
|
var firstErr error
|
|
for _, offset := range []int{firstOffset, lastOffset} {
|
|
fixed := time.Date(year, month, day, 0, 0, 0, 0, time.FixedZone("", offset))
|
|
candidate, candidateErr := calculate(fixed)
|
|
if candidateErr != nil {
|
|
if firstErr == nil {
|
|
firstErr = candidateErr
|
|
}
|
|
continue
|
|
}
|
|
if !candidate.Before(start) && candidate.Before(end) && (result.IsZero() || candidate.Before(result)) {
|
|
result = candidate.In(date.Location())
|
|
}
|
|
}
|
|
if !result.IsZero() {
|
|
return result, nil, true
|
|
}
|
|
if firstErr != nil {
|
|
return time.Time{}, firstErr, true
|
|
}
|
|
return time.Time{}, missing, true
|
|
}
|