From cd29c8d58604259ded9f2ebe2604838dbd5712de Mon Sep 17 00:00:00 2001 From: Nika Jones Date: Tue, 6 May 2014 06:00:56 -0700 Subject: [PATCH 1/2] Fixes #1092, Adds a format option to the {{isotime}} variable. Now using the golang magic date: Mon Jan 2 15:04:05 -0700 MST 2006 One can format the time like: {{isotime "2006-01-02"}} == "YYYY-MM-DD" {{isotime "060102-15"}} == "YYMMDD-HH" (24-hour clock) {{isotime "060102-3"}} == "YYMMDD-H" (12-hour clock) Using {{isotime}} as a standalone variable doesn't change. It still returns RFC3339 formatted time. --- packer/config_template.go | 12 ++++++++++-- packer/config_template_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packer/config_template.go b/packer/config_template.go index d76e3e672..99f4dc03f 100644 --- a/packer/config_template.go +++ b/packer/config_template.go @@ -110,8 +110,16 @@ func templateEnv(n string) string { return os.Getenv(n) } -func templateISOTime() string { - return time.Now().UTC().Format(time.RFC3339) +func templateISOTime(timeFormat ...string) (string, error) { + if len(timeFormat) == 0 { + return time.Now().UTC().Format(time.RFC3339), nil + } + + if len(timeFormat) > 1 { + return "", fmt.Errorf("too many values, 1 needed: %v", timeFormat) + } + + return time.Now().UTC().Format(timeFormat[0]), nil } func templatePwd() (string, error) { diff --git a/packer/config_template_test.go b/packer/config_template_test.go index 8604f2c43..e74e30c0d 100644 --- a/packer/config_template_test.go +++ b/packer/config_template_test.go @@ -1,6 +1,7 @@ package packer import ( + "fmt" "math" "os" "strconv" @@ -42,6 +43,33 @@ func TestConfigTemplateProcess_isotime(t *testing.T) { } } +// Note must format with the magic Date: Mon Jan 2 15:04:05 -0700 MST 2006 +func TestConfigTemplateProcess_isotime_withFormat(t *testing.T) { + tpl, err := NewConfigTemplate() + if err != nil { + t.Fatalf("err: %s", err) + } + + // Checking for a too-many arguments error + // Because of the variadic function, compile time checking won't work + _, err = tpl.Process(`{{isotime "20060102" "huh"}}`, nil) + if err == nil { + t.Fatalf("err: cannot have more than 1 input") + } + + result, err := tpl.Process(`{{isotime "20060102"}}`, nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + ti := time.Now().UTC() + val := fmt.Sprintf("%04d%02d%02d", ti.Year(), ti.Month(), ti.Day()) + + if result != val { + t.Fatalf("val: %s (formated: %s)", val, result) + } +} + func TestConfigTemplateProcess_pwd(t *testing.T) { tpl, err := NewConfigTemplate() if err != nil { From 04d9b0a1a5585ede943c4be93f7ad61fa14748e2 Mon Sep 17 00:00:00 2001 From: Nika Jones Date: Tue, 6 May 2014 23:51:33 -0700 Subject: [PATCH 2/2] Adds documentation to the website. Explains how to format the time. --- .../configuration-templates.html.markdown | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/website/source/docs/templates/configuration-templates.html.markdown b/website/source/docs/templates/configuration-templates.html.markdown index f2c42c2fb..5bb1e823e 100644 --- a/website/source/docs/templates/configuration-templates.html.markdown +++ b/website/source/docs/templates/configuration-templates.html.markdown @@ -55,9 +55,62 @@ in Packer templates. These are listed below for reference. * `pwd` - The working directory while executing Packer. * `isotime` - UTC time in RFC-3339 format. +* isotime `<format>` - UTC time in [<format>](http://golang.org/pkg/time/#example_Time_Format) format. * `timestamp` - The current Unix timestamp in UTC. * `uuid` - Returns a random UUID. +### isotime Format +Formatting for the function isotime `<format>` uses the magic reference date **Mon Jan 2 15:04:05 -0700 MST 2006**, which breaks down to the following: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Day of WeekMonthDateHourMinuteSecondYearTimezone
Numeric-010203 (15)040506-0700
TextualMonday (Mon)January (Jan)-----MST
+ _The values in parentheses are the abbreviated, or 24-hour clock values_ + + Here are some example formated time, using the above format options: + +
+isotime = June 7, 7:22:43pm 2014
+
+{{isotime "2006-01-02"}} = 2014-06-07
+{{isotime "Mon 1506"}} = Sat 1914
+{{isotime "01-Jan-06 03\_04\_05"}} = 07-Jun-2014 07\_22\_43
+{{isotime "Hour15Year200603"}} = Hour19Year201407
+
+ + ## Amazon Specific Functions Specific to Amazon builders: