datetime

package
v1.0.31-alpha.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
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 AddDays

func AddDays(t time.Time, n int) time.Time

AddDays returns t moved by n calendar days, its wall time kept.

func DaysBetween

func DaysBetween(a, b time.Time) int

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 Format

func Format(t time.Time, s Style) string

Format formats t in the current locale (Current).

func LoadZone

func LoadZone(name string) (*time.Location, error)

LoadZone returns the IANA zone name, or UTC and an error when it isn't one.

func OffsetBetween

func OffsetBetween(t time.Time, a, b *time.Location) time.Duration

OffsetBetween returns how far zone b's clocks are ahead of a's at t.

func Relative

func Relative(t, now time.Time) string

Relative describes t from now in the current locale.

func SameDay

func SameDay(a, b time.Time) bool

SameDay reports whether a and b fall on the same day in a's location.

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

func SetFirstWeekday(d time.Weekday)

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

func SetMonthNames(calendarID, lang string, wide, short, genitive []string)

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

func StartOfDay(t time.Time) time.Time

StartOfDay returns the first instant of t's day in t's location (midnight, or later where a DST change skips midnight).

func StartOfMonth

func StartOfMonth(cal Calendar, t time.Time) time.Time

StartOfMonth returns the start of the month holding t in cal.

func StartOfWeek

func StartOfWeek(t time.Time, first time.Weekday) time.Time

StartOfWeek returns the start of the week holding t, weeks starting on first.

func ZoneLabel

func ZoneLabel(t time.Time) string

ZoneLabel names t's zone as a UTC offset, "GMT+3:30" or "GMT-5", for zones without a common abbreviation; "GMT" at UTC.

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.

func AddMonths

func AddMonths(cal Calendar, d Date, n int) Date

AddMonths returns d moved by n months in cal, its day kept or, past the end of the new month, its last day (January 31 + 1 month is February 28 or 29).

func (Date) Before

func (d Date) Before(e Date) bool

Before reports whether d is before e.

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 NewLocale

func NewLocale(tag string) Locale

NewLocale returns the conventions of a locale tag.

func (Locale) Calendar

func (l Locale) Calendar() Calendar

Calendar returns the calendar dates use: Persian for Persian in Iran and Afghanistan, Gregorian elsewhere, unless SetCalendar chose one.

func (Locale) DayPeriod

func (l Locale) DayPeriod(h int) string

DayPeriod returns the 12-hour clock's "AM" or "PM" for hour h.

func (Locale) Digits

func (l Locale) Digits(s string) string

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

func (l Locale) FirstWeekday() time.Weekday

FirstWeekday returns the day weeks start on.

func (Locale) Format

func (l Locale) Format(t time.Time, s Style) string

Format formats t in the locale's style, in its calendar and digits.

func (Locale) FormatPattern

func (l Locale) FormatPattern(t time.Time, pattern string) string

FormatPattern formats t with a Unicode date pattern (see Style's letters), in the locale's calendar, names and digits.

func (Locale) Hour12

func (l Locale) Hour12() bool

Hour12 reports whether times use a 12-hour clock.

func (Locale) InZones

func (l Locale) InZones(t time.Time, theirs *time.Location, s Style) (yours, their string)

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) Language

func (l Locale) Language() string

Language returns its language code.

func (Locale) MonthName

func (l Locale) MonthName(cal Calendar, month int, w Width) string

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

func (l Locale) ParseDate(s string, thisYear int) (Date, error)

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

func (l Locale) ParseTime(s string) (h, m int, err error)

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

func (l Locale) Relative(t, now time.Time) string

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.

func (Locale) Tag

func (l Locale) Tag() string

Tag returns the locale's tag.

func (Locale) WeekdayName

func (l Locale) WeekdayName(d time.Weekday, w Width) string

WeekdayName returns d's name in the locale's language; i18n keys "giode.weekday.<n>" (0 Sunday) and "giode.weekday_short.<n>" override it.

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
)

type Width

type Width int

Width is how long a name is.

const (
	// Wide is the full name: "January", "Monday".
	Wide Width = iota
	// Abbreviated is short: "Jan", "Mon".
	Abbreviated
	// Narrow is a letter or so: "J", "M".
	Narrow
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL