Skip to content

Commit 1139574

Browse files
committed
bake: add unixtimestampparse function
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent b1c1048 commit 1139574

3 files changed

Lines changed: 116 additions & 1 deletion

File tree

bake/hclparser/stdlib.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ var stdlibFunctions = []funcDef{
129129
{name: "trimspace", fn: stdlib.TrimSpaceFunc},
130130
{name: "trimsuffix", fn: stdlib.TrimSuffixFunc},
131131
{name: "try", fn: tryfunc.TryFunc, descriptionAlt: `Variadic function that tries to evaluate all of is arguments in sequence until one succeeds, in which case it returns that result, or returns an error if none of them succeed.`},
132+
{name: "unixtimestampparse", factory: unixtimestampParseFunc},
132133
{name: "upper", fn: stdlib.UpperFunc},
133134
{name: "urlencode", fn: encoding.URLEncodeFunc, descriptionAlt: `Applies URL encoding to a given string.`},
134135
{name: "uuidv4", fn: uuid.V4Func, descriptionAlt: `Generates and returns a Type-4 UUID in the standard hexadecimal string format.`},
@@ -280,7 +281,7 @@ func semvercmpFunc() function.Function {
280281

281282
// timestampFunc constructs a function that returns a string representation of the current date and time.
282283
//
283-
// This function was imported from terraform's datetime utilities.
284+
// This function was imported from Terraform's datetime utilities.
284285
func timestampFunc() function.Function {
285286
return function.New(&function.Spec{
286287
Description: `Returns a string representation of the current date and time.`,
@@ -313,6 +314,59 @@ func homedirFunc() function.Function {
313314
})
314315
}
315316

317+
// unixtimestampParseFunc, given a unix timestamp integer, will parse and
318+
// return an object representation of that date and time
319+
//
320+
// This function is similar to the `unix_timestamp_parse` function in Terraform:
321+
// https://registry.terraform.io/providers/hashicorp/time/latest/docs/functions/unix_timestamp_parse
322+
func unixtimestampParseFunc() function.Function {
323+
return function.New(&function.Spec{
324+
Description: `Given a unix timestamp integer, will parse and return an object representation of that date and time. A unix timestamp is the number of seconds elapsed since January 1, 1970 UTC.`,
325+
Params: []function.Parameter{
326+
{
327+
Name: "unix_timestamp",
328+
Description: "Unix Timestamp integer to parse",
329+
Type: cty.Number,
330+
},
331+
},
332+
Type: function.StaticReturnType(cty.Object(map[string]cty.Type{
333+
"year": cty.Number,
334+
"year_day": cty.Number,
335+
"day": cty.Number,
336+
"month": cty.Number,
337+
"month_name": cty.String,
338+
"weekday": cty.Number,
339+
"weekday_name": cty.String,
340+
"hour": cty.Number,
341+
"minute": cty.Number,
342+
"second": cty.Number,
343+
"rfc3339": cty.String,
344+
"iso_year": cty.Number,
345+
"iso_week": cty.Number,
346+
})),
347+
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
348+
ts, _ := args[0].AsBigFloat().Int64()
349+
unixTime := time.Unix(ts, 0).UTC()
350+
isoYear, isoWeek := unixTime.ISOWeek()
351+
return cty.ObjectVal(map[string]cty.Value{
352+
"year": cty.NumberIntVal(int64(unixTime.Year())),
353+
"year_day": cty.NumberIntVal(int64(unixTime.YearDay())),
354+
"day": cty.NumberIntVal(int64(unixTime.Day())),
355+
"month": cty.NumberIntVal(int64(unixTime.Month())),
356+
"month_name": cty.StringVal(unixTime.Month().String()),
357+
"weekday": cty.NumberIntVal(int64(unixTime.Weekday())),
358+
"weekday_name": cty.StringVal(unixTime.Weekday().String()),
359+
"hour": cty.NumberIntVal(int64(unixTime.Hour())),
360+
"minute": cty.NumberIntVal(int64(unixTime.Minute())),
361+
"second": cty.NumberIntVal(int64(unixTime.Second())),
362+
"rfc3339": cty.StringVal(unixTime.Format(time.RFC3339)),
363+
"iso_year": cty.NumberIntVal(int64(isoYear)),
364+
"iso_week": cty.NumberIntVal(int64(isoWeek)),
365+
}), nil
366+
},
367+
})
368+
}
369+
316370
func Stdlib() map[string]function.Function {
317371
funcs := make(map[string]function.Function, len(stdlibFunctions))
318372
for _, v := range stdlibFunctions {

bake/hclparser/stdlib_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,29 @@ func TestSemverCmp(t *testing.T) {
258258
})
259259
}
260260
}
261+
262+
func TestUnixTimestampParseFunc(t *testing.T) {
263+
fn := unixtimestampParseFunc()
264+
input := cty.NumberIntVal(1690328596)
265+
got, err := fn.Call([]cty.Value{input})
266+
require.NoError(t, err)
267+
268+
expected := map[string]cty.Value{
269+
"year": cty.NumberIntVal(2023),
270+
"year_day": cty.NumberIntVal(206),
271+
"day": cty.NumberIntVal(25),
272+
"month": cty.NumberIntVal(7),
273+
"month_name": cty.StringVal("July"),
274+
"weekday": cty.NumberIntVal(2),
275+
"weekday_name": cty.StringVal("Tuesday"),
276+
"hour": cty.NumberIntVal(23),
277+
"minute": cty.NumberIntVal(43),
278+
"second": cty.NumberIntVal(16),
279+
"rfc3339": cty.StringVal("2023-07-25T23:43:16Z"),
280+
"iso_year": cty.NumberIntVal(2023),
281+
"iso_week": cty.NumberIntVal(30),
282+
}
283+
for k, v := range expected {
284+
require.True(t, got.GetAttr(k).RawEquals(v), "field %s: got %v, want %v", k, got.GetAttr(k), v)
285+
}
286+
}

docs/bake-stdlib.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ title: Bake standard library functions
104104
| [`trimspace`](#trimspace) | Removes any consecutive space characters (as defined by Unicode) from the start and end of the given string. |
105105
| [`trimsuffix`](#trimsuffix) | Removes the given suffix from the start of the given string, if present. |
106106
| [`try`](#try) | Variadic function that tries to evaluate all of is arguments in sequence until one succeeds, in which case it returns that result, or returns an error if none of them succeed. |
107+
| [`unixtimestampparse`](#unixtimestampparse) | Given a unix timestamp integer, will parse and return an object representation of that date and time. A unix timestamp is the number of seconds elapsed since January 1, 1970 UTC. |
107108
| [`upper`](#upper) | Returns the given string with all Unicode letters translated to their uppercase equivalents. |
108109
| [`urlencode`](#urlencode) | Applies URL encoding to a given string. |
109110
| [`uuidv4`](#uuidv4) | Generates and returns a Type-4 UUID in the standard hexadecimal string format. |
@@ -1409,6 +1410,40 @@ target "webapp-dev" {
14091410
}
14101411
```
14111412

1413+
## `unixtimestampparse`
1414+
1415+
The returned object has the following attributes:
1416+
* `year` (Number) The year for the unix timestamp.
1417+
* `year_day` (Number) The day of the year for the unix timestamp, in the range 1-365 for non-leap years, and 1-366 in leap years.
1418+
* `day` (Number) The day of the month for the unix timestamp.
1419+
* `month` (Number) The month of the year for the unix timestamp.
1420+
* `month_name` (String) The name of the month for the unix timestamp (ex. "January").
1421+
* `weekday` (Number) The day of the week for the unix timestamp.
1422+
* `weekday_name` (String) The name of the day for the unix timestamp (ex. "Sunday").
1423+
* `hour` (Number) The hour within the day for the unix timestamp, in the range 0-23.
1424+
* `minute` (Number) The minute offset within the hour for the unix timestamp, in the range 0-59.
1425+
* `second` (Number) The second offset within the minute for the unix timestamp, in the range 0-59.
1426+
* `rfc3339` (String) The RFC3339 format string.
1427+
* `iso_year` (Number) The ISO 8601 year number.
1428+
* `iso_week` (Number) The ISO 8601 week number.
1429+
1430+
```hcl
1431+
# docker-bake.hcl
1432+
variable "SOURCE_DATE_EPOCH" {
1433+
type = number
1434+
default = 1690328596
1435+
}
1436+
1437+
target "default" {
1438+
args = {
1439+
SOURCE_DATE_EPOCH = SOURCE_DATE_EPOCH
1440+
}
1441+
labels = {
1442+
"org.opencontainers.image.created" = unixtimestampparse(SOURCE_DATE_EPOCH).rfc3339
1443+
}
1444+
}
1445+
```
1446+
14121447
## `upper`
14131448

14141449
```hcl

0 commit comments

Comments
 (0)