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. 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 ¶
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
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 ¶
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 Option ¶
type Option interface { Name() string Value() interface{} }
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
WithSpecification 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.