Documentation ¶
Overview ¶
Package libcalendar implements functions to compute and convert dates from various calendars. These are the Gregorian, ISO, Julian, Islamic, Hebrew, Mayan (long count, haab, tzolkin), French Revolutionary, and Old Hindu (solar, lunar) calendars.
The calendrical algorithms are a translation of the Lisp code discussed in:
Dershowitz, Nachum, and Edward Reingold. 1990. "Calendrical Calculations", Software - Practice and Experience, 20 (9), 899-928. https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.17.4274
Reingold, Edward, Nachum Dershowitz, and Stewart Clamen. 1993. "Calendrical Calculations, II: Three Historical Calendars", Software - Practice & Experience, 23 (4), 383-404. https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.13.9215
Example ¶
Convert from a Gregorian date to the corresponding date of the Julian, ISO, Islamic, Hebrew, French Revolutionary, Mayan (long count, haab, tzolkin), and Old Hindu (solar, lunar) calendars.
package main import ( "fmt" lc "staudtlex.de/libcalendar" ) func main() { // Convert dates // 1. set Gregorian date gregorianDate := lc.GregorianDate{Year: 2022, Month: 06, Day: 15} // 2. convert Gregorian date to absolute (fixed) date absoluteDate := lc.AbsoluteFromGregorian(gregorianDate) // 3. convert absolute dates into corresponding calendar dates fmt.Println("Converting from a Gregorian date to other calendar dates") fmt.Println("Gregorian:\t\t", gregorianDate) fmt.Println("Absolute date:\t\t", absoluteDate) fmt.Println("Julian:\t\t\t", lc.JulianFromAbsolute(absoluteDate)) fmt.Println("ISO:\t\t\t", lc.IsoFromAbsolute(absoluteDate)) fmt.Println("Islamic:\t\t", lc.IslamicFromAbsolute(absoluteDate)) fmt.Println("Hebrew:\t\t\t", lc.HebrewFromAbsolute(absoluteDate)) fmt.Println("French Revolutionary:\t", lc.FrenchFromAbsolute(absoluteDate)) fmt.Println("Mayan Long Count:\t", lc.MayanLongCountFromAbsolute(absoluteDate)) fmt.Println("Mayan Haab:\t\t", lc.MayanHaabFromAbsolute(absoluteDate)) fmt.Println("Mayan Tzolkin:\t\t", lc.MayanTzolkinFromAbsolute(absoluteDate)) fmt.Println("Hindu Solar:\t\t", lc.OldHinduSolarFromAbsolute(absoluteDate)) fmt.Println("Hindu Lunar:\t\t", lc.OldHinduLunarFromAbsolute(absoluteDate)) // Utility functions // 1. Marshal a Date to JSON: gregorian_json := gregorianDate.Date().Json() fmt.Println(gregorian_json) // 2. Unmarshal JSON into a Date json_1 := `{ "components": [2022,6,15], "calendar": "gregorian", "componentNames" :[], "monthNames": [] }` fmt.Println(json_1) unmarshal_json_1 := lc.JsonToDate(json_1) fmt.Println(unmarshal_json_1) // 3. Convert a Date to an absolute date, and compute a Mayan Long count // date from that absolute date mayanLongCount := lc.FromAbsolute( lc.AbsoluteFromDate(unmarshal_json_1), "mayanLongCount") fmt.Println("Mayan long count: ", mayanLongCount) }
Output:
Index ¶
- Constants
- Variables
- func AbsoluteFromDate(d Date) (absoluteDate float64)
- func AbsoluteFromFrench(d FrenchDate) (absoluteDate float64)
- func AbsoluteFromGregorian(d GregorianDate) (absoluteDate float64)
- func AbsoluteFromHebrew(d HebrewDate) (absoluteDate float64)
- func AbsoluteFromIslamic(d IslamicDate) (absoluteDate float64)
- func AbsoluteFromIso(d IsoDate) (absoluteDate float64)
- func AbsoluteFromJulian(d JulianDate) (absoluteDate float64)
- func AbsoluteFromMayanLongCount(d MayanLongCount) (date float64)
- func AbsoluteFromOldHinduLunar(d OldHinduLunarDate) (absoluteDate float64)
- func AbsoluteFromOldHinduSolar(d OldHinduSolarDate) (absoluteDate float64)
- func Advent(year float64) (absoluteDate float64)
- func Christmas(year float64) (absoluteDate float64)
- func DaylightSavingsEnd(year float64) (absoluteDate float64)
- func DaylightSavingsStart(year float64) (absoluteDate float64)
- func DaysInHebrewYear(year float64) (days float64)
- func Easter(year float64) (absoluteDate float64)
- func EasternOrthodoxChristmas(year float64) (absoluteDates []float64)
- func Epiphany(year float64) (absoluteDate float64)
- func FrenchLastDayOfMonth(month, year float64) (day float64)
- func FrenchLeapYear(year float64) bool
- func FromAbsolute(absoluteDate float64, calendar string) string
- func HebrewBirthday(birthdate HebrewDate, year float64) (absoluteDate float64)
- func HebrewCalendarElapsedDays(year float64) (days float64)
- func HebrewLeapYear(year float64) bool
- func HebrewMonthNames(d HebrewDate) []string
- func IndependenceDay(year float64) (absoluteDate float64)
- func IslamicDatesInGregorianYear(month float64, day float64, year float64) (absoluteDates []float64)
- func IslamicLeapYear(year float64) bool
- func JsonDateFromAbsolute(rd float64, calendar string) string
- func KDayOnOrBefore(absoluteDate float64, k float64) float64
- func LaborDay(year float64) (absoluteDate float64)
- func LastDayOfGregorianMonth(month float64, year float64) (day float64)
- func LastDayOfHebrewMonth(month float64, year float64) (day float64)
- func LastDayOfIslamicMonth(month float64, year float64) (day float64)
- func LastDayOfJulianMonth(month float64, year float64) (day float64)
- func LastMonthOfHebrewYear(year float64) (month float64)
- func LastValidDayOfMonth(year, month, day float64, calendar string) (validDay float64)
- func LongHeshvan(year float64) bool
- func LunarLongitude(t *big.Rat) (degrees *big.Rat)
- func LunarPhase(t *big.Rat) (phase float64)
- func MayanHaabDifference(d1, d2 MayanHaabDate) (days float64)
- func MayanHaabOnOrBefore(haab MayanHaabDate, d float64) (date float64)
- func MayanHaabTzolkinOnOrBefore(haab MayanHaabDate, tzolkin MayanTzolkinDate, d float64) (absoluteDate float64)
- func MayanTzolkinDifference(d1, d2 MayanTzolkinDate) (days float64)
- func MemorialDay(year float64) (absoluteDate float64)
- func MuladAlNabi(year float64) (absoluteDates []float64)
- func NewMoon(t *big.Rat) *big.Rat
- func NicaeanRuleEaster(year float64) (absoluteDate float64)
- func NthKDay(n float64, k float64, month float64, year float64) (absoluteDate float64)
- func OldHinduLunarPrecedes(d1, d2 OldHinduLunarDate) bool
- func Passover(year float64) (absoluteDate float64)
- func Pentecost(year float64) (absoluteDate float64)
- func Purim(year float64) (absoluteDate float64)
- func ShortKislev(year float64) bool
- func SolarLongitude(t *big.Rat) (degrees *big.Rat)
- func TaAnitEsther(year float64) (absoluteDate float64)
- func TishaBAv(year float64) (absoluteDate float64)
- func Yahrzeit(deathDate HebrewDate, year float64) (absoluteDate float64)
- func YomKippur(year float64) (absoluteDate float64)
- func Zodiac(t *big.Rat) (zodiac float64)
- type Date
- type FrenchDate
- type GregorianDate
- type HebrewDate
- type IslamicDate
- type IsoDate
- type JulianDate
- type MayanHaabDate
- type MayanLongCount
- type MayanTzolkinDate
- type OldHinduLunarDate
- type OldHinduSolarDate
- type Scanner
Examples ¶
Constants ¶
const ( // Special tokens ILLEGAL token = iota EOF WS // Literals NAME_OR_STRING CALENDAR COMPONENTS ARRAY DIGITS // Misc characters COMMA // , COLON // : )
const MayanDaysBeforeAbsoluteZero float64 = 1137142
Number of days of the Mayan calendar epoch before absolute day 0, according to the Goodman-Martinez-Thompson correlation (see Reingold/Dershowitz 2018).
Variables ¶
var LunarSiderealMonth = add(big.NewRat(27, 1), big.NewRat(4644439, 14438334))
var LunarSynodicMonth = add(big.NewRat(29, 1), big.NewRat(7087771, 13358334))
var SolarMonth = div(SolarSiderealYear, big.NewRat(12, 1))
var SolarSiderealYear = add(big.NewRat(365, 1), big.NewRat(279457, 1080000))
Functions ¶
func AbsoluteFromDate ¶
AbsoluteFromDate returns the absolute (fixed) date from a given calendar date. Note that no checks are performed as to whether the given date is valid, i.e. in the date range for which the calendar was defined. For instance, conversion of dates preceding the 19.07.622 (1 Muharram 1 A.H.) into the Islamic calendar or dates preceding the 22.09.1792 (1 Vendémiare an 1) may yield non-sensical results.
func AbsoluteFromFrench ¶
func AbsoluteFromFrench(d FrenchDate) (absoluteDate float64)
AbsoluteFromFrench returns the absolute (fixed) date from a given French Revolutionary date.
func AbsoluteFromGregorian ¶
func AbsoluteFromGregorian(d GregorianDate) (absoluteDate float64)
AbsoluteFromGregorian computes the absolute (fixed) date from a Gregorian date.
func AbsoluteFromHebrew ¶
func AbsoluteFromHebrew(d HebrewDate) (absoluteDate float64)
AbsoluteFromHebrew computes the absolute (fixed) date from a given Hebrew date.
func AbsoluteFromIslamic ¶
func AbsoluteFromIslamic(d IslamicDate) (absoluteDate float64)
AbsoluteFromIslamic computes the absolute date corresponding to a given Islamic date.
func AbsoluteFromIso ¶
AbsoluteFromIso computes the absolute (fixed) date from an ISO date.
func AbsoluteFromJulian ¶
func AbsoluteFromJulian(d JulianDate) (absoluteDate float64)
AbsoluteFromJulian computes the absolute (fixed) date corresponding to a given Julian date.
func AbsoluteFromMayanLongCount ¶
func AbsoluteFromMayanLongCount(d MayanLongCount) (date float64)
AbsoluteFromMayanLongCount returns the absolute (fixed) date of a given Mayan long count.
func AbsoluteFromOldHinduLunar ¶
func AbsoluteFromOldHinduLunar(d OldHinduLunarDate) (absoluteDate float64)
AbsoluteFromOldHinduLunar returns the absolute (fixed) date corresponding to a given Old Hindu lunar date.
func AbsoluteFromOldHinduSolar ¶
func AbsoluteFromOldHinduSolar(d OldHinduSolarDate) (absoluteDate float64)
AbsoluteFromOldHinduSolar returns the absolute (fixed) date from a given Old Hindu solar date.
func Christmas ¶
Christmas returns the absolute (fixed) date of Gregorian Christmas in a given Gregorian year.
func DaylightSavingsEnd ¶
DaylightSavingsEnd returns the absolute (fixed) date of the end of US daylight savings time.
func DaylightSavingsStart ¶
DaylightSavingsStart returns the absolute (fixed) date of the start of US daylight savings time.
func DaysInHebrewYear ¶
DaysInHebrewYear computes the number of days in a given Hebrew year.
func EasternOrthodoxChristmas ¶
EasternOrthodoxChristmas returns the absolute (fixed) date of Eastern Orthodox Christmas in a given Gregorian year.
func FrenchLastDayOfMonth ¶
FrenchLastDayOfMonth returns the last day of a given French Revolutionary month in a given French Revolutionary year
func FrenchLeapYear ¶
FrenchLeapYear returns true if a given year is a leap year, and false otherwise
func FromAbsolute ¶
FromAbsolute returns a string-formatted calendar date from a given absolute date and calendar name.
func HebrewBirthday ¶
func HebrewBirthday(birthdate HebrewDate, year float64) (absoluteDate float64)
HebrewBirthday determines the absolute (fixed) date of the anniversary of a given Hebrew birth date in a given Hebrew year.
func HebrewCalendarElapsedDays ¶
HebrewCalendarElapsedDays computes the number of days elapsed from the Sunday prior to the start of the Hebrew calendar to the mean conjunction of Tishri of a given Hebrew year.
func HebrewLeapYear ¶
HebrewLeapYear returns true if year is a Hebrew leap year.
func HebrewMonthNames ¶
func HebrewMonthNames(d HebrewDate) []string
HebrewMonthNames returns the month names for a given Hebrew year, taking into account the Hebrew leap year rule.
func IndependenceDay ¶
IndependenceDay returns the absolute (fixed) date of the US Independence Day.
func IslamicDatesInGregorianYear ¶
func IslamicDatesInGregorianYear(month float64, day float64, year float64) (absoluteDates []float64)
IslamicDatesInGregorianYear returns a slice of absolute dates of a given Islamic date (month, day) that occur in a given Gregorian year.
func IslamicLeapYear ¶
IslamicLeapYear returns true if a given Islamic year is leap, and false otherwise.
func JsonDateFromAbsolute ¶
JsonDateFromAbsolute converts a given absolute (fixed) date into the date representation specified in `calendar`. It returns a Date marshalled into a JSON-string.
func KDayOnOrBefore ¶
KDayOnOrBefore computes the absolute date of a given week day in the seven-day interval ending on date.
func LaborDay ¶
LaborDay returns the absolute (fixed) date of US Labor Day in a given Gregorian year.
func LastDayOfGregorianMonth ¶
LastDayOfGregorianMonth returns the last day (number of days) of a given Gregorian month.
func LastDayOfHebrewMonth ¶
LastDayOfHebrewMonth returns the day (number of days) of a given Hebrew month.
func LastDayOfIslamicMonth ¶
LastDayOfIslamicMonth determines the last day of an Islamic month.
func LastDayOfJulianMonth ¶
LastDayOfJulianMonth returns the last day (number of days) of a given Julian month.
func LastMonthOfHebrewYear ¶
LastMonthOfHebrewYear returns the last month of a given Hebrew year.
func LastValidDayOfMonth ¶
lastValidDate checks if the day provided in the parameters is valid for a given year and month. If the day is valid, returns that day, otherwise returns the last valid day of the given year and month.
func LongHeshvan ¶
LongHeshvan returns true if Heshvan is long in a given Hebrew year.
func LunarLongitude ¶
LunarLongitude returns the sidereal longitude of the moon (in degrees) at a given moment (date and fraction of a day).
func LunarPhase ¶
LunarPhase computes the lunar phase of the moon for a given moment (date and fraction of a day).
func MayanHaabDifference ¶
func MayanHaabDifference(d1, d2 MayanHaabDate) (days float64)
MayanHaabDifference computes the number of days between two haab dates.
func MayanHaabOnOrBefore ¶
func MayanHaabOnOrBefore(haab MayanHaabDate, d float64) (date float64)
MayanHaabOnOrBefore returns the absolute (fixed) date of a Mayan haab date on or before a given absolute date.
func MayanHaabTzolkinOnOrBefore ¶
func MayanHaabTzolkinOnOrBefore(haab MayanHaabDate, tzolkin MayanTzolkinDate, d float64) (absoluteDate float64)
MayanHaabTzolkinOnOrBefore returns the absolute date of the latest date on or before a given haab date and a given tzolkin date. Returns NaN when no such combination is found.
func MayanTzolkinDifference ¶
func MayanTzolkinDifference(d1, d2 MayanTzolkinDate) (days float64)
MayanTzolkinDifference returns the number of days between two given Mayan tzolkin dates.
func MemorialDay ¶
MemorialDay returns the absolute (fixed) date of US Memorial Day in a given Gregorian year.
func MuladAlNabi ¶
MuladAlNabi computes slice of absolute (fixed) dates of Mulad al Nabi that occur in a given Gregorian year.
func NewMoon ¶
NewMoon determines the time of the most recent new moon for a given moment (date and fraction of day).
func NicaeanRuleEaster ¶
NicaeanRuleEaster computes the absolute (fixed) date of Easter in a given Julian year.
func NthKDay ¶
NthKDay computes the absolute (fixed) date of the nth kth day in a given month in a given Gregorian year.
func OldHinduLunarPrecedes ¶
func OldHinduLunarPrecedes(d1, d2 OldHinduLunarDate) bool
OldHinduLunarPrecedes returns true if a given Hindu lunar date d1 precedes (i.e. is smaller than) a given Hindu lunar date d2, and false otherwise.
func Pentecost ¶
Pentecost returns the absolute (fixed) date of Pentecost in a given Gregorian year.
func ShortKislev ¶
ShortKislev returns true if Kislev is short in a given Hebrew year.
func SolarLongitude ¶
SolarLongitude returns the position of the sun (in degrees) for a given moment (day and fraction of a day).
func TaAnitEsther ¶
TaAnitEsther returns the absolute (fixed) date of TaAnitEsther in a given Gregorian year.
func Yahrzeit ¶
func Yahrzeit(deathDate HebrewDate, year float64) (absoluteDate float64)
Yahrzeit determines the absolute (fixed) date of the anniversary of a given Hebrew death-date in a given Hebrew year
Types ¶
type Date ¶
type Date struct { Calendar string `json:"calendar"` // calendar name, e.g. "gregorian" Components []float64 `json:"components"` // e.g. []float64{2022, 05, 28} ComponentNames []string `json:"componentNames"` // e.g. []string{"year", "month", "day"} MonthNames []string `json:"monthNames"` // e.g. []string{"January", ..., "December"} }
Date represents a generic container for dates, holding information about arbitrary dates. Possible calendar names are:
func DateFromAbsolute ¶
DateFromAbsolute converts a given absolute (fixed) date into the date representation specified in `calendar`
func JsonToDate ¶
JsonToDate unmarshals a JSON-serialized Date object into a Date struct. Unmarshals only elements "calendar" and "components".
Example ¶
package main import ( "fmt" lc "staudtlex.de/libcalendar" ) func main() { jsonString := `{ "components": [2022,6,15], "calendar": "gregorian", "componentNames" :[], "monthNames": [] }` fmt.Println(jsonString) unmarshal_json := lc.JsonToDate(jsonString) fmt.Println(unmarshal_json) // Convert a Date to an absolute date, and compute a Mayan Long count // date from that absolute date mayanLongCount := lc.FromAbsolute( lc.AbsoluteFromDate(unmarshal_json), "mayanLongCount") fmt.Println("Mayan long count: ", mayanLongCount) }
Output:
func (Date) Json ¶
Json serializes the receiver into a JSON-formatted string.
Example ¶
package main import ( "fmt" lc "staudtlex.de/libcalendar" ) func main() { gregorianDate := lc.GregorianDate{Year: 2022, Month: 06, Day: 15} gregorian_json := gregorianDate.Date().Json() fmt.Println(gregorian_json) }
Output:
type FrenchDate ¶
French date type
func FrenchFromAbsolute ¶
func FrenchFromAbsolute(absoluteDate float64) FrenchDate
FrenchFromAbsolute returns the French Revolutionary date corresponding to a given absolute (fixed) date.
func (FrenchDate) String ¶
func (d FrenchDate) String() string
type GregorianDate ¶
Gregorian date
func GregorianFromAbsolute ¶
func GregorianFromAbsolute(absoluteDate float64) GregorianDate
GregorianFromAbsolute computes the Gregorian date corresponding to a given absolute date.
func (GregorianDate) Date ¶
func (d GregorianDate) Date() Date
Date() creates a Date from its receiver.
func (GregorianDate) String ¶
func (d GregorianDate) String() string
type HebrewDate ¶
Hebrew date
func HebrewFromAbsolute ¶
func HebrewFromAbsolute(absoluteDate float64) HebrewDate
HebrewFromAbsolute computes the Hebrew date corresponding to a given absolute (fixed) date
func (HebrewDate) String ¶
func (d HebrewDate) String() string
type IslamicDate ¶
Islamic date
func IslamicFromAbsolute ¶
func IslamicFromAbsolute(absoluteDate float64) IslamicDate
IslamicFromAbsolute computes the Islamic date corresponding to a given absolute date.
func (IslamicDate) String ¶
func (d IslamicDate) String() string
type IsoDate ¶
ISO date
func IsoFromAbsolute ¶
IsoFromAbsolute computes the IsoDate corresponding to a given absolute (fixed) date.
type JulianDate ¶
type JulianDate GregorianDate
Julian date
func JulianFromAbsolute ¶
func JulianFromAbsolute(absoluteDate float64) JulianDate
JulianFromAbsolute computes the Julian date corresponding to a given absolute date.
func (JulianDate) String ¶
func (d JulianDate) String() string
type MayanHaabDate ¶
Mayan Haab date type
var MayanHaabAtEpoch MayanHaabDate = MayanHaabDate{8, cumku}
MayanHaabAtEpoch denotes the haab date at long count 0.0.0.0.0.
func MayanHaabFromAbsolute ¶
func MayanHaabFromAbsolute(absoluteDate float64) MayanHaabDate
MayanHaabFromAbsolute returns the Mayan haab date corresponding to a given absolute (fixed) date.
func (MayanHaabDate) Date ¶
func (d MayanHaabDate) Date() Date
Date() creates a Date from its receiver.
func (MayanHaabDate) String ¶
func (d MayanHaabDate) String() string
type MayanLongCount ¶
Mayan long count date type
func MayanLongCountFromAbsolute ¶
func MayanLongCountFromAbsolute(absoluteDate float64) MayanLongCount
MayanLongCountFromAbsolute computes the Mayan long count corresponding to the given absolute date.
func (MayanLongCount) Date ¶
func (d MayanLongCount) Date() Date
Date() creates a Date from its receiver.
func (MayanLongCount) String ¶
func (d MayanLongCount) String() string
type MayanTzolkinDate ¶
Mayan Tzolkin date type
var MayanTzolkinAtEpoch MayanTzolkinDate = MayanTzolkinDate{4, ahau}
MayanTzolkinAtEpoch denotes tha tzolkin date at long count 0.0.0.0.0.
func MayanTzolkinFromAbsolute ¶
func MayanTzolkinFromAbsolute(absoluteDate float64) MayanTzolkinDate
MayanTzolkinFromAbsolute returns a Mayan tzolkin date corresponding to a given absolute (fixed) date.
func (MayanTzolkinDate) Date ¶
func (d MayanTzolkinDate) Date() Date
Date() creates a Date from its receiver.
func (MayanTzolkinDate) String ¶
func (d MayanTzolkinDate) String() string
type OldHinduLunarDate ¶
Old Hindu lunar date type
func OldHinduLunarFromAbsolute ¶
func OldHinduLunarFromAbsolute(absoluteDate float64) OldHinduLunarDate
OldHinduLunarFromAbsolute returns the Old Hindu lunar date corresponding to a given absolute (fixed) date.
func (OldHinduLunarDate) Date ¶
func (d OldHinduLunarDate) Date() Date
Date() creates a Date from its receiver.
func (OldHinduLunarDate) String ¶
func (d OldHinduLunarDate) String() string
type OldHinduSolarDate ¶
Old Hindu solar date type
func OldHinduSolarFromAbsolute ¶
func OldHinduSolarFromAbsolute(absoluteDate float64) OldHinduSolarDate
OldHinduSolarFromAbsolute computes the Old Hindu solar date corresponding to a given absolute (fixed) date.
func (OldHinduSolarDate) Date ¶
func (d OldHinduSolarDate) Date() Date
Date() creates a Date from its receiver.
func (OldHinduSolarDate) String ¶
func (d OldHinduSolarDate) String() string
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner represents a lexical scanner.
func NewScanner ¶
NewScanner returns a new instance of Scanner.