Documentation
¶
Overview ¶
Package datetimeformat implements the ECMA-402 Intl.DateTimeFormat constructor.
locales, _ := locale.ParseList("en-US")
format, _ := datetimeformat.New(locales, datetimeformat.Options{})
out := format.Format(time.Now())
_ = out
See README.md for usage examples and SPECS/30-datetimeformat.md for the contract.
Example ¶
Example demonstrates Intl.DateTimeFormat.prototype.format from ECMA-402.
package main
import (
"fmt"
"time"
gointl "github.com/agentable/go-intl"
"github.com/agentable/go-intl/datetimeformat"
"github.com/agentable/go-intl/locale"
)
func main() {
format, err := datetimeformat.New(mustLocaleList("en-US"), datetimeformat.Options{
TimeZone: gointl.String("UTC"),
})
if err != nil {
panic(err)
}
t := time.Date(2020, time.May, 14, 15, 4, 0, 0, time.UTC)
fmt.Println(format.Format(t))
}
func mustLocaleList(tags ...string) locale.List {
locales, err := locale.ParseList(tags...)
if err != nil {
panic(err)
}
return locales
}
Output: 5/14/2020
Example (Options) ¶
Example_options demonstrates Intl.DateTimeFormat constructor options from ECMA-402.
package main
import (
"fmt"
"time"
gointl "github.com/agentable/go-intl"
"github.com/agentable/go-intl/datetimeformat"
"github.com/agentable/go-intl/locale"
)
func main() {
format, err := datetimeformat.New(mustLocaleList("en-US"), datetimeformat.Options{
DateStyle: gointl.String(string(datetimeformat.LongDateTimeStyle)),
TimeZone: gointl.String("UTC"),
})
if err != nil {
panic(err)
}
t := time.Date(2020, time.May, 14, 15, 4, 0, 0, time.UTC)
fmt.Println(format.Format(t))
}
func mustLocaleList(tags ...string) locale.List {
locales, err := locale.ParseList(tags...)
if err != nil {
panic(err)
}
return locales
}
Output: May 14, 2020
Index ¶
- Constants
- func SupportedLocalesOf(locales locale.List, opts Options) (locale.List, error)
- type DateTimeFormat
- func (f *DateTimeFormat) Format(t time.Time) string
- func (f *DateTimeFormat) FormatRange(start, end time.Time) (string, error)
- func (f *DateTimeFormat) FormatRangeToParts(start, end time.Time) ([]RangePart, error)
- func (f *DateTimeFormat) FormatToParts(t time.Time) []Part
- func (f *DateTimeFormat) ResolvedOptions() ResolvedOptions
- type FieldStyle
- type FormatMatcher
- type HourCycle
- type LocaleMatcher
- type MonthStyle
- type NumericStyle
- type Options
- type Part
- type PartType
- type RangePart
- type RangeSource
- type ResolvedOptions
- type Style
- type TimeZoneName
Examples ¶
Constants ¶
const ( LookupLocaleMatcher LocaleMatcher = "lookup" BestFitLocaleMatcher LocaleMatcher = "best fit" BasicFormatMatcher FormatMatcher = "basic" BestFitFormatMatcher FormatMatcher = "best fit" H11HourCycle HourCycle = "h11" H12HourCycle HourCycle = "h12" H23HourCycle HourCycle = "h23" H24HourCycle HourCycle = "h24" NarrowFieldStyle FieldStyle = "narrow" ShortFieldStyle FieldStyle = "short" LongFieldStyle FieldStyle = "long" NumericFieldStyle NumericStyle = "numeric" TwoDigitFieldStyle NumericStyle = "2-digit" NumericMonthStyle MonthStyle = "numeric" TwoDigitMonthStyle MonthStyle = "2-digit" NarrowMonthStyle MonthStyle = "narrow" ShortMonthStyle MonthStyle = "short" LongMonthStyle MonthStyle = "long" ShortTimeZoneName TimeZoneName = "short" LongTimeZoneName TimeZoneName = "long" ShortOffsetTimeZoneName TimeZoneName = "shortOffset" LongOffsetTimeZoneName TimeZoneName = "longOffset" ShortGenericTimeZoneName TimeZoneName = "shortGeneric" LongGenericTimeZoneName TimeZoneName = "longGeneric" FullDateTimeStyle Style = "full" LongDateTimeStyle Style = "long" MediumDateTimeStyle Style = "medium" ShortDateTimeStyle Style = "short" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DateTimeFormat ¶
type DateTimeFormat struct {
// contains filtered or unexported fields
}
func (*DateTimeFormat) Format ¶
func (f *DateTimeFormat) Format(t time.Time) string
Example ¶
format, err := New(mustLocaleList("en-US"), Options{Year: stringPtr(NumericFieldStyle), Month: stringPtr(ShortMonthStyle), Day: stringPtr(NumericFieldStyle)})
if err != nil {
panic(err)
}
fmt.Println(format.Format(time.Date(2026, time.May, 8, 0, 0, 0, 0, time.UTC)))
Output: May 8, 2026
Example (Timezone) ¶
format, err := New(mustLocaleList("en-US"), Options{TimeZone: stringPtr("America/New_York"), Hour: stringPtr(NumericFieldStyle), TimeZoneName: stringPtr(LongGenericTimeZoneName)})
if err != nil {
panic(err)
}
fmt.Println(format.Format(time.Date(2026, time.January, 8, 12, 0, 0, 0, time.UTC)))
Output: 7 AM Eastern Time
func (*DateTimeFormat) FormatRange ¶
func (f *DateTimeFormat) FormatRange(start, end time.Time) (string, error)
func (*DateTimeFormat) FormatRangeToParts ¶
func (f *DateTimeFormat) FormatRangeToParts(start, end time.Time) ([]RangePart, error)
func (*DateTimeFormat) FormatToParts ¶
func (f *DateTimeFormat) FormatToParts(t time.Time) []Part
Example ¶
ExampleDateTimeFormat_FormatToParts demonstrates Intl.DateTimeFormat.prototype.formatToParts from ECMA-402.
package main
import (
"fmt"
"time"
gointl "github.com/agentable/go-intl"
"github.com/agentable/go-intl/datetimeformat"
"github.com/agentable/go-intl/locale"
)
func main() {
format, err := datetimeformat.New(mustLocaleList("en-US"), datetimeformat.Options{
Year: gointl.String(string(datetimeformat.NumericFieldStyle)),
Month: gointl.String(string(datetimeformat.ShortMonthStyle)),
Day: gointl.String(string(datetimeformat.NumericFieldStyle)),
TimeZone: gointl.String("UTC"),
})
if err != nil {
panic(err)
}
t := time.Date(2020, time.May, 14, 15, 4, 0, 0, time.UTC)
for _, part := range format.FormatToParts(t) {
fmt.Printf("%s=%q\n", part.Type, part.Value)
}
}
func mustLocaleList(tags ...string) locale.List {
locales, err := locale.ParseList(tags...)
if err != nil {
panic(err)
}
return locales
}
Output: month="May" literal=" " day="14" literal=", " year="2020"
Example ¶
format, err := New(mustLocaleList("en-US"), Options{Weekday: stringPtr(LongFieldStyle), Month: stringPtr(LongMonthStyle), Day: stringPtr(NumericFieldStyle), Year: stringPtr(NumericFieldStyle)})
if err != nil {
panic(err)
}
for _, part := range format.FormatToParts(time.Date(2026, time.May, 8, 0, 0, 0, 0, time.UTC)) {
fmt.Printf("%s=%q\n", part.Type, part.Value)
}
Output: weekday="Friday" literal=", " month="May" literal=" " day="8" literal=", " year="2026"
func (*DateTimeFormat) ResolvedOptions ¶
func (f *DateTimeFormat) ResolvedOptions() ResolvedOptions
type FieldStyle ¶
type FieldStyle string
FieldStyle selects textual field width. Mirrors Intl.DateTimeFormat options "weekday", "era", and "dayPeriod".
type FormatMatcher ¶
type FormatMatcher string
FormatMatcher selects date-time component matching. Mirrors Intl.DateTimeFormat option "formatMatcher".
type HourCycle ¶
type HourCycle string
HourCycle selects the resolved hour cycle. Mirrors Intl.DateTimeFormat option "hourCycle".
type LocaleMatcher ¶
type LocaleMatcher string
LocaleMatcher selects locale negotiation behavior. Mirrors Intl.DateTimeFormat option "localeMatcher".
type MonthStyle ¶
type MonthStyle string
MonthStyle selects month field width. Mirrors Intl.DateTimeFormat option "month".
type NumericStyle ¶
type NumericStyle string
NumericStyle selects numeric or two-digit field width. Mirrors Intl.DateTimeFormat numeric field options.
type Options ¶
type Options struct {
Calendar *string
NumberingSystem *string
LocaleMatcher *string
FormatMatcher *string
TimeZone *string
TimeZoneName *string
Weekday *string
Era *string
Year *string
Month *string
Day *string
DayPeriod *string
Hour *string
Minute *string
Second *string
HourCycle *string
Hour12 *bool
DateStyle *string
TimeStyle *string
FractionalSecondDigits *int
}
type PartType ¶
type PartType string
PartType identifies a format part record. Mirrors Intl.DateTimeFormat part field "type".
const ( // ECMA-402 §15.5.1 Table 9 (Type Field of FormatDateTimePattern part records). PartYear PartType = "year" PartMonth PartType = "month" PartDay PartType = "day" PartHour PartType = "hour" PartMinute PartType = "minute" PartSecond PartType = "second" PartWeekday PartType = "weekday" PartEra PartType = "era" PartDayPeriod PartType = "dayPeriod" PartTimeZoneName PartType = "timeZoneName" PartLiteral PartType = "literal" PartFractionalSecond PartType = "fractionalSecond" // PartRelatedYear and PartYearName appear when the resolved calendar is a // non-Gregorian cyclic calendar (e.g. Chinese, Dangi); the active Gregorian // path never emits them, but the constants must exist so consumer switches // remain exhaustive when calendar support widens. PartRelatedYear PartType = "relatedYear" PartYearName PartType = "yearName" // PartUnknown is ECMA-402's explicit fallback for an unrecognized format // pattern element. PartUnknown PartType = "unknown" )
type RangePart ¶
type RangePart struct {
Type PartType `json:"type"`
Value string `json:"value"`
Source RangeSource `json:"source"`
}
type RangeSource ¶
type RangeSource string
RangeSource identifies a range part source. Mirrors Intl.DateTimeFormat range part field "source".
const ( SourceStartRange RangeSource = "startRange" SourceEndRange RangeSource = "endRange" )
type ResolvedOptions ¶
type ResolvedOptions struct {
Locale locale.Locale `json:"locale"`
Calendar string `json:"calendar"`
NumberingSystem string `json:"numberingSystem"`
TimeZone string `json:"timeZone"`
// Nil when ECMA-402 omits hour-cycle properties because no hour field is present.
HourCycle *HourCycle `json:"hourCycle,omitempty"`
// Nil when ECMA-402 omits hour-cycle properties because no hour field is present.
Hour12 *bool `json:"hour12,omitempty"`
// Nil when the weekday component is absent.
Weekday *FieldStyle `json:"weekday,omitempty"`
// Nil when the era component is absent.
Era *FieldStyle `json:"era,omitempty"`
// Nil when the year component is absent.
Year *NumericStyle `json:"year,omitempty"`
// Nil when the month component is absent.
Month *MonthStyle `json:"month,omitempty"`
// Nil when the day component is absent.
Day *NumericStyle `json:"day,omitempty"`
// Nil when the day-period component is absent.
DayPeriod *FieldStyle `json:"dayPeriod,omitempty"`
// Nil when the hour component is absent.
Hour *NumericStyle `json:"hour,omitempty"`
// Nil when the minute component is absent.
Minute *NumericStyle `json:"minute,omitempty"`
// Nil when the second component is absent.
Second *NumericStyle `json:"second,omitempty"`
// Nil when the fractional-second component is absent.
FractionalSecondDigits *int `json:"fractionalSecondDigits,omitempty"`
// Nil when the time-zone name component is absent.
TimeZoneName *TimeZoneName `json:"timeZoneName,omitempty"`
// Nil when dateStyle was not used.
DateStyle *Style `json:"dateStyle,omitempty"`
// Nil when timeStyle was not used.
TimeStyle *Style `json:"timeStyle,omitempty"`
}
type Style ¶
type Style string
Style selects date or time style width. Mirrors Intl.DateTimeFormat options "dateStyle" and "timeStyle".
type TimeZoneName ¶
type TimeZoneName string
TimeZoneName selects time-zone name width. Mirrors Intl.DateTimeFormat option "timeZoneName".