relativetimeformat

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

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

format, _ := relativetimeformat.New(locale.MustParseList("en-US"), relativetimeformat.Options{})
out, _ := format.FormatInt(-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(locale.MustParseList("en-US"), relativetimeformat.Options{})
	if err != nil {
		panic(err)
	}

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

}
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(locale.MustParseList("en-US"), relativetimeformat.Options{
		Numeric: relativetimeformat.NumericAuto,
	})
	if err != nil {
		panic(err)
	}

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

}
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"
	Seconds  Unit = "seconds"
	Minute   Unit = "minute"
	Minutes  Unit = "minutes"
	Hour     Unit = "hour"
	Hours    Unit = "hours"
	Day      Unit = "day"
	Days     Unit = "days"
	Week     Unit = "week"
	Weeks    Unit = "weeks"
	Month    Unit = "month"
	Months   Unit = "months"
	Quarter  Unit = "quarter"
	Quarters Unit = "quarters"
	Year     Unit = "year"
	Years    Unit = "years"

	// 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) FormatDecimal

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

func (*RelativeTimeFormat) FormatDecimalToParts

func (f *RelativeTimeFormat) FormatDecimalToParts(value string, unit Unit) ([]Part, error)

func (*RelativeTimeFormat) FormatFloat64

func (f *RelativeTimeFormat) FormatFloat64(value float64, unit Unit) (string, error)

func (*RelativeTimeFormat) FormatFloat64ToParts

func (f *RelativeTimeFormat) FormatFloat64ToParts(value float64, unit Unit) ([]Part, error)

func (*RelativeTimeFormat) FormatInt

func (f *RelativeTimeFormat) FormatInt(value int, unit Unit) (string, error)

func (*RelativeTimeFormat) FormatInt64

func (f *RelativeTimeFormat) FormatInt64(value int64, unit Unit) (string, error)

func (*RelativeTimeFormat) FormatInt64ToParts

func (f *RelativeTimeFormat) FormatInt64ToParts(value int64, unit Unit) ([]Part, error)

func (*RelativeTimeFormat) FormatIntToParts

func (f *RelativeTimeFormat) FormatIntToParts(value int, unit Unit) ([]Part, error)
Example

ExampleRelativeTimeFormat_FormatIntToParts 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(locale.MustParseList("en-US"), relativetimeformat.Options{})
	if err != nil {
		panic(err)
	}

	parts, err := format.FormatIntToParts(-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)
	}

}
Output:
integer="2" unit="day"
literal=" days ago" unit=""

func (*RelativeTimeFormat) FormatUint

func (f *RelativeTimeFormat) FormatUint(value uint, unit Unit) (string, error)

func (*RelativeTimeFormat) FormatUint64

func (f *RelativeTimeFormat) FormatUint64(value uint64, unit Unit) (string, error)

func (*RelativeTimeFormat) FormatUint64ToParts

func (f *RelativeTimeFormat) FormatUint64ToParts(value uint64, unit Unit) ([]Part, error)

func (*RelativeTimeFormat) FormatUintToParts

func (f *RelativeTimeFormat) FormatUintToParts(value uint, unit Unit) ([]Part, error)

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

Jump to

Keyboard shortcuts

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