Skip to content

Commit

Permalink
add week of the year to datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
sevendials committed Jul 29, 2021
1 parent aba65fb commit 6bad64d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cty/function/stdlib/datetime.go
Expand Up @@ -86,6 +86,16 @@ var FormatDateFunc = function.New(&function.Spec{
default:
return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q: month must be \"M\", \"MM\", \"MMM\", or \"MMMM\"", tok)
}
case 'W':
_, d := t.ISOWeek()
switch len(tok) {
case 1:
fmt.Fprintf(&buf, "%d", d)
case 2:
fmt.Fprintf(&buf, "%02d", d)
default:
return cty.DynamicVal, function.NewArgErrorf(0, "invalid date format verb %q: week of year must either be \"W\" or \"WW\"", tok)
}
case 'D':
d := t.Day()
switch len(tok) {
Expand Down Expand Up @@ -246,6 +256,8 @@ var TimeAddFunc = function.New(&function.Spec{
// MM Month number zero-padded to two digits, like "01".
// MMM English month name abbreviated to three letters, like "Jan".
// MMMM English month name unabbreviated, like "January".
// W Week of the year number, like "3".
// WW Week of the year number zero-padded to two digits, like "26".
// D Day of month number, like "2".
// DD Day of month number zero-padded to two digits, like "02".
// EEE English day of week name abbreviated to three letters, like "Mon".
Expand Down
5 changes: 5 additions & 0 deletions cty/function/stdlib/datetime_test.go
Expand Up @@ -64,6 +64,11 @@ func TestFormatDate(t *testing.T) {
cty.StringVal("Monday"),
``,
},
{
cty.StringVal("WW"),
cty.StringVal("01"),
``,
},
{
cty.StringVal("aa"),
cty.StringVal("pm"),
Expand Down

0 comments on commit 6bad64d

Please sign in to comment.