carbon/calendar.go

42 lines
1.2 KiB
Go
Raw Normal View History

2024-01-22 17:08:38 +08:00
package carbon
import (
"gitee.com/golang-module/carbon/v2/calendar/julian"
2024-01-22 17:08:38 +08:00
"gitee.com/golang-module/carbon/v2/calendar/lunar"
)
2024-01-23 18:03:51 +08:00
// Lunar converts gregorian calendar to lunar calendar.
// 将 公历 转化为 农历
2024-01-23 18:03:51 +08:00
func (c Carbon) Lunar() (l lunar.Lunar) {
2024-01-22 17:08:38 +08:00
if c.Error != nil {
l.Error = c.Error
return l
}
2024-01-23 18:03:51 +08:00
return lunar.NewGregorian(c.ToStdTime()).ToLunar()
2024-01-22 17:08:38 +08:00
}
// CreateFromLunar creates a Carbon instance from Lunar date and time.
// 从 农历日期 创建 Carbon 实例
func CreateFromLunar(year, month, day, hour, minute, second int, isLeapMonth bool) Carbon {
c := NewCarbon()
2024-01-23 18:03:51 +08:00
c.time = lunar.NewLunar(year, month, day, hour, minute, second, isLeapMonth).ToGregorian().Time
2024-01-22 17:08:38 +08:00
return c
}
// Julian converts gregorian calendar to Julian calendar
// 将 公历 转化为 儒略历
func (c Carbon) Julian() (j julian.Julian) {
if c.Error != nil {
return j
}
return julian.NewGregorian(c.ToStdTime()).ToJulian()
}
// CreateFromJulian creates a Carbon instance from Julian Day.or Modified Julian Day
// 从 儒略日/简化儒略日 创建 Carbon 实例
func CreateFromJulian(f float64) Carbon {
c := NewCarbon()
c.time = julian.NewJulian(f).ToGregorian().Time
return c
}