github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/timeutil/human_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package timeutil_test
    21  
    22  import (
    23  	"time"
    24  
    25  	"gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/timeutil"
    28  )
    29  
    30  type humanSuite struct {
    31  	beforeDSTbegins, afterDSTbegins, beforeDSTends, afterDSTends time.Time
    32  }
    33  
    34  var _ = check.Suite(&humanSuite{})
    35  
    36  func (s *humanSuite) SetUpSuite(c *check.C) {
    37  	loc, err := time.LoadLocation("Europe/London")
    38  	c.Assert(err, check.IsNil)
    39  
    40  	s.beforeDSTbegins = time.Date(2017, 3, 26, 0, 59, 0, 0, loc)
    41  	// note this is actually 2:01am DST
    42  	s.afterDSTbegins = time.Date(2017, 3, 26, 1, 1, 0, 0, loc)
    43  
    44  	// apparently no way to straight out initialise a time inside the DST overlap
    45  	s.beforeDSTends = time.Date(2017, 10, 29, 0, 59, 0, 0, loc).Add(60 * time.Minute)
    46  	s.afterDSTends = time.Date(2017, 10, 29, 1, 1, 0, 0, loc)
    47  
    48  	// sanity check
    49  	c.Check(s.beforeDSTbegins.Format("MST"), check.Equals, s.afterDSTends.Format("MST"))
    50  	c.Check(s.beforeDSTbegins.Format("MST"), check.Equals, "GMT")
    51  	c.Check(s.afterDSTbegins.Format("MST"), check.Equals, s.beforeDSTends.Format("MST"))
    52  	c.Check(s.afterDSTbegins.Format("MST"), check.Equals, "BST")
    53  
    54  	// “The month, day, hour, min, sec, and nsec values may be outside their
    55  	//  usual ranges and will be normalized during the conversion.”
    56  	// so you can always add or subtract 1 from a day and it'll just work \o/
    57  	c.Check(time.Date(2017, -1, -1, -1, -1, -1, 0, loc), check.DeepEquals, time.Date(2016, 10, 29, 22, 58, 59, 0, loc))
    58  	c.Check(time.Date(2017, 13, 32, 25, 61, 63, 0, loc), check.DeepEquals, time.Date(2018, 2, 2, 2, 2, 3, 0, loc))
    59  }
    60  
    61  func (s *humanSuite) TestHumanTimeDST(c *check.C) {
    62  	c.Check(timeutil.HumanTimeSince(s.beforeDSTbegins, s.afterDSTbegins, 300), check.Equals, "today at 00:59 GMT")
    63  	c.Check(timeutil.HumanTimeSince(s.beforeDSTends, s.afterDSTends, 300), check.Equals, "today at 01:59 BST")
    64  	c.Check(timeutil.HumanTimeSince(s.beforeDSTbegins, s.afterDSTends, 300), check.Equals, "217 days ago, at 00:59 GMT")
    65  }
    66  
    67  func (s *humanSuite) TestHumanTimeDSTMore(c *check.C) {
    68  	loc, err := time.LoadLocation("Europe/London")
    69  	c.Assert(err, check.IsNil)
    70  	d0 := time.Date(2018, 3, 23, 13, 14, 15, 0, loc)
    71  	df := time.Date(2018, 3, 25, 13, 14, 15, 0, loc)
    72  	c.Check(timeutil.HumanTimeSince(d0, df, 300), check.Equals, "2 days ago, at 13:14 GMT")
    73  	c.Check(timeutil.HumanTimeSince(df, d0, 300), check.Equals, "in 2 days, at 13:14 BST")
    74  }
    75  
    76  func (*humanSuite) TestHuman(c *check.C) {
    77  	now := time.Now()
    78  
    79  	for i, expected := range []string{
    80  		"2 days ago, at ", "yesterday at ", "today at ", "tomorrow at ", "in 2 days, at ",
    81  	} {
    82  		t := now.AddDate(0, 0, i-2)
    83  		timePart := t.Format("15:04 MST")
    84  		c.Check(timeutil.Human(t), check.Equals, expected+timePart)
    85  	}
    86  
    87  	// two outside of the 60-day cutoff:
    88  	d1 := now.AddDate(0, -3, 0)
    89  	d2 := now.AddDate(0, 3, 0)
    90  	c.Check(timeutil.Human(d1), check.Equals, d1.Format("2006-01-02"))
    91  	c.Check(timeutil.Human(d2), check.Equals, d2.Format("2006-01-02"))
    92  
    93  }