fx

package
v0.1.1228 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package fx keeps the dollar-to-yuan rate the CNY currency preference (see internal/settings) converts list-price costs with. It asks a free, no-key endpoint for it, keeps what it got for TTL in a small cache file beside magpie's other caches, and falls back to the last rate it ever had — or a fixed default, when it has never reached the network — while offline. Nothing here changes what usage.Record and its Totals store: they stay in USD, and only what's shown is converted.

Index

Constants

View Source
const Fallback = 7.2

Fallback is the rate shown when magpie has never reached rateURL and has no cache of its own: roughly what a dollar has been worth in yuan, fixed so a first run offline still shows something plausible rather than nothing.

View Source
const TTL = 12 * time.Hour

TTL is how long a fetched rate is used before another is asked for.

Variables

This section is empty.

Functions

func CachePath

func CachePath() string

CachePath is where the rate is kept between runs.

func Reset

func Reset()

Reset forgets the in-memory rate, so the next Get rereads the cache file or asks rateURL again; tests use it between cases.

func SetRateURL added in v0.1.652

func SetRateURL(u string) (restore func())

SetRateURL points the rate at u until the returned func puts it back; for other packages' tests.

func Settle added in v0.1.652

func Settle()

Settle waits for a fetch Soon started to end; for tests, whose home goes once they do.

Types

type Rate

type Rate struct {
	CNYPerUSD float64   `json:"rate"`
	At        time.Time `json:"at"`
}

Rate is a USD→CNY snapshot: how much one dollar is worth right now (CNYPerUSD) and when that was learned (At; the zero time for Fallback, which was never learned from anywhere).

func Get

func Get(ctx context.Context) Rate

Get is the rate to show right now: what this process (or, failing that, the cache file) last learned, if it's under TTL old; else a fresh fetch of rateURL, kept in the cache for next time; else — offline, or the endpoint's reply didn't parse — the last rate however old, or Fallback when there has never been one.

func Soon added in v0.1.652

func Soon() Rate

Soon is Get without the wait, for a page that must not stand on the network (#541): the rate known now — this process's, the cache file's, else Fallback — and, when that is stale, Get behind it (one at a time), for the next look to have.

func (Rate) Stale

func (r Rate) Stale() bool

Stale is true once a rate is older than TTL — shown on hover so "using yesterday's rate, offline" reads differently from "just fetched".

Jump to

Keyboard shortcuts

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