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