10 "github.com/zclconf/go-cty/cty"
11 "github.com/zclconf/go-cty/cty/function"
14 var FormatDateFunc = function.New(&function.Spec{
15 Params: []function.Parameter{
25 Type: function.StaticReturnType(cty.String),
26 Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
27 formatStr := args[0].AsString()
28 timeStr := args[1].AsString()
29 t, err := parseTimestamp(timeStr)
31 return cty.DynamicVal, function.NewArgError(1, err)
35 sc := bufio.NewScanner(strings.NewReader(formatStr))
36 sc.Split(splitDateFormat)
41 // The leading byte signals the token type
44 if tok[len(tok)-1] != esc || len(tok) == 1 {
45 return cty.DynamicVal, function.NewArgErrorf(0, "unterminated literal '")
48 // Must be a single escaped quote, ''
51 // The content (until a closing esc) is printed out verbatim
52 // except that we must un-double any double-esc escapes in
53 // the middle of the string.
54 raw := tok[1 : len(tok)-1]
55 for i := 0; i < len(raw); i++ {
58 i++ // skip the escaped quote
63 case startsDateFormatVerb(tok[0]):
69 fmt.Fprintf(&buf, "%02d", y%100)
71 fmt.Fprintf(&buf, "%04d", y)
73 return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q: year must either be \"YY\" or \"YYYY\"", tok)
79 fmt.Fprintf(&buf, "%d", m)
81 fmt.Fprintf(&buf, "%02d", m)
83 buf.WriteString(m.String()[:3])
85 buf.WriteString(m.String())
87 return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q: month must be \"M\", \"MM\", \"MMM\", or \"MMMM\"", tok)
93 fmt.Fprintf(&buf, "%d", d)
95 fmt.Fprintf(&buf, "%02d", d)
97 return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q: day of month must either be \"D\" or \"DD\"", tok)
103 buf.WriteString(d.String()[:3])
105 buf.WriteString(d.String())
107 return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q: day of week must either be \"EEE\" or \"EEEE\"", tok)
113 fmt.Fprintf(&buf, "%d", h)
115 fmt.Fprintf(&buf, "%02d", h)
117 return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q: 24-hour must either be \"h\" or \"hh\"", tok)
126 fmt.Fprintf(&buf, "%d", h)
128 fmt.Fprintf(&buf, "%02d", h)
130 return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q: 12-hour must either be \"H\" or \"HH\"", tok)
134 return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q: must be \"%s%s\"", tok, tok[0:1], tok[0:1])
136 upper := tok[0] == 'A'
137 switch t.Hour() / 12 {
140 buf.WriteString("AM")
142 buf.WriteString("am")
146 buf.WriteString("PM")
148 buf.WriteString("pm")
155 fmt.Fprintf(&buf, "%d", m)
157 fmt.Fprintf(&buf, "%02d", m)
159 return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q: minute must either be \"m\" or \"mm\"", tok)
165 fmt.Fprintf(&buf, "%d", s)
167 fmt.Fprintf(&buf, "%02d", s)
169 return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q: second must either be \"s\" or \"ss\"", tok)
172 // We'll just lean on Go's own formatter for this one, since
173 // the necessary information is unexported.
176 buf.WriteString(t.Format("Z07:00"))
178 str := t.Format("-0700")
181 buf.WriteString("UTC")
186 buf.WriteString(t.Format("-0700"))
188 buf.WriteString(t.Format("-07:00"))
190 return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q: timezone must be Z, ZZZZ, or ZZZZZ", tok)
193 return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q", tok)
197 // Any other starting character indicates a literal sequence
202 return cty.StringVal(buf.String()), nil
206 // FormatDate reformats a timestamp given in RFC3339 syntax into another time
207 // syntax defined by a given format string.
209 // The format string uses letter mnemonics to represent portions of the
210 // timestamp, with repetition signifying length variants of each portion.
211 // Single quote characters ' can be used to quote sequences of literal letters
212 // that should not be interpreted as formatting mnemonics.
214 // The full set of supported mnemonic sequences is listed below:
216 // YY Year modulo 100 zero-padded to two digits, like "06".
217 // YYYY Four (or more) digit year, like "2006".
218 // M Month number, like "1" for January.
219 // MM Month number zero-padded to two digits, like "01".
220 // MMM English month name abbreviated to three letters, like "Jan".
221 // MMMM English month name unabbreviated, like "January".
222 // D Day of month number, like "2".
223 // DD Day of month number zero-padded to two digits, like "02".
224 // EEE English day of week name abbreviated to three letters, like "Mon".
225 // EEEE English day of week name unabbreviated, like "Monday".
226 // h 24-hour number, like "2".
227 // hh 24-hour number zero-padded to two digits, like "02".
228 // H 12-hour number, like "2".
229 // HH 12-hour number zero-padded to two digits, like "02".
230 // AA Hour AM/PM marker in uppercase, like "AM".
231 // aa Hour AM/PM marker in lowercase, like "am".
232 // m Minute within hour, like "5".
233 // mm Minute within hour zero-padded to two digits, like "05".
234 // s Second within minute, like "9".
235 // ss Second within minute zero-padded to two digits, like "09".
236 // ZZZZ Timezone offset with just sign and digit, like "-0800".
237 // ZZZZZ Timezone offset with colon separating hours and minutes, like "-08:00".
238 // Z Like ZZZZZ but with a special case "Z" for UTC.
239 // ZZZ Like ZZZZ but with a special case "UTC" for UTC.
241 // The format syntax is optimized mainly for generating machine-oriented
242 // timestamps rather than human-oriented timestamps; the English language
243 // portions of the output reflect the use of English names in a number of
244 // machine-readable date formatting standards. For presentation to humans,
245 // a locale-aware time formatter (not included in this package) is a better
248 // The format syntax is not compatible with that of any other language, but
249 // is optimized so that patterns for common standard date formats can be
250 // recognized quickly even by a reader unfamiliar with the format syntax.
251 func FormatDate(format cty.Value, timestamp cty.Value) (cty.Value, error) {
252 return FormatDateFunc.Call([]cty.Value{format, timestamp})
255 func parseTimestamp(ts string) (time.Time, error) {
256 t, err := time.Parse(time.RFC3339, ts)
258 switch err := err.(type) {
259 case *time.ParseError:
260 // If err is s time.ParseError then its string representation is not
261 // appropriate since it relies on details of Go's strange date format
262 // representation, which a caller of our functions is not expected
263 // to be familiar with.
265 // Therefore we do some light transformation to get a more suitable
266 // error that should make more sense to our callers. These are
267 // still not awesome error messages, but at least they refer to
268 // the timestamp portions by name rather than by Go's example
270 if err.LayoutElem == "" && err.ValueElem == "" && err.Message != "" {
271 // For some reason err.Message is populated with a ": " prefix
272 // by the time package.
273 return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp%s", err.Message)
276 switch err.LayoutElem {
282 what = "day of month"
292 return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: missing required time introducer 'T'")
294 if err.ValueElem == "" {
295 return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: end of string where %q is expected", err.LayoutElem)
297 return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: found %q where %q is expected", err.ValueElem, err.LayoutElem)
300 // Should never get here, because time.RFC3339 includes only the
301 // above portions, but since that might change in future we'll
303 what = "timestamp segment"
305 if err.ValueElem == "" {
306 return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: end of string before %s", what)
308 return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: cannot use %q as %s", err.ValueElem, what)
311 return time.Time{}, err
316 // splitDataFormat is a bufio.SplitFunc used to tokenize a date format.
317 func splitDateFormat(data []byte, atEOF bool) (advance int, token []byte, err error) {
327 // If we have another quote immediately after then this is a single
329 if len(data) > 1 && data[1] == esc {
330 return 2, data[:2], nil
333 // Beginning of quoted sequence, so we will seek forward until we find
334 // the closing quote, ignoring escaped quotes along the way.
335 for i := 1; i < len(data); i++ {
337 if (i + 1) == len(data) {
338 // We need at least one more byte to decide if this is an
339 // escape or a terminator.
342 if data[i+1] == esc {
343 i++ // doubled-up quotes are an escape sequence
346 // We've found the closing quote
347 return i + 1, data[:i+1], nil
350 // If we fall out here then we need more bytes to find the end,
351 // unless we're already at the end with an unclosed quote.
353 return len(data), data, nil
357 case startsDateFormatVerb(data[0]):
359 for i := 1; i < len(data); i++ {
361 return i, data[:i], nil
365 return len(data), data, nil
367 // We need more data to decide if we've found the end
371 for i := 1; i < len(data); i++ {
372 if data[i] == esc || startsDateFormatVerb(data[i]) {
373 return i, data[:i], nil
376 // We might not actually be at the end of a literal sequence,
377 // but that doesn't matter since we'll concat them back together
379 return len(data), data, nil
383 func startsDateFormatVerb(b byte) bool {
384 return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')