Help us shape the future of Go! Take the 2026 Go Developer Survey

strftime

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package strftime parses and formats instants with strftime-style formats, the ones Polars, chrono, Python and C share: "%Y-%m-%d %H:%M:%S".

Why not Go's layouts

time.Parse reads a layout by example — "2006-01-02" — so translating a strftime format into one would hand every literal in the format to Go's matcher too: a literal "1" in "Q1 %Y" reads as a month, and "Mon" as a weekday. A format here is compiled into directives and literals, and only the directives are fields.

What it reads

%Y year, four digits, or signed past 0000–9999 (+10000)
%m month 01–12     %d day 01–31     %e day, space-padded
%y year 00–99: 00–69 are 2000–2069 and 70–99 are 1970–1999, as chrono reads them
%j day of year 001–366
%H hour 00–23   %I hour 01–12   %p AM/PM   %M minute 00–59   %S second 00–59
%f fraction, 1–9 digits as written   %.f the same after a dot, optional
%.3f %.6f %.9f a dot and exactly 3, 6, 9 digits   %3f %6f %9f the same, no dot
%b %h month abbreviation   %B month name   %a weekday abbreviation   %A weekday
%z offset +hhmm, read with or without the colon   %:z offset +hh:mm
%Z zone name (formatting only)
%T %H:%M:%S   %D %m/%d/%y   %F %Y-%m-%d   %R %H:%M   %% a percent sign

Names are English. Parsing is strict about shape: a number takes at most its width, a date must exist (no 30 February), and the whole input must be consumed. A weekday name is read and not checked against the date.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fields

type Fields struct {
	Year, Month, Day          int
	Hour, Minute, Second      int
	Nanos                     int
	Offset                    int
	HasDate, HasTime, HasZone bool
}

Fields is what a parse found. Year, Month and Day are a calendar date when HasDate is set; Offset is seconds east of UTC when HasZone is.

type Format

type Format struct {

	// What the directives can supply, for the callers that refuse a format that
	// cannot say what their type needs. HasDate is any part of a date, which is
	// what a Time cannot format; ParsesDate is a whole one — a year with a month
	// and a day, or with %j — which is what Parse needs to produce a date at all.
	HasDate, HasTime, HasZone bool
	ParsesDate                bool
	// HasZoneName is %Z, which is formatted and cannot be read back.
	HasZoneName bool
	// contains filtered or unexported fields
}

Format is a compiled format.

func Compile

func Compile(src string) (*Format, error)

Compile parses a format.

func (*Format) Append

func (f *Format) Append(dst []byte, t time.Time, zone bool) ([]byte, error)

Append formats t, in its own location, onto dst. zone reports whether t's zone is meaningful: a naive instant has none, and %z and %Z refuse to invent one.

func (*Format) Parse

func (f *Format) Parse(s string) (Fields, error)

Parse reads s with f. It reports false, with the reason, when s does not match the format or names a date or time that does not exist.

func (*Format) String

func (f *Format) String() string

String is the source format.

Jump to

Keyboard shortcuts

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