8 "github.com/pkg/errors"
11 var directives = map[byte]appender{
12 'A': timefmt("Monday"),
14 'B': timefmt("January"),
17 'c': timefmt("Mon Jan _2 15:04:05 2006"),
18 'D': timefmt("01/02/06"),
21 'F': timefmt("2006-01-02"),
23 'h': timefmt("Jan"), // same as 'b'
26 'k': hourwblank(false),
27 'l': hourwblank(true),
32 'R': timefmt("15:04"),
33 'r': timefmt("3:04:05 PM"),
35 'T': timefmt("15:04:05"),
37 'U': weeknumberOffset(0), // week number of the year, Sunday first
40 'v': timefmt("_2-Jan-2006"),
41 'W': weeknumberOffset(1), // week number of the year, Monday first
43 'X': timefmt("15:04:05"), // national representation of the time XXX is this correct?
44 'x': timefmt("01/02/06"), // national representation of the date XXX is this correct?
45 'Y': timefmt("2006"), // year with century
46 'y': timefmt("06"), // year w/o century
47 'Z': timefmt("MST"), // time zone name
48 'z': timefmt("-0700"), // time zone ofset from UTC
52 type combiningAppend struct {
58 func (ca *combiningAppend) Append(w appender) {
59 if ca.prevCanCombine {
60 if wc, ok := w.(combiner); ok && wc.canCombine() {
61 ca.prev = ca.prev.(combiner).combine(wc)
62 ca.list[len(ca.list)-1] = ca.prev
67 ca.list = append(ca.list, w)
69 ca.prevCanCombine = false
70 if comb, ok := w.(combiner); ok {
71 if comb.canCombine() {
72 ca.prevCanCombine = true
77 func compile(wl *appenderList, p string) error {
78 var ca combiningAppend
79 for l := len(p); l > 0; l = len(p) {
80 i := strings.IndexByte(p, '%')
82 ca.Append(verbatim(p))
83 // this is silly, but I don't trust break keywords when there's a
84 // possibility of this piece of code being rearranged
89 return errors.New(`stray % at the end of pattern`)
92 // we found a '%'. we need the next byte to decide what to do next
93 // we already know that i < l - 1
94 // everything up to the i is verbatim
96 ca.Append(verbatim(p[:i]))
100 directive, ok := directives[p[1]]
102 return errors.Errorf(`unknown time format specification '%c'`, p[1])
113 // Format takes the format `s` and the time `t` to produce the
114 // format date/time. Note that this function re-compiles the
115 // pattern every time it is called.
117 // If you know beforehand that you will be reusing the pattern
118 // within your application, consider creating a `Strftime` object
120 func Format(p string, t time.Time) (string, error) {
122 // TODO: optimize for 64 byte strings
123 dst = make([]byte, 0, len(p)+10)
124 // Compile, but execute as we go
125 for l := len(p); l > 0; l = len(p) {
126 i := strings.IndexByte(p, '%')
128 dst = append(dst, p...)
129 // this is silly, but I don't trust break keywords when there's a
130 // possibility of this piece of code being rearranged
135 return "", errors.New(`stray % at the end of pattern`)
138 // we found a '%'. we need the next byte to decide what to do next
139 // we already know that i < l - 1
140 // everything up to the i is verbatim
142 dst = append(dst, p[:i]...)
146 directive, ok := directives[p[1]]
148 return "", errors.Errorf(`unknown time format specification '%c'`, p[1])
150 dst = directive.Append(dst, t)
154 return string(dst), nil
157 // Strftime is the object that represents a compiled strftime pattern
158 type Strftime struct {
160 compiled appenderList
163 // New creates a new Strftime object. If the compilation fails, then
164 // an error is returned in the second argument.
165 func New(f string) (*Strftime, error) {
167 if err := compile(&wl, f); err != nil {
168 return nil, errors.Wrap(err, `failed to compile format`)
176 // Pattern returns the original pattern string
177 func (f *Strftime) Pattern() string {
181 // Format takes the destination `dst` and time `t`. It formats the date/time
182 // using the pre-compiled pattern, and outputs the results to `dst`
183 func (f *Strftime) Format(dst io.Writer, t time.Time) error {
186 max := len(f.pattern) + 10
188 var buf [bufSize]byte
191 b = make([]byte, 0, max)
193 if _, err := dst.Write(f.format(b, t)); err != nil {
199 func (f *Strftime) format(b []byte, t time.Time) []byte {
200 for _, w := range f.compiled {
206 // FormatString takes the time `t` and formats it, returning the
207 // string containing the formated data.
208 func (f *Strftime) FormatString(t time.Time) string {
211 max := len(f.pattern) + 10
213 var buf [bufSize]byte
216 b = make([]byte, 0, max)
218 return string(f.format(b, t))