Debian Codegraph @

hugolib/dates_test.go

raw
1 // Copyright 2025 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package hugolib
15
16 import (
17 "fmt"
18 "strings"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 )
23
24 func TestDateFormatMultilingual(t *testing.T) {
25 t.Parallel()
26
27 files := `
28 -- hugo.toml --
29 baseURL = "https://example.org"
30 defaultContentLanguage = "en"
31 defaultContentLanguageInSubDir = true
32 [languages]
33 [languages.en]
34 weight=10
35 [languages.nn]
36 weight=20
37 -- content/_index.en.md --
38 ---
39 title: Page
40 date: 2021-07-18
41 ---
42 -- content/_index.nn.md --
43 ---
44 title: Page
45 date: 2021-07-18
46 ---
47 -- layouts/home.html --
48 Date: {{ .Date | time.Format ":date_long" }}
49 `
50
51 b := Test(t, files)
52
53 b.AssertFileContent("public/en/index.html", `Date: July 18, 2021`)
54 b.AssertFileContent("public/nn/index.html", `Date: 18. juli 2021`)
55 }
56
57 func TestTimeZones(t *testing.T) {
58 t.Parallel()
59
60 const (
61 pageTemplYaml = `---
62 title: Page
63 date: %s
64 lastMod: %s
65 publishDate: %s
66 expiryDate: %s
67 ---
68 `
69
70 pageTemplTOML = `+++
71 title="Page"
72 date=%s
73 lastMod=%s
74 publishDate=%s
75 expiryDate=%s
76 +++
77 `
78
79 shortDateTempl = `%d-07-%d`
80 longDateTempl = `%d-07-%d 15:28:01`
81 )
82
83 createPageContent := func(pageTempl, dateTempl string, quoted bool) string {
84 createDate := func(year, i int) string {
85 d := fmt.Sprintf(dateTempl, year, i)
86 if quoted {
87 return fmt.Sprintf("%q", d)
88 }
89
90 return d
91 }
92
93 return fmt.Sprintf(
94 pageTempl,
95 createDate(2021, 10),
96 createDate(2021, 11),
97 createDate(2021, 12),
98 createDate(2099, 13), // This test will fail in 2099 :-)
99 )
100 }
101
102 files := `
103 -- hugo.toml --
104 baseURL = "https://example.org"
105 defaultContentLanguage = "en"
106 defaultContentLanguageInSubDir = true
107 [languages]
108 [languages.en]
109 timeZone="UTC"
110 weight=10
111 [languages.nn]
112 timeZone="America/Antigua"
113 weight=20
114 -- layouts/single.html --
115 Date: {{ .Date | safeHTML }}
116 Lastmod: {{ .Lastmod | safeHTML }}
117 PublishDate: {{ .PublishDate | safeHTML }}
118 ExpiryDate: {{ .ExpiryDate | safeHTML }}
119 -- content/short-date-yaml-unqouted.en.md --
120 ` + createPageContent(pageTemplYaml, shortDateTempl, false) + `
121 -- content/short-date-yaml-unqouted.nn.md --
122 ` + createPageContent(pageTemplYaml, shortDateTempl, false) + `
123 -- content/short-date-yaml-qouted.en.md --
124 ` + createPageContent(pageTemplYaml, shortDateTempl, true) + `
125 -- content/short-date-yaml-qouted.nn.md --
126 ` + createPageContent(pageTemplYaml, shortDateTempl, true) + `
127 -- content/long-date-yaml-unqouted.en.md --
128 ` + createPageContent(pageTemplYaml, longDateTempl, false) + `
129 -- content/long-date-yaml-unqouted.nn.md --
130 ` + createPageContent(pageTemplYaml, longDateTempl, false) + `
131 -- content/short-date-toml-unqouted.en.md --
132 ` + createPageContent(pageTemplTOML, shortDateTempl, false) + `
133 -- content/short-date-toml-unqouted.nn.md --
134 ` + createPageContent(pageTemplTOML, shortDateTempl, false) + `
135 -- content/short-date-toml-qouted.en.md --
136 ` + createPageContent(pageTemplTOML, shortDateTempl, true) + `
137 -- content/short-date-toml-qouted.nn.md --
138 ` + createPageContent(pageTemplTOML, shortDateTempl, true) + `
139 `
140
141 b := Test(t, files)
142
143 expectShortDateEn := `
144 Date: 2021-07-10 00:00:00 +0000 UTC
145 Lastmod: 2021-07-11 00:00:00 +0000 UTC
146 PublishDate: 2021-07-12 00:00:00 +0000 UTC
147 ExpiryDate: 2099-07-13 00:00:00 +0000 UTC`
148
149 expectShortDateNn := strings.ReplaceAll(expectShortDateEn, "+0000 UTC", "-0400 AST")
150
151 expectLongDateEn := `
152 Date: 2021-07-10 15:28:01 +0000 UTC
153 Lastmod: 2021-07-11 15:28:01 +0000 UTC
154 PublishDate: 2021-07-12 15:28:01 +0000 UTC
155 ExpiryDate: 2099-07-13 15:28:01 +0000 UTC`
156
157 expectLongDateNn := strings.ReplaceAll(expectLongDateEn, "+0000 UTC", "-0400 AST")
158
159 // YAML
160 b.AssertFileContent("public/en/short-date-yaml-unqouted/index.html", expectShortDateEn)
161 b.AssertFileContent("public/nn/short-date-yaml-unqouted/index.html", expectShortDateNn)
162 b.AssertFileContent("public/en/short-date-yaml-qouted/index.html", expectShortDateEn)
163 b.AssertFileContent("public/nn/short-date-yaml-qouted/index.html", expectShortDateNn)
164
165 b.AssertFileContent("public/en/long-date-yaml-unqouted/index.html", expectLongDateEn)
166 b.AssertFileContent("public/nn/long-date-yaml-unqouted/index.html", expectLongDateNn)
167
168 // TOML
169 b.AssertFileContent("public/en/short-date-toml-unqouted/index.html", expectShortDateEn)
170 b.AssertFileContent("public/nn/short-date-toml-unqouted/index.html", expectShortDateNn)
171 b.AssertFileContent("public/en/short-date-toml-qouted/index.html", expectShortDateEn)
172 b.AssertFileContent("public/nn/short-date-toml-qouted/index.html", expectShortDateNn)
173 }
174
175 // Issue 8832
176 func TestTimeZoneInvalid(t *testing.T) {
177 t.Parallel()
178
179 files := `
180 -- hugo.toml --
181 timeZone = "America/LosAngeles" # Should be America/Los_Angeles
182 `
183 b, err := TestE(t, files)
184
185 b.Assert(err, qt.Not(qt.IsNil))
186 b.Assert(err.Error(), qt.Contains, `invalid timeZone for language "en": unknown time zone America/LosAngeles`)
187 }
188
189 // Issue 8835
190 func TestTimeOnError(t *testing.T) {
191 t.Parallel()
192
193 files := `
194 -- hugo.toml --
195 -- layouts/home.html --
196 time: {{ time "2020-10-20" "invalid-timezone" }}
197 -- content/p1.md --
198 `
199 b, err := TestE(t, files)
200
201 b.Assert(err, qt.Not(qt.IsNil))
202 }
203
204 func TestTOMLDates(t *testing.T) {
205 t.Parallel()
206
207 files := `
208 -- hugo.toml --
209 timeZone = "America/Los_Angeles"
210 -- content/_index.md --
211 ---
212 date: "2020-10-20"
213 ---
214 -- content/p1.md --
215 +++
216 title = "TOML Date with UTC offset"
217 date = 2021-08-16T06:00:00+00:00
218 +++
219
220
221 ## Foo
222 -- data/mydata.toml --
223 date = 2020-10-20
224 talks = [
225 { date = 2017-01-23, name = "Past talk 1" },
226 { date = 2017-01-24, name = "Past talk 2" },
227 { date = 2017-01-26, name = "Past talk 3" },
228 { date = 2050-02-12, name = "Future talk 1" },
229 { date = 2050-02-13, name = "Future talk 2" },
230 ]
231 -- layouts/home.html --
232 {{ $futureTalks := where hugo.Data.mydata.talks "date" ">" now }}
233 {{ $pastTalks := where hugo.Data.mydata.talks "date" "<" now }}
234
235 {{ $homeDate := site.Home.Date }}
236 {{ $p1Date := (site.GetPage "p1").Date }}
237 Future talks: {{ len $futureTalks }}
238 Past talks: {{ len $pastTalks }}
239
240 Home's Date should be greater than past: {{ gt $homeDate (index $pastTalks 0).date }}
241 Home's Date should be less than future: {{ lt $homeDate (index $futureTalks 0).date }}
242 Home's Date should be equal mydata date: {{ eq $homeDate hugo.Data.mydata.date }}
243 Home date: {{ $homeDate }}
244 mydata.date: {{ hugo.Data.mydata.date }}
245 Full time: {{ $p1Date | time.Format ":time_full" }}
246 `
247
248 b := Test(t, files)
249
250 b.AssertFileContent("public/index.html", `
251 Future talks: 2
252 Past talks: 3
253 Home's Date should be greater than past: true
254 Home's Date should be less than future: true
255 Home's Date should be equal mydata date: true
256 Full time: 6:00:00 am UTC
257 `)
258 }
259
260 func TestPublisDateRollupIssue12438(t *testing.T) {
261 t.Parallel()
262
263 // To future Hugo maintainers, this test will start to fail in 2099.
264 files := `
265 -- hugo.toml --
266 disableKinds = ['home','rss','sitemap']
267 [taxonomies]
268 tag = 'tags'
269 -- layouts/list.html --
270 Date: {{ .Date.Format "2006-01-02" }}
271 PublishDate: {{ .PublishDate.Format "2006-01-02" }}
272 Lastmod: {{ .Lastmod.Format "2006-01-02" }}
273 -- layouts/single.html --
274 {{ .Title }}
275 -- content/s1/p1.md --
276 ---
277 title: p1
278 date: 2024-03-01
279 lastmod: 2024-03-02
280 tags: [t1]
281 ---
282 -- content/s1/p2.md --
283 ---
284 title: p2
285 date: 2024-04-03
286 lastmod: 2024-04-04
287 tags: [t1]
288 ---
289 -- content/s1/p3.md --
290 ---
291 title: p3
292 lastmod: 2099-05-06
293 tags: [t1]
294 ---
295
296 `
297
298 // Test without publishDate in front matter.
299 b := Test(t, files)
300
301 b.AssertFileContent("public/s1/index.html", `
302 Date: 2024-04-03
303 PublishDate: 2024-04-03
304 Lastmod: 2024-04-04
305 `)
306
307 b.AssertFileContent("public/tags/index.html", `
308 Date: 2024-04-03
309 PublishDate: 2024-04-03
310 Lastmod: 2024-04-04
311 `)
312
313 b.AssertFileContent("public/tags/t1/index.html", `
314 Date: 2024-04-03
315 PublishDate: 2024-04-03
316 Lastmod: 2024-04-04
317 `)
318
319 // Test with publishDate in front matter.
320 files = strings.ReplaceAll(files, "lastmod", "publishDate")
321
322 b = Test(t, files)
323
324 b.AssertFileContent("public/s1/index.html", `
325 Date: 2024-04-03
326 PublishDate: 2024-04-04
327 Lastmod: 2024-04-03
328 `)
329
330 b.AssertFileContent("public/tags/index.html", `
331 Date: 2024-04-03
332 PublishDate: 2024-04-04
333 Lastmod: 2024-04-03
334 `)
335
336 b.AssertFileContent("public/tags/t1/index.html", `
337 Date: 2024-04-03
338 PublishDate: 2024-04-04
339 Lastmod: 2024-04-03
340 `)
341 }