Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add week of the year to datetime #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cty/function/stdlib/datetime.go
Original file line number Diff line number Diff line change
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':
_, w := t.ISOWeek()
switch len(tok) {
case 1:
fmt.Fprintf(&buf, "%d", w)
case 2:
fmt.Fprintf(&buf, "%02d", w)
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
Original file line number Diff line number Diff line change
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