Documentation
¶
Overview ¶
Package date is a pure-Go (no cgo) reimplementation of Ruby's date standard library — MRI 4.0.5's Date and DateTime — the deterministic, interpreter-independent core of calendar arithmetic, parsing and formatting.
The calendar core is built from scratch on the astronomical Julian Day Number (JDN): every Date is, fundamentally, an integer day count, so arbitrary BC/AD years, the proleptic Gregorian calendar and day/month arithmetic all reduce to integer math. No use is made of Go's time package for the calendar (only the Today seam defaults to it); the JDN conversions follow the same formulae MRI's date_core.c uses, so results match MRI byte-for-byte.
A DateTime extends a Date with a time-of-day (hour/minute/second, a fractional nanosecond part) and a UTC offset in seconds, mirroring MRI's split between Date (a calendar value pinned to midnight) and DateTime (an instant).
Index ¶
- Constants
- Variables
- func SetToday(y, m, d int) func()
- func SetTodayInstant(y, m, d, h, min, s, ns int) func()
- type Date
- func Commercial(cwyear, cweek, cwday int) (*Date, error)
- func DateJD(jd int) *Date
- func NewDate(y, m, d int) (*Date, error)
- func NewDateStart(y, m, d, start int) (*Date, error)
- func NewDateTime(y, m, d, h, min, s, offsetSec int) (*Date, error)
- func Now() *Date
- func Ordinal(y, yday int) (*Date, error)
- func Parse(s string, comp bool) (*Date, error)
- func ParseDateTime(s string, comp bool) (*Date, error)
- func Strptime(s, format string) (*Date, error)
- func Today() *Date
- func (d *Date) Asctime() string
- func (d *Date) Cmp(o *Date) int
- func (d *Date) Ctime() string
- func (d *Date) Cwday() int
- func (d *Date) Cweek() int
- func (d *Date) Cwyear() int
- func (d *Date) Day() int
- func (d *Date) Diff(o *Date) int
- func (d *Date) Downto(limit *Date, fn func(*Date)) int
- func (d *Date) Equal(o *Date) bool
- func (d *Date) Hour() int
- func (d *Date) Httpdate() string
- func (d *Date) IsDateTime() bool
- func (d *Date) Iso8601() string
- func (d *Date) Jd() int
- func (d *Date) Jisx0301() string
- func (d *Date) Leap() bool
- func (d *Date) Min() int
- func (d *Date) Minus(n int) *Date
- func (d *Date) Mjd() int
- func (d *Date) Month() int
- func (d *Date) NextDay(n int) *Date
- func (d *Date) NextMonth(n int) *Date
- func (d *Date) NextYear(n int) *Date
- func (d *Date) NsecOfDay() int64
- func (d *Date) Offset() int
- func (d *Date) Plus(n int) *Date
- func (d *Date) PlusMonths(n int) *Date
- func (d *Date) PlusYears(n int) *Date
- func (d *Date) PrevDay(n int) *Date
- func (d *Date) PrevMonth(n int) *Date
- func (d *Date) PrevYear(n int) *Date
- func (d *Date) Rfc2822() string
- func (d *Date) Rfc3339() string
- func (d *Date) Sec() int
- func (d *Date) SecFractionNanos() int64
- func (d *Date) Step(limit *Date, step int, fn func(*Date)) int
- func (d *Date) Strftime(format string) string
- func (d *Date) String() string
- func (d *Date) Succ() *Date
- func (d *Date) ToDate() *Date
- func (d *Date) Upto(limit *Date, fn func(*Date)) int
- func (d *Date) Wday() int
- func (d *Date) Yday() int
- func (d *Date) Year() int
Constants ¶
const ( ITALY = 2299161 // 1582-10-15, MRI's default reform point ENGLAND = 2361222 // 1752-09-14 Gregorian = -1 << 62 // proleptic Gregorian everywhere (Date::GREGORIAN, -∞) Julian = 1 << 62 // proleptic Julian everywhere (Date::JULIAN, +∞) )
Calendar-reform sentinels for the start parameter (Date::ITALY / ENGLAND / GREGORIAN / JULIAN in MRI). ITALY is the default — the date the Gregorian calendar was first adopted (1582-10-15 = JD 2_299_161).
Variables ¶
var ErrInvalidDate = errors.New("invalid date")
ErrInvalidDate is returned by the constructors when the requested calendar / week-date / ordinal coordinates do not denote a real date.
Functions ¶
func SetToday ¶
func SetToday(y, m, d int) func()
SetToday overrides the clock Today consults and returns a function that restores the previous one — the test seam that keeps "today" deterministic.
func SetTodayInstant ¶
func SetTodayInstant(y, m, d, h, min, s, ns int) func()
SetTodayInstant is SetToday with a full wall-clock instant, so tests that exercise Now's time-of-day are deterministic too. It returns the restore function.
Types ¶
type Date ¶
type Date struct {
// contains filtered or unexported fields
}
Date is an MRI-compatible Ruby Date / DateTime value. It is an astronomical Julian Day Number (jd) plus, for a DateTime, a time-of-day in nanoseconds since local midnight (nsec, 0 for a plain Date) and a UTC offset in seconds (offset). The isDateTime flag distinguishes a Date (which formats and inspects as a bare calendar date) from a DateTime (which carries the wall clock).
func Commercial ¶
Commercial constructs Date.commercial(cwyear, cweek, cwday) — an ISO week date (cwday 1 = Monday .. 7 = Sunday). Out-of-range coordinates yield ErrInvalidDate.
func DateJD ¶
DateJD constructs Date.jd(jd) — the date with the given astronomical Julian Day Number, under the default ITALY reform.
func NewDate ¶
NewDate constructs Date.new(y, m, d) / Date.civil(y, m, d) under the default ITALY reform. A negative month or day counts from the end (month -1 = December, day -1 = last of the month), as in MRI. An out-of-range coordinate, or one falling in the 1582 reform gap, yields ErrInvalidDate.
func NewDateStart ¶
NewDateStart is NewDate with an explicit calendar-reform point (Date.new's optional `start` — ITALY / ENGLAND / GREGORIAN / JULIAN, or any JDN).
func NewDateTime ¶
NewDateTime constructs DateTime.new(y, m, d, h, min, s, offset). The offset is in seconds east of UTC. An invalid calendar date or out-of-range clock field yields ErrInvalidDate.
func Now ¶
func Now() *Date
Now returns the current local instant as a DateTime in UTC. Like Today it is the sole wall-clock consumer for DateTime and is driven by the same seam.
func Ordinal ¶
Ordinal constructs Date.ordinal(y, yday) — the yday-th day of year y (negative yday counts from the end). An out-of-range yday yields ErrInvalidDate.
func Parse ¶
Parse reads s heuristically (Date.parse) and returns a plain Date — any time-of-day in s is recognised (so the string still parses) but dropped, as MRI's Date.parse yields a Date. comp controls two-digit-year expansion for the slashed/dashed numeric forms. Use ParseDateTime to retain the clock.
func ParseDateTime ¶
ParseDateTime reads s heuristically (DateTime.parse), retaining any time-of-day and zone it finds.
func Strptime ¶
Strptime parses s against the explicit Ruby strptime format (Date.strptime / DateTime.strptime). An unmatched format or out-of-range value is an error.
func Today ¶
func Today() *Date
Today returns the current local date as a plain Date. It is the only place the calendar core consults the wall clock; tests inject a deterministic clock via SetToday so they never depend on real time.
func (*Date) Cmp ¶
Cmp orders d against o: -1 if earlier, 0 if equal, +1 if later (Date#<=>), comparing by jd and, for DateTimes, by the instant (jd-day plus the offset-adjusted time-of-day).
func (*Date) Cwday ¶
Cwday returns the ISO commercial day-of-week, 1 = Monday .. 7 = Sunday (Date#cwday).
func (*Date) Cwyear ¶
Cwyear returns the ISO commercial week-numbering year (Date#cwyear), which can differ from the calendar year at the year boundary.
func (*Date) Diff ¶
Diff returns the whole number of days from o to d (d - o), positive when d is later — MRI's Date#- between two dates yields a Rational that, for whole midnights, reduces to this integer.
func (*Date) Httpdate ¶
Httpdate renders Date#httpdate — the RFC 1123 / HTTP form in GMT, so the instant is shifted to UTC first (which can move it to an adjacent day).
func (*Date) IsDateTime ¶
IsDateTime reports whether d carries a time-of-day (a DateTime) rather than being a bare calendar Date.
func (*Date) Jisx0301 ¶
Jisx0301 renders Date#jisx0301 — the Japanese-era calendar form "E YY.MM.DD" (e.g. "R08.06.29"), with the time appended for a DateTime. A pre-Meiji date falls back to ISO-8601.
func (*Date) NsecOfDay ¶
NsecOfDay returns the nanoseconds since local midnight (internal/whole-day).
func (*Date) Offset ¶
Offset returns the UTC offset in seconds east of Greenwich (DateTime#offset is this over 86_400 as a day fraction; callers that need the fraction divide).
func (*Date) PlusMonths ¶
PlusMonths returns d shifted by n calendar months (Date#>> for n>0, Date#<< for n<0), with MRI's day-of-month clamping (Jan 31 >> 1 → Feb 28).
func (*Date) PlusYears ¶
PlusYears returns d shifted by n years (Date#next_year(n) / #prev_year), a 12n-month shift with the same clamping (Feb 29 → Feb 28 off a leap year).
func (*Date) Rfc2822 ¶
Rfc2822 renders Date#rfc2822 / #rfc822 — "Day, D Mon YYYY HH:MM:SS ±HHMM" (the day-of-month is not zero-padded, matching MRI).
func (*Date) Rfc3339 ¶
Rfc3339 renders Date#rfc3339 / DateTime#rfc3339 — always the full timestamp form, so a plain Date appears at midnight UTC.
func (*Date) SecFractionNanos ¶
SecFractionNanos returns the sub-second part in nanoseconds, 0 .. 999_999_999 (the numerator MRI's DateTime#sec_fraction reports as a Rational over 1e9).
func (*Date) Step ¶
Step calls fn for each date from d to limit inclusive, stepping by step days (which may be negative), mirroring Date#step. It returns the number of steps taken. A zero step would not terminate and so takes none.
func (*Date) Strftime ¶
Strftime formats d per the Ruby format string (Date#strftime / DateTime#strftime).
func (*Date) String ¶
String renders Date#to_s: a plain Date as "YYYY-MM-DD" and a DateTime as the ISO-8601 instant "YYYY-MM-DDTHH:MM:SS±HH:MM".
func (*Date) ToDate ¶
ToDate is the public form of dateOnly: the calendar Date underlying a DateTime (DateTime#to_date), with the clock dropped.
func (*Date) Upto ¶
Upto calls fn for each date from d to limit inclusive, one day at a time (Date#upto); it is Step with a step of 1.
