format

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 9 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 format formats numbers and times with the syntax of d3-format and d3-time-format, which Vega uses for its axes, its legends and the functions format and timeFormat of its expressions. zuzu follows the documented behavior and is not a port (D6). The locale is US English by default (D27), and a Locale holds another.

Index

Constants

This section is empty.

Variables

View Source
var EnUS = Locale{
	Decimal:     ".",
	Thousands:   ",",
	Grouping:    []int{3},
	Currency:    [2]string{"$", ""},
	Minus:       "−",
	Percent:     "%",
	NaN:         "NaN",
	DateTime:    "%x, %X",
	Date:        "%-m/%-d/%Y",
	Time:        "%-I:%M:%S %p",
	Periods:     [2]string{"AM", "PM"},
	Days:        [7]string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
	ShortDays:   [7]string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"},
	Months:      [12]string{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"},
	ShortMonths: [12]string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"},
}

EnUS is the locale of US English, the default of d3 and of zuzu (D27).

Intervals are the intervals, from the longest to the shortest.

View Source
var UnitSpecifiers = map[string]string{
	"year":            "%Y ",
	"quarter":         "Q%q ",
	"month":           "%b ",
	"date":            "%d ",
	"week":            "W%U ",
	"day":             "%a ",
	"dayofyear":       "%j ",
	"hours":           "%H:00",
	"minutes":         "00:%M",
	"seconds":         ":%S",
	"milliseconds":    ".%L",
	"year-month":      "%Y-%m ",
	"year-month-date": "%Y-%m-%d ",
	"hours-minutes":   "%H:%M",
}

UnitSpecifiers are the default parts of the format spec that TimeUnitSpecifier joins, as Vega documents them for its function timeUnitSpecifier.

Functions

func Number

func Number(spec string, loc Locale) (func(float64) string, error)

Number returns a function that formats a number with a format spec of d3-format, such as ",.2f" or "~s", in a locale.

func Parse

func Parse(spec string, loc Locale, zone *time.Location) func(string) (time.Time, bool)

Parse returns a function that parses a time with a format spec of d3-time-format. The function reads the time in zone, unless the text gives an offset with %Z. It reports false when the text does not match the spec, or has text after it. A field that the text does not give is the start of its range: January 1, 1900, at midnight. A field out of its range carries into the next, so the month 13 of a year is January of the next year.

func ParseDate

func ParseDate(s string, zone *time.Location) (time.Time, bool)

ParseDate parses a date the way the Date of JavaScript parses a string, for the date formats that data and expressions give without a format spec.

The ISO form, such as 2024-02-29 or 2024-02-29T12:30:05.007+05:30, is first. A date with no time is in UTC, and a time with no offset is in zone. Other forms, such as 2024/02/29, 02/29/2024, Feb 29 2024 and February 29, 2024 1:30 PM, are in zone unless they give an offset. In them a year of 0 to 99 is in 1950 to 2049, and a number alone is a date too: 0 is 2000-01-01, 5 is 2001-05-01 and 32 is 2032-01-01. A time more than 100,000,000 days from 1970-01-01 is not a date, as in JavaScript.

func PrecisionFixed added in v0.7.2

func PrecisionFixed(step float64) int

PrecisionFixed returns the number of decimals that fixed notation needs to tell apart two values that are step apart, such as 1 for 0.5 and 2 for 0.01, as precisionFixed of d3-format. It returns -1 for a step that is not a number.

func PrecisionPrefix added in v0.7.2

func PrecisionPrefix(step, value float64) int

PrecisionPrefix returns the number of decimals that the notation of an SI prefix needs to tell apart two values that are step apart, where the prefix is the one of value, such as 1 for a step of 100000 and a value of 1300000 (1.3M), as precisionPrefix of d3-format. It returns -1 for a step or a value that is not a number.

func PrecisionRound added in v0.7.2

func PrecisionRound(step, largest float64) int

PrecisionRound returns the number of significant digits that tell apart two values that are step apart, up to the largest value, such as 3 for a step of 0.01 and a largest value of 1.01, as precisionRound of d3-format. It returns -1 for a step or a value that is not a number.

func Prefix added in v0.7.2

func Prefix(spec string, value float64, loc Locale) (func(float64) string, error)

Prefix returns a function that formats a number in fixed notation, in the unit of the SI prefix of value, with that prefix after it, such as 1.5M for 1500000 when value is 4000000. Every number takes the same prefix, as formatPrefix of d3-format does. The type of the spec is ignored.

func Ticks added in v0.7.2

func Ticks(spec string, lo, hi, step float64, loc Locale) (func(float64) string, error)

Ticks returns the function that writes the labels of the ticks of a scale of numbers from lo to hi, step apart, with a spec of d3-format, as tickFormat of d3-scale does. The spec ",f" is the default. If the spec gives no precision, the precision is the one that tells two ticks apart. The type s writes each label with the SI prefix of the largest end, such as 0M, 1M and 2M.

func Time

func Time(spec string, loc Locale) func(time.Time) string

Time returns a function that formats a time with a format spec of d3-time-format, such as "%b %d, %Y", in a locale. The function writes the time in its own location: pass t.UTC() for the UTC formats, and t.In(zone) for the local ones.

A directive is a % and a letter. A -, _ or 0 between them pads the number with nothing, spaces or zeros. A letter that is not a directive is written as it is, without the %.

func TimeUnitSpecifier

func TimeUnitSpecifier(units []string, specs map[string]string) string

TimeUnitSpecifier returns a format spec of d3-time-format for a set of time units, such as year and month. A part in specs takes the place of the default part of the same name in UnitSpecifiers. The parts are joined from the longest unit to the shortest. Year, month and date join as year-month-date, year and month as year-month, and hours and minutes as hours-minutes.

func WallTime

func WallTime(year, month, day, hour, minute, sec, ms int, loc *time.Location) time.Time

WallTime returns the time that a clock in loc shows as the given fields, as the Date of JavaScript builds it. A field out of its range carries into the next. A time that the clock skips, when it moves forward, reads with the offset from before the move, so 2:30 on a day that skips from 2:00 to 3:00 is 3:30. A time that the clock shows twice, when it moves back, is the first of the two.

Types

type Error

type Error string

Error is an error of the package format.

const ErrInvalidSpec Error = "invalid format spec"

ErrInvalidSpec is the error of a format spec that does not parse.

func (Error) Error

func (err Error) Error() string

Error satisfies the error interface.

type Interval

type Interval string

Interval is a unit of time, such as a month or an hour, in the location of the times it takes. It has the names of the time units of Vega: year, quarter, month, week, date, day, dayofyear, hours, minutes, seconds and milliseconds. date, day and dayofyear are the same interval: a day. week starts on Sunday.

const (
	Year         Interval = "year"
	Quarter      Interval = "quarter"
	Month        Interval = "month"
	Week         Interval = "week"
	Date         Interval = "date"
	Day          Interval = "day"
	DayOfYear    Interval = "dayofyear"
	Hours        Interval = "hours"
	Minutes      Interval = "minutes"
	Seconds      Interval = "seconds"
	Milliseconds Interval = "milliseconds"
)

The intervals.

func (Interval) Ceil

func (i Interval) Ceil(t time.Time) time.Time

Ceil returns the start of the first interval that starts at t or after.

func (Interval) Floor

func (i Interval) Floor(t time.Time) time.Time

Floor returns the start of the interval that holds t.

func (Interval) Offset

func (i Interval) Offset(t time.Time, n int) time.Time

Offset returns t moved by n intervals. A month, a quarter or a year keeps the day of the month, and a day past the end of the month carries into the next. A day or a week keeps the time of day.

func (Interval) Range

func (i Interval) Range(start, stop time.Time, step int) []time.Time

Range returns the starts of intervals from start, inclusive, to stop, exclusive, with every step-th interval. A step below 1 is 1.

func (Interval) Valid

func (i Interval) Valid() bool

Valid reports whether i is an interval.

type Locale

type Locale struct {
	Decimal     string
	Thousands   string
	Grouping    []int
	Currency    [2]string
	Minus       string
	Percent     string
	NaN         string
	DateTime    string
	Date        string
	Time        string
	Periods     [2]string
	Days        [7]string
	ShortDays   [7]string
	Months      [12]string
	ShortMonths [12]string
}

Locale holds the symbols of numbers and the names of times of a language.

type Spec

type Spec struct {
	Fill      string
	Align     string
	Sign      string
	Symbol    string
	Zero      bool
	Width     int
	Comma     bool
	Precision int // -1 when the spec gives none
	Trim      bool
	Type      string
}

Spec is a parsed format spec of d3-format.

func ParseSpec

func ParseSpec(s string) (Spec, error)

ParseSpec parses a format spec of d3-format.

Jump to

Keyboard shortcuts

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