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

     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
    15  
    16  import (
    17  	"strings"
    18  	"testing"
    19  	"time"
    20  
    21  	qt "github.com/frankban/quicktest"
    22  
    23  	translators "github.com/gohugoio/localescompressed"
    24  )
    25  
    26  func TestTimeLocation(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	loc, _ := time.LoadLocation("America/Antigua")
    30  	ns := New(translators.GetTranslator("en"), loc)
    31  
    32  	for i, test := range []struct {
    33  		name     string
    34  		value    string
    35  		location interface{}
    36  		expect   interface{}
    37  	}{
    38  		{"Empty location", "2020-10-20", "", "2020-10-20 00:00:00 +0000 UTC"},
    39  		{"New location", "2020-10-20", nil, "2020-10-20 00:00:00 -0400 AST"},
    40  		{"New York EDT", "2020-10-20", "America/New_York", "2020-10-20 00:00:00 -0400 EDT"},
    41  		{"New York EST", "2020-01-20", "America/New_York", "2020-01-20 00:00:00 -0500 EST"},
    42  		{"Empty location, time", "2020-10-20 20:33:59", "", "2020-10-20 20:33:59 +0000 UTC"},
    43  		{"New York, time", "2020-10-20 20:33:59", "America/New_York", "2020-10-20 20:33:59 -0400 EDT"},
    44  		// The following have an explicit offset specified. In this case, it overrides timezone
    45  		{"Offset minus 0700, empty location", "2020-09-23T20:33:44-0700", "", "2020-09-23 20:33:44 -0700 -0700"},
    46  		{"Offset plus 0200, empty location", "2020-09-23T20:33:44+0200", "", "2020-09-23 20:33:44 +0200 +0200"},
    47  
    48  		{"Offset, New York", "2020-09-23T20:33:44-0700", "America/New_York", "2020-09-23 20:33:44 -0700 -0700"},
    49  		{"Offset, Oslo", "2020-09-23T20:33:44+0200", "Europe/Oslo", "2020-09-23 20:33:44 +0200 +0200"},
    50  
    51  		// Failures.
    52  		{"Invalid time zone", "2020-01-20", "invalid-timezone", false},
    53  		{"Invalid time value", "invalid-value", "", false},
    54  	} {
    55  		t.Run(test.name, func(t *testing.T) {
    56  			var args []interface{}
    57  			if test.location != nil {
    58  				args = append(args, test.location)
    59  			}
    60  			result, err := ns.AsTime(test.value, args...)
    61  			if b, ok := test.expect.(bool); ok && !b {
    62  				if err == nil {
    63  					t.Errorf("[%d] AsTime didn't return an expected error, got %v", i, result)
    64  				}
    65  			} else {
    66  				if err != nil {
    67  					t.Errorf("[%d] AsTime failed: %s", i, err)
    68  					return
    69  				}
    70  
    71  				// See https://github.com/gohugoio/hugo/issues/8843#issuecomment-891551447
    72  				// Drop the location string (last element) when comparing,
    73  				// as that may change depending on the local locale.
    74  				timeStr := result.(time.Time).String()
    75  				timeStr = timeStr[:strings.LastIndex(timeStr, " ")]
    76  				if !strings.HasPrefix(test.expect.(string), timeStr) {
    77  					t.Errorf("[%d] AsTime got %v but expected %v", i, timeStr, test.expect)
    78  				}
    79  			}
    80  		})
    81  	}
    82  }
    83  
    84  func TestFormat(t *testing.T) {
    85  	c := qt.New(t)
    86  
    87  	c.Run("UTC", func(c *qt.C) {
    88  		c.Parallel()
    89  		ns := New(translators.GetTranslator("en"), time.UTC)
    90  
    91  		for i, test := range []struct {
    92  			layout string
    93  			value  interface{}
    94  			expect interface{}
    95  		}{
    96  			{"Monday, Jan 2, 2006", "2015-01-21", "Wednesday, Jan 21, 2015"},
    97  			{"Monday, Jan 2, 2006", time.Date(2015, time.January, 21, 0, 0, 0, 0, time.UTC), "Wednesday, Jan 21, 2015"},
    98  			{"This isn't a date layout string", "2015-01-21", "This isn't a date layout string"},
    99  			// The following test case gives either "Tuesday, Jan 20, 2015" or "Monday, Jan 19, 2015" depending on the local time zone
   100  			{"Monday, Jan 2, 2006", 1421733600, time.Unix(1421733600, 0).Format("Monday, Jan 2, 2006")},
   101  			{"Monday, Jan 2, 2006", 1421733600.123, false},
   102  			{time.RFC3339, time.Date(2016, time.March, 3, 4, 5, 0, 0, time.UTC), "2016-03-03T04:05:00Z"},
   103  			{time.RFC1123, time.Date(2016, time.March, 3, 4, 5, 0, 0, time.UTC), "Thu, 03 Mar 2016 04:05:00 UTC"},
   104  			{time.RFC3339, "Thu, 03 Mar 2016 04:05:00 UTC", "2016-03-03T04:05:00Z"},
   105  			{time.RFC1123, "2016-03-03T04:05:00Z", "Thu, 03 Mar 2016 04:05:00 UTC"},
   106  			// Custom layouts, as introduced in Hugo 0.87.
   107  			{":date_medium", "2015-01-21", "Jan 21, 2015"},
   108  		} {
   109  			result, err := ns.Format(test.layout, test.value)
   110  			if b, ok := test.expect.(bool); ok && !b {
   111  				if err == nil {
   112  					c.Errorf("[%d] DateFormat didn't return an expected error, got %v", i, result)
   113  				}
   114  			} else {
   115  				if err != nil {
   116  					c.Errorf("[%d] DateFormat failed: %s", i, err)
   117  					continue
   118  				}
   119  				if result != test.expect {
   120  					c.Errorf("[%d] DateFormat got %v but expected %v", i, result, test.expect)
   121  				}
   122  			}
   123  		}
   124  	})
   125  
   126  	//Issue #9084
   127  	c.Run("TZ America/Los_Angeles", func(c *qt.C) {
   128  		c.Parallel()
   129  
   130  		loc, err := time.LoadLocation("America/Los_Angeles")
   131  		c.Assert(err, qt.IsNil)
   132  		ns := New(translators.GetTranslator("en"), loc)
   133  
   134  		d, err := ns.Format(":time_full", "2020-03-09T11:00:00")
   135  
   136  		c.Assert(err, qt.IsNil)
   137  		c.Assert(d, qt.Equals, "11:00:00 am Pacific Daylight Time")
   138  
   139  	})
   140  
   141  }
   142  
   143  func TestDuration(t *testing.T) {
   144  	t.Parallel()
   145  
   146  	ns := New(translators.GetTranslator("en"), time.UTC)
   147  
   148  	for i, test := range []struct {
   149  		unit   interface{}
   150  		num    interface{}
   151  		expect interface{}
   152  	}{
   153  		{"nanosecond", 10, 10 * time.Nanosecond},
   154  		{"ns", 10, 10 * time.Nanosecond},
   155  		{"microsecond", 20, 20 * time.Microsecond},
   156  		{"us", 20, 20 * time.Microsecond},
   157  		{"µs", 20, 20 * time.Microsecond},
   158  		{"millisecond", 20, 20 * time.Millisecond},
   159  		{"ms", 20, 20 * time.Millisecond},
   160  		{"second", 30, 30 * time.Second},
   161  		{"s", 30, 30 * time.Second},
   162  		{"minute", 20, 20 * time.Minute},
   163  		{"m", 20, 20 * time.Minute},
   164  		{"hour", 20, 20 * time.Hour},
   165  		{"h", 20, 20 * time.Hour},
   166  		{"hours", 20, false},
   167  		{"hour", "30", 30 * time.Hour},
   168  	} {
   169  		result, err := ns.Duration(test.unit, test.num)
   170  		if b, ok := test.expect.(bool); ok && !b {
   171  			if err == nil {
   172  				t.Errorf("[%d] Duration didn't return an expected error, got %v", i, result)
   173  			}
   174  		} else {
   175  			if err != nil {
   176  				t.Errorf("[%d] Duration failed: %s", i, err)
   177  				continue
   178  			}
   179  			if result != test.expect {
   180  				t.Errorf("[%d] Duration got %v but expected %v", i, result, test.expect)
   181  			}
   182  		}
   183  	}
   184  }