Documentation
¶
Overview ¶
Package datetime holds what dates and times need beyond the time package: calendar systems (Gregorian, Persian, or an app's own), locale conventions (first day of the week, 12 or 24 hours, month and weekday names, date patterns, digits), formatting, relative times ("in 2 days", "yesterday"), time zones, and day arithmetic that daylight saving doesn't break. Giode's date pickers and calendar views are built on it.
Importing it embeds the IANA time zone database (time/tzdata), so zones load on every platform, Windows and phones included.
Index ¶
- Variables
- func AddDays(t time.Time, n int) time.Time
- func DaysBetween(a, b time.Time) int
- func Format(t time.Time, s Style) string
- func LoadZone(name string) (*time.Location, error)
- func OffsetBetween(t time.Time, a, b *time.Location) time.Duration
- func Relative(t, now time.Time) string
- func SameDay(a, b time.Time) bool
- func SetCalendar(cal Calendar)
- func SetFirstWeekday(d time.Weekday)
- func SetHour12(v int)
- func SetMonthNames(calendarID, lang string, wide, short, genitive []string)
- func StartOfDay(t time.Time) time.Time
- func StartOfMonth(cal Calendar, t time.Time) time.Time
- func StartOfWeek(t time.Time, first time.Weekday) time.Time
- func ZoneLabel(t time.Time) string
- type Calendar
- type Date
- type Locale
- func (l Locale) Calendar() Calendar
- func (l Locale) DayPeriod(h int) string
- func (l Locale) Digits(s string) string
- func (l Locale) FirstWeekday() time.Weekday
- func (l Locale) Format(t time.Time, s Style) string
- func (l Locale) FormatPattern(t time.Time, pattern string) string
- func (l Locale) Hour12() bool
- func (l Locale) InZones(t time.Time, theirs *time.Location, s Style) (yours, their string)
- func (l Locale) Language() string
- func (l Locale) MonthName(cal Calendar, month int, w Width) string
- func (l Locale) ParseDate(s string, thisYear int) (Date, error)
- func (l Locale) ParseTime(s string) (h, m int, err error)
- func (l Locale) Relative(t, now time.Time) string
- func (l Locale) Tag() string
- func (l Locale) WeekdayName(d time.Weekday, w Width) string
- type Style
- type Width
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoDate = errors.New("datetime: not a date") ErrNoTime = errors.New("datetime: not a time") )
ErrNoDate and ErrNoTime are what the parsers return for text they can't read.
Functions ¶
func DaysBetween ¶
DaysBetween returns the number of calendar days from a's day to b's, in a's location: 1 from 23:00 to 01:00 the next day.
func OffsetBetween ¶
OffsetBetween returns how far zone b's clocks are ahead of a's at t.
func SetCalendar ¶
func SetCalendar(cal Calendar)
SetCalendar makes dates use cal whatever the locale; nil goes back to the locale's (Persian in Iran and Afghanistan, Gregorian elsewhere).
func SetFirstWeekday ¶
SetFirstWeekday makes weeks start on d whatever the locale; -1 goes back to the locale's.
func SetHour12 ¶
func SetHour12(v int)
SetHour12 makes times use 12 hours (1) or 24 (0) whatever the locale; -1 goes back to the locale's.
func SetMonthNames ¶
SetMonthNames gives the month names of a calendar in a language, for a calendar an app adds or a language Giode lacks; short and genitive (the form after a day number, as in Russian) may be nil.
func StartOfDay ¶
StartOfDay returns the first instant of t's day in t's location (midnight, or later where a DST change skips midnight).
func StartOfMonth ¶
StartOfMonth returns the start of the month holding t in cal.
func StartOfWeek ¶
StartOfWeek returns the start of the week holding t, weeks starting on first.
Types ¶
type Calendar ¶
type Calendar interface {
// ID names it: "gregorian", "persian".
ID() string
// FromTime returns the date of t, in t's location.
FromTime(t time.Time) Date
// ToTime returns the start of d in loc. Days past the end of a
// month roll over into the next, as time.Date does.
ToTime(d Date, loc *time.Location) time.Time
// MonthsInYear and DaysInMonth describe the calendar.
MonthsInYear(year int) int
DaysInMonth(year, month int) int
}
Calendar is a calendar system. Gregorian and Persian are built in; an app adds another (Hijri, Hebrew, Buddhist...) by implementing it and passing it to SetCalendar, with its month names (SetMonthNames).
var Gregorian Calendar = gregorian{}
Gregorian is the Gregorian calendar.
var Persian Calendar = persian{}
Persian is the Persian (Solar Hijri, Jalali) calendar used in Iran and Afghanistan: its year starts at the March equinox, its first six months have 31 days, the next five 30, and Esfand 29, or 30 in a leap year. Dates follow the 2820-year cycle's breaks, as in jalaali-js, from 1 to 3177 AP.
type Date ¶
type Date struct {
Year, Month, Day int
}
Date is a day in a calendar system: Month from 1, Day from 1.
type Locale ¶
type Locale struct {
// contains filtered or unexported fields
}
Locale is the date and time conventions of a language and region, from a tag such as "en-US", "fa-IR" or "de".
func Current ¶
func Current() Locale
Current returns the conventions of the app's locale (i18n.Locale), with the overrides set here (SetFirstWeekday, SetHour12, SetCalendar).
func (Locale) Calendar ¶
Calendar returns the calendar dates use: Persian for Persian in Iran and Afghanistan, Gregorian elsewhere, unless SetCalendar chose one.
func (Locale) Digits ¶
Digits writes the ASCII digits in s in the locale's own: Persian digits in Persian, Arabic-Indic in Arabic (except the Maghreb).
func (Locale) FirstWeekday ¶
FirstWeekday returns the day weeks start on.
func (Locale) FormatPattern ¶
FormatPattern formats t with a Unicode date pattern (see Style's letters), in the locale's calendar, names and digits.
func (Locale) InZones ¶
InZones formats t in the local zone and in another person's, for "3 PM your time, 11:30 PM their time" displays. Each carries its zone label.
func (Locale) MonthName ¶
MonthName returns month's name in cal, in the locale's language, as a title (a calendar's header); i18n keys "giode.month.<calendar>.<n>" and "giode.month_short.<calendar>.<n>" override it.
func (Locale) ParseDate ¶
ParseDate reads a date typed in the locale's order ("11/20/2026" in the US, "20.11.2026" in Germany, "1405/8/29" in Iran), with any separators, native digits, a two-digit year (the nearest century), a month by name ("20 Nov 2026"), or a missing year (this year's, from thisYear).
func (Locale) ParseTime ¶
ParseTime reads a time of day: "15:04", "3:04 pm", "3pm", "1504", "3.04", "۱۵:۰۴", with the locale's or English AM/PM. It returns hours (0 to 23) and minutes.
func (Locale) Relative ¶
Relative describes t from now: "now", "5 minutes ago", "in 3 hours", "yesterday", "tomorrow", "in 2 days", "3 weeks ago", "in 4 months", "2 years ago". Days count calendar days, so 23:00 yesterday is "yesterday" at 01:00. Its texts are i18n keys "giode.rel.*", with English, German, French, Spanish, Russian and Persian built in.
type Style ¶
type Style int
Style is a date or time format of a locale.
const ( // DateShort is all numbers: 11/20/2026, 20.11.2026, ۱۴۰۵/۸/۲۹. DateShort Style = iota // DateMedium names the month briefly: Nov 20, 2026. DateMedium // DateLong names the weekday and month: Friday, November 20, 2026. DateLong // MonthYear is a calendar's header: November 2026. MonthYear // DayMonth is a date without the year: Nov 20. DayMonth // Time is hours and minutes: 3:04 PM or 15:04. Time // TimeSeconds adds seconds. TimeSeconds // DateTimeShort is DateShort and Time. DateTimeShort // DateTimeMedium is DateMedium and Time. DateTimeMedium )