From 6bad64dca230bfa92b912a4a5a3513c8dd75ab92 Mon Sep 17 00:00:00 2001 From: Christopher Jenkins Date: Wed, 28 Jul 2021 19:54:16 -0700 Subject: [PATCH] add week of the year to datetime --- cty/function/stdlib/datetime.go | 12 ++++++++++++ cty/function/stdlib/datetime_test.go | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/cty/function/stdlib/datetime.go b/cty/function/stdlib/datetime.go index 1ceffcf6..fe77ab89 100644 --- a/cty/function/stdlib/datetime.go +++ b/cty/function/stdlib/datetime.go @@ -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) { @@ -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". diff --git a/cty/function/stdlib/datetime_test.go b/cty/function/stdlib/datetime_test.go index 7432f820..72a556c5 100644 --- a/cty/function/stdlib/datetime_test.go +++ b/cty/function/stdlib/datetime_test.go @@ -64,6 +64,11 @@ func TestFormatDate(t *testing.T) { cty.StringVal("Monday"), ``, }, + { + cty.StringVal("WW"), + cty.StringVal("01"), + ``, + }, { cty.StringVal("aa"), cty.StringVal("pm"),