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