| 1 |
// Copyright 2017 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 time_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"strings" |
| 18 |
"testing" |
| 19 |
gtime "time" |
| 20 |
|
| 21 |
"github.com/bep/golocales" |
| 22 |
qt "github.com/frankban/quicktest" |
| 23 |
|
| 24 |
"github.com/gohugoio/hugo/common/htime" |
| 25 |
"github.com/gohugoio/hugo/hugolib" |
| 26 |
"github.com/gohugoio/hugo/tpl/time" |
| 27 |
) |
| 28 |
|
| 29 |
func TestTimeLocation(t *testing.T) { |
| 30 |
t.Parallel() |
| 31 |
|
| 32 |
b := hugolib.Test(t, "") |
| 33 |
|
| 34 |
loc, _ := gtime.LoadLocation("America/Antigua") |
| 35 |
ns := time.New(htime.NewTimeFormatter(golocales.New("en")), loc, b.H.Deps) |
| 36 |
|
| 37 |
for i, test := range []struct { |
| 38 |
name string |
| 39 |
value string |
| 40 |
location any |
| 41 |
expect any |
| 42 |
}{ |
| 43 |
{"Empty location", "2020-10-20", "", "2020-10-20 00:00:00 +0000 UTC"}, |
| 44 |
{"New location", "2020-10-20", nil, "2020-10-20 00:00:00 -0400 AST"}, |
| 45 |
{"New York EDT", "2020-10-20", "America/New_York", "2020-10-20 00:00:00 -0400 EDT"}, |
| 46 |
{"New York EST", "2020-01-20", "America/New_York", "2020-01-20 00:00:00 -0500 EST"}, |
| 47 |
{"Empty location, time", "2020-10-20 20:33:59", "", "2020-10-20 20:33:59 +0000 UTC"}, |
| 48 |
{"New York, time", "2020-10-20 20:33:59", "America/New_York", "2020-10-20 20:33:59 -0400 EDT"}, |
| 49 |
// The following have an explicit offset specified. In this case, it overrides timezone |
| 50 |
{"Offset minus 0700, empty location", "2020-09-23T20:33:44-0700", "", "2020-09-23 20:33:44 -0700 -0700"}, |
| 51 |
{"Offset plus 0200, empty location", "2020-09-23T20:33:44+0200", "", "2020-09-23 20:33:44 +0200 +0200"}, |
| 52 |
|
| 53 |
{"Offset, New York", "2020-09-23T20:33:44-0700", "America/New_York", "2020-09-23 20:33:44 -0700 -0700"}, |
| 54 |
{"Offset, Oslo", "2020-09-23T20:33:44+0200", "Europe/Oslo", "2020-09-23 20:33:44 +0200 +0200"}, |
| 55 |
|
| 56 |
// Failures. |
| 57 |
{"Invalid time zone", "2020-01-20", "invalid-timezone", false}, |
| 58 |
{"Invalid time value", "invalid-value", "", false}, |
| 59 |
} { |
| 60 |
t.Run(test.name, func(t *testing.T) { |
| 61 |
var args []any |
| 62 |
if test.location != nil { |
| 63 |
args = append(args, test.location) |
| 64 |
} |
| 65 |
result, err := ns.AsTime(test.value, args...) |
| 66 |
if b, ok := test.expect.(bool); ok && !b { |
| 67 |
if err == nil { |
| 68 |
t.Errorf("[%d] AsTime didn't return an expected error, got %v", i, result) |
| 69 |
} |
| 70 |
} else { |
| 71 |
if err != nil { |
| 72 |
t.Errorf("[%d] AsTime failed: %s", i, err) |
| 73 |
return |
| 74 |
} |
| 75 |
|
| 76 |
// See https://github.com/gohugoio/hugo/issues/8843#issuecomment-891551447 |
| 77 |
// Drop the location string (last element) when comparing, |
| 78 |
// as that may change depending on the local locale. |
| 79 |
timeStr := result.(gtime.Time).String() |
| 80 |
timeStr = timeStr[:strings.LastIndex(timeStr, " ")] |
| 81 |
if !strings.HasPrefix(test.expect.(string), timeStr) { |
| 82 |
t.Errorf("[%d] AsTime got %v but expected %v", i, timeStr, test.expect) |
| 83 |
} |
| 84 |
} |
| 85 |
}) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func TestFormat(t *testing.T) { |
| 90 |
c := qt.New(t) |
| 91 |
|
| 92 |
b := hugolib.Test(t, "") |
| 93 |
|
| 94 |
c.Run("UTC", func(c *qt.C) { |
| 95 |
c.Parallel() |
| 96 |
|
| 97 |
ns := time.New(htime.NewTimeFormatter(golocales.New("en")), gtime.UTC, b.H.Deps) |
| 98 |
|
| 99 |
for i, test := range []struct { |
| 100 |
layout string |
| 101 |
value any |
| 102 |
expect any |
| 103 |
}{ |
| 104 |
{"Monday, Jan 2, 2006", "2015-01-21", "Wednesday, Jan 21, 2015"}, |
| 105 |
{"Monday, Jan 2, 2006", gtime.Date(2015, gtime.January, 21, 0, 0, 0, 0, gtime.UTC), "Wednesday, Jan 21, 2015"}, |
| 106 |
{"This isn't a date layout string", "2015-01-21", "This isn't a date layout string"}, |
| 107 |
// The following test case gives either "Tuesday, Jan 20, 2015" or "Monday, Jan 19, 2015" depending on the local time zone |
| 108 |
{"Monday, Jan 2, 2006", 1421733600, gtime.Unix(1421733600, 0).Format("Monday, Jan 2, 2006")}, |
| 109 |
{"Monday, Jan 2, 2006", 1421733600.123, false}, |
| 110 |
{gtime.RFC3339, gtime.Date(2016, gtime.March, 3, 4, 5, 0, 0, gtime.UTC), "2016-03-03T04:05:00Z"}, |
| 111 |
{gtime.RFC1123, gtime.Date(2016, gtime.March, 3, 4, 5, 0, 0, gtime.UTC), "Thu, 03 Mar 2016 04:05:00 UTC"}, |
| 112 |
{gtime.RFC3339, "Thu, 03 Mar 2016 04:05:00 UTC", "2016-03-03T04:05:00Z"}, |
| 113 |
{gtime.RFC1123, "2016-03-03T04:05:00Z", "Thu, 03 Mar 2016 04:05:00 UTC"}, |
| 114 |
// Custom layouts, as introduced in Hugo 0.87. |
| 115 |
{":date_medium", "2015-01-21", "Jan 21, 2015"}, |
| 116 |
} { |
| 117 |
result, err := ns.Format(test.layout, test.value) |
| 118 |
if b, ok := test.expect.(bool); ok && !b { |
| 119 |
if err == nil { |
| 120 |
c.Errorf("[%d] DateFormat didn't return an expected error, got %v", i, result) |
| 121 |
} |
| 122 |
} else { |
| 123 |
if err != nil { |
| 124 |
c.Errorf("[%d] DateFormat failed: %s", i, err) |
| 125 |
continue |
| 126 |
} |
| 127 |
if result != test.expect { |
| 128 |
c.Errorf("[%d] DateFormat got %v but expected %v", i, result, test.expect) |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
}) |
| 133 |
|
| 134 |
// Issue #9084 |
| 135 |
c.Run("TZ America/Los_Angeles", func(c *qt.C) { |
| 136 |
c.Parallel() |
| 137 |
|
| 138 |
loc, err := gtime.LoadLocation("America/Los_Angeles") |
| 139 |
c.Assert(err, qt.IsNil) |
| 140 |
ns := time.New(htime.NewTimeFormatter(golocales.New("en")), loc, b.H.Deps) |
| 141 |
|
| 142 |
d, err := ns.Format(":time_full", "2020-03-09T11:00:00") |
| 143 |
|
| 144 |
c.Assert(err, qt.IsNil) |
| 145 |
c.Assert(d, qt.Equals, "11:00:00\u202fam Pacific Daylight Time") |
| 146 |
}) |
| 147 |
} |
| 148 |
|
| 149 |
func TestDuration(t *testing.T) { |
| 150 |
t.Parallel() |
| 151 |
|
| 152 |
b := hugolib.Test(t, "") |
| 153 |
|
| 154 |
ns := time.New(htime.NewTimeFormatter(golocales.New("en")), gtime.UTC, b.H.Deps) |
| 155 |
|
| 156 |
for i, test := range []struct { |
| 157 |
unit any |
| 158 |
num any |
| 159 |
expect any |
| 160 |
}{ |
| 161 |
{"nanosecond", 10, 10 * gtime.Nanosecond}, |
| 162 |
{"ns", 10, 10 * gtime.Nanosecond}, |
| 163 |
{"microsecond", 20, 20 * gtime.Microsecond}, |
| 164 |
{"us", 20, 20 * gtime.Microsecond}, |
| 165 |
{"µs", 20, 20 * gtime.Microsecond}, |
| 166 |
{"millisecond", 20, 20 * gtime.Millisecond}, |
| 167 |
{"ms", 20, 20 * gtime.Millisecond}, |
| 168 |
{"second", 30, 30 * gtime.Second}, |
| 169 |
{"s", 30, 30 * gtime.Second}, |
| 170 |
{"minute", 20, 20 * gtime.Minute}, |
| 171 |
{"m", 20, 20 * gtime.Minute}, |
| 172 |
{"hour", 20, 20 * gtime.Hour}, |
| 173 |
{"h", 20, 20 * gtime.Hour}, |
| 174 |
{"hours", 20, false}, |
| 175 |
{"hour", "30", 30 * gtime.Hour}, |
| 176 |
} { |
| 177 |
result, err := ns.Duration(test.unit, test.num) |
| 178 |
if b, ok := test.expect.(bool); ok && !b { |
| 179 |
if err == nil { |
| 180 |
t.Errorf("[%d] Duration didn't return an expected error, got %v", i, result) |
| 181 |
} |
| 182 |
} else { |
| 183 |
if err != nil { |
| 184 |
t.Errorf("[%d] Duration failed: %s", i, err) |
| 185 |
continue |
| 186 |
} |
| 187 |
if result != test.expect { |
| 188 |
t.Errorf("[%d] Duration got %v but expected %v", i, result, test.expect) |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
func TestIn(t *testing.T) { |
| 195 |
t.Parallel() |
| 196 |
|
| 197 |
b := hugolib.Test(t, "") |
| 198 |
|
| 199 |
ns := time.New(htime.NewTimeFormatter(golocales.New("en")), gtime.UTC, b.H.Deps) |
| 200 |
|
| 201 |
in := gtime.Date(2025, gtime.March, 31, 15, 0, 0, 0, gtime.UTC) |
| 202 |
|
| 203 |
tests := []struct { |
| 204 |
name string |
| 205 |
tzn string // time zone name |
| 206 |
want string |
| 207 |
wantErr bool |
| 208 |
}{ |
| 209 |
{name: "A", tzn: "America/Denver", want: "2025-03-31T09:00:00-06:00", wantErr: false}, |
| 210 |
{name: "B", tzn: "Australia/Adelaide", want: "2025-04-01T01:30:00+10:30", wantErr: false}, |
| 211 |
{name: "C", tzn: "Europe/Oslo", want: "2025-03-31T17:00:00+02:00", wantErr: false}, |
| 212 |
{name: "D", tzn: "UTC", want: "2025-03-31T15:00:00+00:00", wantErr: false}, |
| 213 |
{name: "E", tzn: "", want: "2025-03-31T15:00:00+00:00", wantErr: false}, |
| 214 |
{name: "F", tzn: "InvalidTimeZoneName", want: "0001-01-01T00:00:00+00:00", wantErr: true}, |
| 215 |
} |
| 216 |
|
| 217 |
for _, tt := range tests { |
| 218 |
t.Run(tt.name, func(t *testing.T) { |
| 219 |
result, err := ns.In(tt.tzn, in) |
| 220 |
if (err != nil) != tt.wantErr { |
| 221 |
t.Errorf("time.In() error = %v, wantErr %v", err, tt.wantErr) |
| 222 |
return |
| 223 |
} |
| 224 |
got := result.Format("2006-01-02T15:04:05-07:00") |
| 225 |
if got != tt.want { |
| 226 |
t.Errorf("time.In() = %v, want %v", got, tt.want) |
| 227 |
} |
| 228 |
}) |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
// For benchmark tests below. |
| 233 |
var timeZoneNames []string = []string{"America/New_York", "Europe/Oslo", "Australia/Sydney", "UTC", "Local"} |
| 234 |
|
| 235 |
func BenchmarkInWithCaching(b *testing.B) { |
| 236 |
bb := hugolib.Test(b, "") |
| 237 |
|
| 238 |
ns := time.New(htime.NewTimeFormatter(golocales.New("en")), gtime.UTC, bb.H.Deps) |
| 239 |
|
| 240 |
for i := 0; b.Loop(); i++ { |
| 241 |
timeZoneName := timeZoneNames[i%len(timeZoneNames)] |
| 242 |
_, err := ns.In(timeZoneName, gtime.Now()) |
| 243 |
if err != nil { |
| 244 |
b.Fatalf("Error during benchmark: %v", err) |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
func BenchmarkInWithoutCaching(b *testing.B) { |
| 250 |
for i := 0; b.Loop(); i++ { |
| 251 |
timeZoneName := timeZoneNames[i%len(timeZoneNames)] |
| 252 |
location, err := gtime.LoadLocation(timeZoneName) |
| 253 |
if err != nil { |
| 254 |
b.Fatalf("Error during benchmark: %v", err) |
| 255 |
} |
| 256 |
|
| 257 |
_ = gtime.Now().In(location) |
| 258 |
} |
| 259 |
} |