github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/common/htime/time_test.go (about)

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