Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Format ¶
Format takes the format `s` and the time `t` to produce the format date/time.
When called without options, compiled patterns are cached (up to an internal limit) so that repeated calls with the same pattern avoid recompilation. Calls that pass options always compile on the fly.
If you know beforehand that you will be reusing the pattern within your application, consider creating a `Strftime` object and reusing it.
Types ¶
type AppendFunc ¶
AppendFunc is an utility type to allow users to create a function-only version of an Appender
type Appender ¶
Appender is the interface that must be fulfilled by components that implement the translation of specifications to actual time value.
The Append method takes the accumulated byte buffer, and the time to use to generate the textual representation. The resulting byte sequence must be returned by this method, normally by using the append() builtin function.
func Microseconds ¶ added in v1.0.2
func Microseconds() Appender
Microseconds returns the Appender suitable for creating a zero-padded, 6-digit microsecond textual representation.
func Milliseconds ¶
func Milliseconds() Appender
Milliseconds returns the Appender suitable for creating a zero-padded, 3-digit millisecond textual representation.
func StdlibFormat ¶
StdlibFormat returns an Appender that simply goes through `time.Format()` For example, if you know you want to display the abbreviated month name for %b, you can create a StdlibFormat with the pattern `Jan` and register that for specification `b`:
a := StdlibFormat(`Jan`) ss := NewSpecificationSet() ss.Set('b', a) // does %b -> abbreviated month name
func UnixSeconds ¶ added in v1.0.2
func UnixSeconds() Appender
UnixSeconds returns the Appender suitable for creating unix timestamp textual representation.
type Locale ¶ added in v1.2.0
type Locale interface {
Month(time.Month) string // full month name (%B)
ShortMonth(time.Month) string // abbreviated month name (%b, %h)
Weekday(time.Weekday) string // full weekday name (%A)
ShortWeekday(time.Weekday) string // abbreviated weekday name (%a)
Meridiem(hour int) string // AM/PM marker for the given 0-23 hour (%p)
}
Locale supplies the locale-dependent strings used by the name-producing conversion specifiers (%A, %a, %B, %b, %h, %p). The library ships no locale data of its own: build a Locale for your language with NewLocale, or implement this interface yourself to back it with any source (a map, computed values, an external CLDR dataset, ...).
Months are addressed by time.Month and weekdays by time.Weekday, so an implementation never has to worry about index conventions.
Some languages (Russian, Czech, Polish, Greek, ...) inflect a month name depending on whether it stands alone or sits next to a day number — e.g. Russian "январь" (stand-alone) vs "2 января" (in a date). A Locale returns a single form per month, so format each grammatical context with its own Strftime object, each compiled with the Locale that holds the matching form.
func DefaultLocale ¶ added in v1.2.0
func DefaultLocale() Locale
DefaultLocale returns the English locale. It is the fallback for any name a custom Locale leaves unset, and a convenient base for NewLocale.
func NewLocale ¶ added in v1.2.0
func NewLocale(options ...LocaleOption) Locale
NewLocale creates a Locale from the supplied options, starting from the English locale. Any name an option leaves as the empty string keeps its English default, so a partially-specified Locale never produces blank output.
type LocaleOption ¶ added in v1.2.0
type LocaleOption interface {
// contains filtered or unexported methods
}
LocaleOption configures a Locale built by NewLocale.
func WithMeridiem ¶ added in v1.2.0
func WithMeridiem(am, pm string) LocaleOption
WithMeridiem sets the AM/PM markers (%p). An empty string keeps the English default for that marker.
func WithMonths ¶ added in v1.2.0
func WithMonths(names MonthNames) LocaleOption
WithMonths sets the full month names (%B), indexed by time.Month minus one.
func WithShortMonths ¶ added in v1.2.0
func WithShortMonths(names MonthNames) LocaleOption
WithShortMonths sets the abbreviated month names (%b, %h).
func WithShortWeekdays ¶ added in v1.2.0
func WithShortWeekdays(names WeekdayNames) LocaleOption
WithShortWeekdays sets the abbreviated weekday names (%a).
func WithWeekdays ¶ added in v1.2.0
func WithWeekdays(names WeekdayNames) LocaleOption
WithWeekdays sets the full weekday names (%A), indexed by time.Weekday.
type MonthNames ¶ added in v1.2.0
type MonthNames [12]string
MonthNames holds twelve month names, indexed by time.Month minus one (January is index 0, December is index 11). It is used as the argument to WithMonths and WithShortMonths.
type Option ¶
func WithLocale ¶ added in v1.2.0
WithLocale overrides the name-producing conversion specifiers (%A, %a, %B, %b, %h, %p) with the localized names supplied by loc, which is typically built with NewLocale. Numeric specifiers (%d, %m, %Y, ...) are locale-invariant and are unaffected.
Because a Locale supplies one form per name, format a context that needs inflected month names (e.g. "%d %B" vs "%B %Y" in Slavic languages) by compiling a separate Strftime object per context, each with the Locale holding the appropriate form. See the Locale documentation for details.
func WithMicroseconds ¶ added in v1.0.2
WithMicroseconds is similar to WithSpecification, and specifies that the Strftime object should interpret the pattern `%b` (where b is the byte that you specify as the argument) as the zero-padded, 3 letter microseconds of the time.
func WithMilliseconds ¶
WithMilliseconds is similar to WithSpecification, and specifies that the Strftime object should interpret the pattern `%b` (where b is the byte that you specify as the argument) as the zero-padded, 3 letter milliseconds of the time.
func WithSpecification ¶
WithSpecification allows you to create a new specification set on the fly, to be used only for that invocation.
func WithSpecificationSet ¶
func WithSpecificationSet(ds SpecificationSet) Option
WithSpecificationSet allows you to specify a custom specification set
func WithUnixSeconds ¶ added in v1.0.2
WithUnixSeconds is similar to WithSpecification, and specifies that the Strftime object should interpret the pattern `%b` (where b is the byte that you specify as the argument) as the unix timestamp in seconds
type SpecificationSet ¶
type SpecificationSet interface {
Lookup(byte) (Appender, error)
Delete(byte) error
Set(byte, Appender) error
}
SpecificationSet is a container for patterns that Strftime uses. If you want a custom strftime, you can copy the default SpecificationSet and tweak it
Example ¶
package main
import (
"fmt"
"os"
"time"
"github.com/lestrrat-go/strftime"
)
var ref = time.Unix(1136239445, 123456789).UTC()
func main() {
{
// I want %L as milliseconds!
p, err := strftime.New(`%L`, strftime.WithMilliseconds('L'))
if err != nil {
fmt.Println(err)
return
}
p.Format(os.Stdout, ref)
os.Stdout.Write([]byte{'\n'})
}
{
// I want %f as milliseconds!
p, err := strftime.New(`%f`, strftime.WithMilliseconds('f'))
if err != nil {
fmt.Println(err)
return
}
p.Format(os.Stdout, ref)
os.Stdout.Write([]byte{'\n'})
}
{
// I want %X to print out my name!
a := strftime.Verbatim(`Daisuke Maki`)
p, err := strftime.New(`%X`, strftime.WithSpecification('X', a))
if err != nil {
fmt.Println(err)
return
}
p.Format(os.Stdout, ref)
os.Stdout.Write([]byte{'\n'})
}
{
// I want a completely new specification set, and I want %X to print out my name!
a := strftime.Verbatim(`Daisuke Maki`)
ds := strftime.NewSpecificationSet()
ds.Set('X', a)
p, err := strftime.New(`%X`, strftime.WithSpecificationSet(ds))
if err != nil {
fmt.Println(err)
return
}
p.Format(os.Stdout, ref)
os.Stdout.Write([]byte{'\n'})
}
{
// I want %s as unix timestamp!
p, err := strftime.New(`%s`, strftime.WithUnixSeconds('s'))
if err != nil {
fmt.Println(err)
return
}
p.Format(os.Stdout, ref)
os.Stdout.Write([]byte{'\n'})
}
}
Output: 123 123 Daisuke Maki Daisuke Maki 1136239445
func NewSpecificationSet ¶
func NewSpecificationSet() SpecificationSet
NewSpecificationSet creates a specification set with the default specifications.
type Strftime ¶
type Strftime struct {
// contains filtered or unexported fields
}
Strftime is the object that represents a compiled strftime pattern
func New ¶
New creates a new Strftime object. If the compilation fails, then an error is returned in the second argument.
func (*Strftime) Dump ¶ added in v1.0.1
Dump outputs the internal structure of the formatter, for debugging purposes. Please do NOT assume the output format to be fixed: it is expected to change in the future.
func (*Strftime) Format ¶
Format takes the destination `dst` and time `t`. It formats the date/time using the pre-compiled pattern, and outputs the results to `dst`
func (*Strftime) FormatBuffer ¶ added in v1.0.5
FormatBuffer is equivalent to Format, but appends the result directly to supplied slice dst, returning the updated slice. This avoids any internal memory allocation.
func (*Strftime) FormatString ¶
FormatString takes the time `t` and formats it, returning the string containing the formated data.
type WeekdayNames ¶ added in v1.2.0
type WeekdayNames [7]string
WeekdayNames holds seven weekday names, indexed by time.Weekday (Sunday is index 0, Saturday is index 6). It is used as the argument to WithWeekdays and WithShortWeekdays.