answer

package
v0.3.22 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 16 Imported by: 1 module, 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 answer parses and evaluates the launcher's inline answers beyond arithmetic: currency conversions over the ECB's reference rates, times across time zones, and calendar date arithmetic.

Index

Constants

View Source
const FeedURL = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"

FeedURL is the ECB's daily euro foreign exchange reference rates.

Variables

This section is empty.

Functions

func FormatAmount

func FormatAmount(v *big.Rat) string

FormatAmount writes an amount with two decimals, or with as many as it takes (up to six) to show a non-zero amount below a cent.

Types

type Currency

type Currency uint8

Currency is one of the currencies the ECB publishes a reference rate for, plus the euro the rates are quoted against.

const (
	EUR Currency = iota
	USD
	JPY
	CZK
	DKK
	GBP
	HUF
	PLN
	RON
	SEK
	CHF
	ISK
	NOK
	TRY
	AUD
	BRL
	CAD
	CNY
	HKD
	IDR
	ILS
	INR
	KRW
	MXN
	MYR
	NZD
	PHP
	SGD
	THB
	ZAR
)

The currencies, in the ECB feed's order after the euro.

func (Currency) String

func (c Currency) String() string

String is the ISO 4217 code.

type CurrencyParser

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

CurrencyParser reads currency conversions. Kroner is what "kr" names in the session's language (DKK in Danish); outside the Nordic languages "kr" is ambiguous and names nothing.

func NewCurrencyParser

func NewCurrencyParser(language string) CurrencyParser

NewCurrencyParser reads "kr" as the kroner of language, an ISO 639 code.

func (CurrencyParser) Parse

func (p CurrencyParser) Parse(query string) (CurrencyQuery, bool)

Parse reads "<amount> <currency> in <currency>", the source currency before or after the amount ("$100 to €", "100 dollars in euros").

type CurrencyQuery

type CurrencyQuery struct {
	Amount   string
	From, To Currency
}

CurrencyQuery is a parsed "100 usd in eur": Amount is the decimal text as typed.

type DateAnswer

type DateAnswer struct {
	Kind DateKind
	Day  time.Time
	Days int
}

DateAnswer is an evaluated date query: a calendar day (Day, midnight UTC) or a number of days.

func EvalDate

func EvalDate(query string, now time.Time) (DateAnswer, bool)

EvalDate answers date arithmetic in now's calendar: "today + 90 days", "2026-12-24 - today", "days until 2027-01-01", "days since 2026-01-01", "3 weeks from tomorrow", "10 days ago".

type DateKind

type DateKind uint8

DateKind tells a date answer from a count of days.

const (
	DateIsDay DateKind = iota + 1
	DateIsSpan
)

The kinds of date answer.

type RateTable

type RateTable struct {
	// Date is the reference day, midnight UTC.
	Date time.Time
	// contains filtered or unexported fields
}

RateTable is one day of reference rates, each currency's units per euro.

func ParseECB

func ParseECB(data []byte) (*RateTable, error)

ParseECB reads the ECB's eurofxref XML. Currencies it does not know are left out; a missing day or a rate that is not a positive decimal fails.

func (*RateTable) Convert

func (t *RateTable) Convert(amount string, from, to Currency) (*big.Rat, bool)

Convert turns amount, a plain decimal, from one currency into another, exactly; false when the table has no rate for either.

type Rates

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

Rates keeps the reference rates: read from the cache file, fetched from the feed at most once per calendar day, retried an hour after a failure. Snapshot never waits; Refresh does the I/O on its caller's goroutine.

func NewRates

func NewRates(feed, cache string, now func() time.Time) *Rates

NewRates reads feed, caching it at cache.

func (*Rates) Refresh

func (r *Rates) Refresh(ctx context.Context) bool

Refresh reads the cache on first use and fetches the feed when due; true when the rates changed. A refresh already running elsewhere makes it return at once.

func (*Rates) Snapshot

func (r *Rates) Snapshot() (Snapshot, bool)

Snapshot is the current rates, false before any are known.

type Snapshot

type Snapshot struct {
	Table     *RateTable
	FetchedAt time.Time
}

Snapshot is the rates in use and when they were fetched.

func (Snapshot) AgeDays

func (s Snapshot) AgeDays(now time.Time) int

AgeDays is how many calendar days the reference day lies before now's.

func (Snapshot) Fresh

func (s Snapshot) Fresh(now time.Time) bool

Fresh reports whether the rates were fetched on now's calendar day.

type TimeAnswer

type TimeAnswer struct {
	At        time.Time
	Place     string
	DayOffset int
}

TimeAnswer is an evaluated time query: At in the target zone, named Place ("" for the local zone), DayOffset days after the source's day.

type TimeQuery

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

TimeQuery is a parsed "15:00 PST in Copenhagen" or "time in Tokyo".

func ParseTime

func ParseTime(query string) (TimeQuery, bool)

ParseTime reads "<time> [zone] in <zone>" and "time in <zone>". A time is "15:00", "3pm" or "3:30 pm"; a zone is an abbreviation, a city or an IANA name. A time without a zone is local.

type Zones

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

Zones loads IANA zones once each.

func NewZones

func NewZones(load func(string) (*time.Location, error)) *Zones

NewZones loads zones with load, time.LoadLocation in the shell.

func (*Zones) Eval

func (z *Zones) Eval(q TimeQuery, now time.Time) (TimeAnswer, bool)

Eval places the query's time, on today's date in its source zone, in its target zone; now's location is the local zone. False when a zone does not load.

Jump to

Keyboard shortcuts

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