relativetimeformat

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package relativetimeformat implements the ECMA-402 Intl.RelativeTimeFormat constructor.

locales, _ := locale.ParseList("en-US")
format, _ := relativetimeformat.New(locales, relativetimeformat.Options{})
out, _ := format.Format(relativetimeformat.Int(-1), relativetimeformat.Day)
_ = out

See README.md for usage examples and SPECS/42-relativetimeformat.md for the contract.

Example

Example demonstrates Intl.RelativeTimeFormat.prototype.format from ECMA-402.

package main

import (
	"fmt"

	"github.com/agentable/go-intl/locale"
	"github.com/agentable/go-intl/relativetimeformat"
)

func main() {
	format, err := relativetimeformat.New(mustLocaleList("en-US"), relativetimeformat.Options{})
	if err != nil {
		panic(err)
	}

	out, err := format.Format(relativetimeformat.Int(3), relativetimeformat.Day)
	if err != nil {
		panic(err)
	}
	fmt.Println(out)

}

func mustLocaleList(tags ...string) locale.List {
	locales, err := locale.ParseList(tags...)
	if err != nil {
		panic(err)
	}
	return locales
}
Output:
in 3 days
Example (Options)

Example_options demonstrates Intl.RelativeTimeFormat constructor options from ECMA-402.

package main

import (
	"fmt"

	"github.com/agentable/go-intl/locale"
	"github.com/agentable/go-intl/relativetimeformat"
)

func main() {
	format, err := relativetimeformat.New(mustLocaleList("en-US"), relativetimeformat.Options{
		Numeric: relativetimeformat.NumericAuto,
	})
	if err != nil {
		panic(err)
	}

	out, err := format.Format(relativetimeformat.Int(-1), relativetimeformat.Day)
	if err != nil {
		panic(err)
	}
	fmt.Println(out)

}

func mustLocaleList(tags ...string) locale.List {
	locales, err := locale.ParseList(tags...)
	if err != nil {
		panic(err)
	}
	return locales
}
Output:
yesterday

Index

Examples

Constants

View Source
const (
	LookupLocaleMatcher  LocaleMatcher = "lookup"
	BestFitLocaleMatcher LocaleMatcher = "best fit"

	LongStyle   Style = "long"
	ShortStyle  Style = "short"
	NarrowStyle Style = "narrow"

	NumericAlways Numeric = "always"
	NumericAuto   Numeric = "auto"

	Second  Unit = "second"
	Minute  Unit = "minute"
	Hour    Unit = "hour"
	Day     Unit = "day"
	Week    Unit = "week"
	Month   Unit = "month"
	Quarter Unit = "quarter"
	Year    Unit = "year"

	// PartType values emitted by Format/FormatToParts.
	// ECMA-402 §17.5.6 PartitionRelativeTimePattern produces literal parts
	// for the pattern, and embeds NumberFormat partition records for the value.
	// RelativeTimeFormat's embedded NumberFormat uses Style="decimal" with no
	// Notation, so only the number-style-neutral part types can appear.
	PartLiteral   PartType = "literal"
	PartInteger   PartType = "integer"
	PartGroup     PartType = "group"
	PartDecimal   PartType = "decimal"
	PartFraction  PartType = "fraction"
	PartPlusSign  PartType = "plusSign"
	PartMinusSign PartType = "minusSign"
	PartInfinity  PartType = "infinity"
	PartNaN       PartType = "nan"
)

Variables

This section is empty.

Functions

func SupportedLocalesOf

func SupportedLocalesOf(locales locale.List, opts Options) (locale.List, error)

Types

type LocaleMatcher

type LocaleMatcher string

type Numeric

type Numeric string

type Options

type Options struct {
	LocaleMatcher   LocaleMatcher
	NumberingSystem string
	Style           Style
	Numeric         Numeric
}

type Part

type Part struct {
	Type  PartType `json:"type"`
	Value string   `json:"value"`
	Unit  Unit     `json:"unit,omitempty"`
}

type PartType

type PartType string

type RelativeTimeFormat

type RelativeTimeFormat struct {
	// contains filtered or unexported fields
}

func New

func New(locales locale.List, opts Options) (*RelativeTimeFormat, error)

func (*RelativeTimeFormat) Format added in v0.2.8

func (f *RelativeTimeFormat) Format(value Value, unit Unit) (string, error)

func (*RelativeTimeFormat) FormatToParts added in v0.2.8

func (f *RelativeTimeFormat) FormatToParts(value Value, unit Unit) ([]Part, error)
Example

ExampleRelativeTimeFormat_FormatToParts demonstrates Intl.RelativeTimeFormat.prototype.formatToParts from ECMA-402.

package main

import (
	"fmt"

	"github.com/agentable/go-intl/locale"
	"github.com/agentable/go-intl/relativetimeformat"
)

func main() {
	format, err := relativetimeformat.New(mustLocaleList("en-US"), relativetimeformat.Options{})
	if err != nil {
		panic(err)
	}

	parts, err := format.FormatToParts(relativetimeformat.Int(-2), relativetimeformat.Day)
	if err != nil {
		panic(err)
	}
	for _, part := range parts {
		fmt.Printf("%s=%q unit=%q\n", part.Type, part.Value, part.Unit)
	}

}

func mustLocaleList(tags ...string) locale.List {
	locales, err := locale.ParseList(tags...)
	if err != nil {
		panic(err)
	}
	return locales
}
Output:
integer="2" unit="day"
literal=" days ago" unit=""

func (*RelativeTimeFormat) ResolvedOptions

func (f *RelativeTimeFormat) ResolvedOptions() ResolvedOptions

type ResolvedOptions

type ResolvedOptions struct {
	Locale          locale.Locale `json:"locale"`
	Style           Style         `json:"style"`
	Numeric         Numeric       `json:"numeric"`
	NumberingSystem string        `json:"numberingSystem"`
}

type Style

type Style string

type Unit

type Unit string

type Value added in v0.2.8

type Value struct {
	// contains filtered or unexported fields
}

Value is an opaque ECMA-402 numeric input for RelativeTimeFormat methods.

func Decimal added in v0.2.8

func Decimal(value string) (Value, error)

Decimal parses a finite ECMA-402 decimal-string bridge value.

func Float added in v0.2.8

func Float(value float64) Value

Float returns a float64 relative-time value.

func Int added in v0.2.8

func Int(value int64) Value

Int returns a signed integer relative-time value.

func Uint added in v0.2.8

func Uint(value uint64) Value

Uint returns an unsigned integer relative-time value.

Jump to

Keyboard shortcuts

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