| 
14 | 14 | package hugolib  | 
15 | 15 | 
 
  | 
16 | 16 | import (  | 
 | 17 | +	"fmt"  | 
 | 18 | +	"strings"  | 
17 | 19 | 	"testing"  | 
18 | 20 | )  | 
19 | 21 | 
 
  | 
@@ -54,3 +56,133 @@ Date: {{ .Date | time.Format ":date_long" }}  | 
54 | 56 | 	b.AssertFileContent("public/nn/index.html", `Date: 18. juli 2021`)  | 
55 | 57 | 
 
  | 
56 | 58 | }  | 
 | 59 | + | 
 | 60 | +func TestTimeZones(t *testing.T) {  | 
 | 61 | +	b := newTestSitesBuilder(t)  | 
 | 62 | +	b.WithConfigFile("toml", `  | 
 | 63 | +baseURL = "https://example.org"  | 
 | 64 | +
  | 
 | 65 | +defaultContentLanguage = "en"  | 
 | 66 | +defaultContentLanguageInSubDir = true  | 
 | 67 | +
  | 
 | 68 | +[languages]  | 
 | 69 | +[languages.en]  | 
 | 70 | +timeZone="UTC"  | 
 | 71 | +weight=10  | 
 | 72 | +[languages.nn]  | 
 | 73 | +timeZone="America/Antigua"  | 
 | 74 | +weight=20  | 
 | 75 | +	  | 
 | 76 | +`)  | 
 | 77 | + | 
 | 78 | +	const (  | 
 | 79 | +		pageTemplYaml = `---  | 
 | 80 | +title: Page  | 
 | 81 | +date: %s  | 
 | 82 | +lastMod: %s  | 
 | 83 | +publishDate: %s  | 
 | 84 | +expiryDate: %s  | 
 | 85 | +---	  | 
 | 86 | +`  | 
 | 87 | + | 
 | 88 | +		pageTemplTOML = `+++  | 
 | 89 | +title="Page"  | 
 | 90 | +date=%s  | 
 | 91 | +lastMod=%s  | 
 | 92 | +publishDate=%s  | 
 | 93 | +expiryDate=%s  | 
 | 94 | ++++  | 
 | 95 | +`  | 
 | 96 | + | 
 | 97 | +		shortDateTempl = `%d-07-%d`  | 
 | 98 | +		longDateTempl  = `%d-07-%d 15:28:01`  | 
 | 99 | +	)  | 
 | 100 | + | 
 | 101 | +	createPageContent := func(pageTempl, dateTempl string, quoted bool) string {  | 
 | 102 | +		createDate := func(year, i int) string {  | 
 | 103 | +			d := fmt.Sprintf(dateTempl, year, i)  | 
 | 104 | +			if quoted {  | 
 | 105 | +				return fmt.Sprintf("%q", d)  | 
 | 106 | +			}  | 
 | 107 | +			return d  | 
 | 108 | +		}  | 
 | 109 | + | 
 | 110 | +		return fmt.Sprintf(  | 
 | 111 | +			pageTempl,  | 
 | 112 | +			createDate(2021, 10),  | 
 | 113 | +			createDate(2021, 11),  | 
 | 114 | +			createDate(2021, 12),  | 
 | 115 | +			createDate(2099, 13), // This test will fail in 2099 :-)  | 
 | 116 | +		)  | 
 | 117 | +	}  | 
 | 118 | + | 
 | 119 | +	b.WithContent(  | 
 | 120 | +		// YAML  | 
 | 121 | +		"short-date-yaml-unqouted.en.md", createPageContent(pageTemplYaml, shortDateTempl, false),  | 
 | 122 | +		"short-date-yaml-unqouted.nn.md", createPageContent(pageTemplYaml, shortDateTempl, false),  | 
 | 123 | +		"short-date-yaml-qouted.en.md", createPageContent(pageTemplYaml, shortDateTempl, true),  | 
 | 124 | +		"short-date-yaml-qouted.nn.md", createPageContent(pageTemplYaml, shortDateTempl, true),  | 
 | 125 | +		"long-date-yaml-unqouted.en.md", createPageContent(pageTemplYaml, longDateTempl, false),  | 
 | 126 | +		"long-date-yaml-unqouted.nn.md", createPageContent(pageTemplYaml, longDateTempl, false),  | 
 | 127 | + | 
 | 128 | +		// TOML  | 
 | 129 | +		"short-date-toml-unqouted.en.md", createPageContent(pageTemplTOML, shortDateTempl, false),  | 
 | 130 | +		"short-date-toml-unqouted.nn.md", createPageContent(pageTemplTOML, shortDateTempl, false),  | 
 | 131 | +		"short-date-toml-qouted.en.md", createPageContent(pageTemplTOML, shortDateTempl, true),  | 
 | 132 | +		"short-date-toml-qouted.nn.md", createPageContent(pageTemplTOML, shortDateTempl, true),  | 
 | 133 | +	)  | 
 | 134 | + | 
 | 135 | +	const datesTempl = `  | 
 | 136 | +Date: {{ .Date | safeHTML  }}  | 
 | 137 | +Lastmod: {{ .Lastmod | safeHTML  }}  | 
 | 138 | +PublishDate: {{ .PublishDate | safeHTML  }}  | 
 | 139 | +ExpiryDate: {{ .ExpiryDate | safeHTML  }}  | 
 | 140 | +
  | 
 | 141 | +	`  | 
 | 142 | + | 
 | 143 | +	b.WithTemplatesAdded(  | 
 | 144 | +		"_default/single.html", datesTempl,  | 
 | 145 | +	)  | 
 | 146 | + | 
 | 147 | +	b.Build(BuildCfg{})  | 
 | 148 | + | 
 | 149 | +	expectShortDateEn := `  | 
 | 150 | +Date: 2021-07-10 00:00:00 +0000 UTC  | 
 | 151 | +Lastmod: 2021-07-11 00:00:00 +0000 UTC  | 
 | 152 | +PublishDate: 2021-07-12 00:00:00 +0000 UTC  | 
 | 153 | +ExpiryDate: 2099-07-13 00:00:00 +0000 UTC`  | 
 | 154 | + | 
 | 155 | +	expectShortDateNn := strings.ReplaceAll(expectShortDateEn, "+0000 UTC", "-0400 AST")  | 
 | 156 | + | 
 | 157 | +	expectLongDateEn := `  | 
 | 158 | +Date: 2021-07-10 15:28:01 +0000 UTC  | 
 | 159 | +Lastmod: 2021-07-11 15:28:01 +0000 UTC  | 
 | 160 | +PublishDate: 2021-07-12 15:28:01 +0000 UTC  | 
 | 161 | +ExpiryDate: 2099-07-13 15:28:01 +0000 UTC`  | 
 | 162 | + | 
 | 163 | +	expectLongDateNn := strings.ReplaceAll(expectLongDateEn, "+0000 UTC", "-0400 AST")  | 
 | 164 | + | 
 | 165 | +	// TODO(bep) create a common proposal for go-yaml, go-toml  | 
 | 166 | +	// for a custom date parser hook to handle these time zones.  | 
 | 167 | +	// JSON is omitted from this test as JSON does no (to my knowledge)  | 
 | 168 | +	// have date literals.  | 
 | 169 | + | 
 | 170 | +	// YAML  | 
 | 171 | +	// Note: This is with go-yaml v2, I suspect v3 will fail with the unquouted values.  | 
 | 172 | +	b.AssertFileContent("public/en/short-date-yaml-unqouted/index.html", expectShortDateEn)  | 
 | 173 | +	b.AssertFileContent("public/nn/short-date-yaml-unqouted/index.html", expectShortDateNn)  | 
 | 174 | +	b.AssertFileContent("public/en/short-date-yaml-qouted/index.html", expectShortDateEn)  | 
 | 175 | +	b.AssertFileContent("public/nn/short-date-yaml-qouted/index.html", expectShortDateNn)  | 
 | 176 | + | 
 | 177 | +	b.AssertFileContent("public/en/long-date-yaml-unqouted/index.html", expectLongDateEn)  | 
 | 178 | +	b.AssertFileContent("public/nn/long-date-yaml-unqouted/index.html", expectLongDateNn)  | 
 | 179 | + | 
 | 180 | +	// TOML  | 
 | 181 | +	// These fails: TOML (Burnt Sushi) defaults to local timezone.  | 
 | 182 | +	// TODO(bep) check go-toml  | 
 | 183 | +	//	b.AssertFileContent("public/en/short-date-toml-unqouted/index.html", expectShortDateEn)  | 
 | 184 | +	//  b.AssertFileContent("public/nn/short-date-toml-unqouted/index.html", expectShortDateNn)  | 
 | 185 | +	b.AssertFileContent("public/en/short-date-toml-qouted/index.html", expectShortDateEn)  | 
 | 186 | +	b.AssertFileContent("public/nn/short-date-toml-qouted/index.html", expectShortDateNn)  | 
 | 187 | + | 
 | 188 | +}  | 
0 commit comments