strftime

package module
v0.0.0-...-31724ce Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2021 License: MIT Imports: 8 Imported by: 0

README

strftime

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(p string, t time.Time, options ...Option) (string, error)

Format takes the format `s` and the time `t` to produce the format date/time. Note that this function re-compiles the pattern every time it is called.

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

type AppendFunc func([]byte, time.Time) []byte

AppendFunc is an utility type to allow users to create a function-only version of an Appender

func (AppendFunc) Append

func (af AppendFunc) Append(b []byte, t time.Time) []byte

type Appender

type Appender interface {
	Append([]byte, time.Time) []byte
}

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

func Microseconds() Appender

Microsecond 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

func StdlibFormat(s string) Appender

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

func UnixSeconds() Appender

UnixSeconds returns the Appender suitable for creating unix timestamp textual representation.

func Verbatim

func Verbatim(s string) Appender

Verbatim returns an Appender suitable for generating static text. For static text, this method is slightly favorable than creating your own appender, as adjacent verbatim blocks will be combined at compile time to produce more efficient Appenders

type Option

type Option interface {
	Name() string
	Value() interface{}
}

func WithMicroseconds

func WithMicroseconds(b byte) Option

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

func WithMilliseconds(b byte) Option

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

func WithSpecification(b byte, a Appender) Option

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

WithSpecification allows you to specify a custom specification set

func WithUnixSeconds

func WithUnixSeconds(b byte) Option

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

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

func New(p string, options ...Option) (*Strftime, error)

New creates a new Strftime object. If the compilation fails, then an error is returned in the second argument.

func (*Strftime) Dump

func (f *Strftime) Dump(out io.Writer)

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

func (f *Strftime) Format(dst io.Writer, t time.Time) error

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

func (f *Strftime) FormatBuffer(dst []byte, t time.Time) []byte

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

func (f *Strftime) FormatString(t time.Time) string

FormatString takes the time `t` and formats it, returning the string containing the formated data.

func (*Strftime) Pattern

func (f *Strftime) Pattern() string

Pattern returns the original pattern string

Jump to

Keyboard shortcuts

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