| 1 |
// Copyright 2021 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 htime |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
"time" |
| 19 |
|
| 20 |
"github.com/bep/golocales" |
| 21 |
|
| 22 |
qt "github.com/frankban/quicktest" |
| 23 |
) |
| 24 |
|
| 25 |
func TestTimeFormatter(t *testing.T) { |
| 26 |
c := qt.New(t) |
| 27 |
|
| 28 |
june06, _ := time.Parse("2006-Jan-02", "2018-Jun-06") |
| 29 |
june06 = june06.Add(7777 * time.Second) |
| 30 |
|
| 31 |
jan06, _ := time.Parse("2006-Jan-02", "2018-Jan-06") |
| 32 |
jan06 = jan06.Add(32 * time.Second) |
| 33 |
|
| 34 |
mondayNovemberFirst, _ := time.Parse("2006-Jan-02", "2021-11-01") |
| 35 |
mondayNovemberFirst = mondayNovemberFirst.Add(33 * time.Second) |
| 36 |
|
| 37 |
c.Run("Norsk nynorsk", func(c *qt.C) { |
| 38 |
f := NewTimeFormatter(golocales.New("nn")) |
| 39 |
|
| 40 |
c.Assert(f.Format(june06, "Monday Jan 2 2006"), qt.Equals, "onsdag juni 6 2018") |
| 41 |
c.Assert(f.Format(june06, "Mon January 2 2006"), qt.Equals, "on. juni 6 2018") |
| 42 |
c.Assert(f.Format(june06, "Mon Mon"), qt.Equals, "on. on.") |
| 43 |
}) |
| 44 |
|
| 45 |
c.Run("Custom layouts Norsk nynorsk", func(c *qt.C) { |
| 46 |
f := NewTimeFormatter(golocales.New("nn")) |
| 47 |
|
| 48 |
c.Assert(f.Format(june06, ":date_full"), qt.Equals, "onsdag 6. juni 2018") |
| 49 |
c.Assert(f.Format(june06, ":date_long"), qt.Equals, "6. juni 2018") |
| 50 |
c.Assert(f.Format(june06, ":date_medium"), qt.Equals, "6. juni 2018") |
| 51 |
c.Assert(f.Format(june06, ":date_short"), qt.Equals, "06.06.18") |
| 52 |
|
| 53 |
c.Assert(f.Format(june06, ":time_full"), qt.Equals, "02:09:37 UTC") |
| 54 |
c.Assert(f.Format(june06, ":time_long"), qt.Equals, "02:09:37 UTC") |
| 55 |
c.Assert(f.Format(june06, ":time_medium"), qt.Equals, "02:09:37") |
| 56 |
c.Assert(f.Format(june06, ":time_short"), qt.Equals, "02:09") |
| 57 |
}) |
| 58 |
|
| 59 |
c.Run("Custom layouts English", func(c *qt.C) { |
| 60 |
f := NewTimeFormatter(golocales.New("en")) |
| 61 |
|
| 62 |
c.Assert(f.Format(june06, ":date_full"), qt.Equals, "Wednesday, June 6, 2018") |
| 63 |
c.Assert(f.Format(june06, ":date_long"), qt.Equals, "June 6, 2018") |
| 64 |
c.Assert(f.Format(june06, ":date_medium"), qt.Equals, "Jun 6, 2018") |
| 65 |
c.Assert(f.Format(june06, ":date_short"), qt.Equals, "6/6/18") |
| 66 |
|
| 67 |
c.Assert(f.Format(june06, ":time_full"), qt.Equals, "2:09:37\u202fam UTC") |
| 68 |
c.Assert(f.Format(june06, ":time_long"), qt.Equals, "2:09:37\u202fam UTC") |
| 69 |
c.Assert(f.Format(june06, ":time_medium"), qt.Equals, "2:09:37\u202fam") |
| 70 |
c.Assert(f.Format(june06, ":time_short"), qt.Equals, "2:09\u202fam") |
| 71 |
}) |
| 72 |
|
| 73 |
c.Run("English", func(c *qt.C) { |
| 74 |
f := NewTimeFormatter(golocales.New("en")) |
| 75 |
|
| 76 |
c.Assert(f.Format(june06, "Monday Jan 2 2006"), qt.Equals, "Wednesday Jun 6 2018") |
| 77 |
c.Assert(f.Format(june06, "Mon January 2 2006"), qt.Equals, "Wed June 6 2018") |
| 78 |
c.Assert(f.Format(june06, "Mon Mon"), qt.Equals, "Wed Wed") |
| 79 |
}) |
| 80 |
|
| 81 |
c.Run("Weekdays German", func(c *qt.C) { |
| 82 |
tr := golocales.New("de") |
| 83 |
f := NewTimeFormatter(tr) |
| 84 |
|
| 85 |
// Issue #9107 |
| 86 |
for i, weekDayWideGerman := range []string{"Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"} { |
| 87 |
date := mondayNovemberFirst.Add(time.Duration(i*24) * time.Hour) |
| 88 |
c.Assert(tr.WeekdaysWide()[date.Weekday()], qt.Equals, weekDayWideGerman) |
| 89 |
c.Assert(f.Format(date, "Monday"), qt.Equals, weekDayWideGerman) |
| 90 |
} |
| 91 |
|
| 92 |
for i, weekDayAbbreviatedGerman := range []string{"Mo.", "Di.", "Mi.", "Do.", "Fr.", "Sa.", "So."} { |
| 93 |
date := mondayNovemberFirst.Add(time.Duration(i*24) * time.Hour) |
| 94 |
c.Assert(tr.WeekdaysAbbreviated()[date.Weekday()], qt.Equals, weekDayAbbreviatedGerman) |
| 95 |
c.Assert(f.Format(date, "Mon"), qt.Equals, weekDayAbbreviatedGerman) |
| 96 |
} |
| 97 |
}) |
| 98 |
|
| 99 |
c.Run("Months German", func(c *qt.C) { |
| 100 |
tr := golocales.New("de") |
| 101 |
f := NewTimeFormatter(tr) |
| 102 |
|
| 103 |
// Issue #9107 |
| 104 |
for i, monthWideNorway := range []string{"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli"} { |
| 105 |
date := jan06.Add(time.Duration(i*24*31) * time.Hour) |
| 106 |
c.Assert(tr.MonthsWide()[date.Month()-1], qt.Equals, monthWideNorway) |
| 107 |
c.Assert(f.Format(date, "January"), qt.Equals, monthWideNorway) |
| 108 |
} |
| 109 |
}) |
| 110 |
} |
| 111 |
|
| 112 |
func BenchmarkTimeFormatter(b *testing.B) { |
| 113 |
june06, _ := time.Parse("2006-Jan-02", "2018-Jun-06") |
| 114 |
|
| 115 |
b.Run("Native", func(b *testing.B) { |
| 116 |
for b.Loop() { |
| 117 |
got := june06.Format("Monday Jan 2 2006") |
| 118 |
if got != "Wednesday Jun 6 2018" { |
| 119 |
b.Fatalf("invalid format, got %q", got) |
| 120 |
} |
| 121 |
} |
| 122 |
}) |
| 123 |
|
| 124 |
b.Run("Localized", func(b *testing.B) { |
| 125 |
f := NewTimeFormatter(golocales.New("nn")) |
| 126 |
b.ResetTimer() |
| 127 |
for b.Loop() { |
| 128 |
got := f.Format(june06, "Monday Jan 2 2006") |
| 129 |
if got != "onsdag juni 6 2018" { |
| 130 |
b.Fatalf("invalid format, got %q", got) |
| 131 |
} |
| 132 |
} |
| 133 |
}) |
| 134 |
|
| 135 |
b.Run("Localized Custom", func(b *testing.B) { |
| 136 |
f := NewTimeFormatter(golocales.New("nn")) |
| 137 |
b.ResetTimer() |
| 138 |
for b.Loop() { |
| 139 |
got := f.Format(june06, ":date_medium") |
| 140 |
if got != "6. juni 2018" { |
| 141 |
b.Fatalf("invalid format, got %q", got) |
| 142 |
} |
| 143 |
} |
| 144 |
}) |
| 145 |
} |