@@ -1693,3 +1693,77 @@ func TestTimeWithZoneTransition(t *testing.T) {
1693
1693
}
1694
1694
}
1695
1695
}
1696
+
1697
+ func TestZoneBounds (t * testing.T ) {
1698
+ undo := DisablePlatformSources ()
1699
+ defer undo ()
1700
+ loc , err := LoadLocation ("Asia/Shanghai" )
1701
+ if err != nil {
1702
+ t .Fatal (err )
1703
+ }
1704
+
1705
+ // The ZoneBounds of a UTC location would just return two zero Time.
1706
+ for _ , test := range utctests {
1707
+ sec := test .seconds
1708
+ golden := & test .golden
1709
+ tm := Unix (sec , 0 ).UTC ()
1710
+ start , end := tm .ZoneBounds ()
1711
+ if ! (start .IsZero () && end .IsZero ()) {
1712
+ t .Errorf ("ZoneBounds of %+v expects two zero Time, got:\n start=%v\n end=%v" , * golden , start , end )
1713
+ }
1714
+ }
1715
+
1716
+ // If the zone begins at the beginning of time, start will be returned as a zero Time.
1717
+ // Use math.MinInt32 to avoid overflow of int arguments on 32-bit systems.
1718
+ beginTime := Date (math .MinInt32 , January , 1 , 0 , 0 , 0 , 0 , loc )
1719
+ start , end := beginTime .ZoneBounds ()
1720
+ if ! start .IsZero () || end .IsZero () {
1721
+ t .Errorf ("ZoneBounds of %v expects start is zero Time, got:\n start=%v\n end=%v" , beginTime , start , end )
1722
+ }
1723
+
1724
+ // If the zone goes on forever, end will be returned as a zero Time.
1725
+ // Use math.MaxInt32 to avoid overflow of int arguments on 32-bit systems.
1726
+ foreverTime := Date (math .MaxInt32 , January , 1 , 0 , 0 , 0 , 0 , loc )
1727
+ start , end = foreverTime .ZoneBounds ()
1728
+ if start .IsZero () || ! end .IsZero () {
1729
+ t .Errorf ("ZoneBounds of %v expects end is zero Time, got:\n start=%v\n end=%v" , foreverTime , start , end )
1730
+ }
1731
+
1732
+ // Check some real-world cases to make sure we're getting the right bounds.
1733
+ boundOne := Date (1990 , September , 16 , 1 , 0 , 0 , 0 , loc )
1734
+ boundTwo := Date (1991 , April , 14 , 3 , 0 , 0 , 0 , loc )
1735
+ boundThree := Date (1991 , September , 15 , 1 , 0 , 0 , 0 , loc )
1736
+ makeLocalTime := func (sec int64 ) Time { return Unix (sec , 0 ) }
1737
+ realTests := [... ]struct {
1738
+ giveTime Time
1739
+ wantStart Time
1740
+ wantEnd Time
1741
+ }{
1742
+ // The ZoneBounds of "Asia/Shanghai" Daylight Saving Time
1743
+ 0 : {Date (1991 , April , 13 , 17 , 50 , 0 , 0 , loc ), boundOne , boundTwo },
1744
+ 1 : {Date (1991 , April , 13 , 18 , 0 , 0 , 0 , loc ), boundOne , boundTwo },
1745
+ 2 : {Date (1991 , April , 14 , 1 , 50 , 0 , 0 , loc ), boundOne , boundTwo },
1746
+ 3 : {boundTwo , boundTwo , boundThree },
1747
+ 4 : {Date (1991 , September , 14 , 16 , 50 , 0 , 0 , loc ), boundTwo , boundThree },
1748
+ 5 : {Date (1991 , September , 14 , 17 , 0 , 0 , 0 , loc ), boundTwo , boundThree },
1749
+ 6 : {Date (1991 , September , 15 , 0 , 50 , 0 , 0 , loc ), boundTwo , boundThree },
1750
+
1751
+ // The ZoneBounds of a local time would return two local Time.
1752
+ // Note: We preloaded "America/Los_Angeles" as time.Local for testing
1753
+ 7 : {makeLocalTime (0 ), makeLocalTime (- 5756400 ), makeLocalTime (9972000 )},
1754
+ 8 : {makeLocalTime (1221681866 ), makeLocalTime (1205056800 ), makeLocalTime (1225616400 )},
1755
+ 9 : {makeLocalTime (2152173599 ), makeLocalTime (2145916800 ), makeLocalTime (2152173600 )},
1756
+ 10 : {makeLocalTime (2152173600 ), makeLocalTime (2152173600 ), makeLocalTime (2172733200 )},
1757
+ 11 : {makeLocalTime (2152173601 ), makeLocalTime (2152173600 ), makeLocalTime (2172733200 )},
1758
+ 12 : {makeLocalTime (2159200800 ), makeLocalTime (2152173600 ), makeLocalTime (2172733200 )},
1759
+ 13 : {makeLocalTime (2172733199 ), makeLocalTime (2152173600 ), makeLocalTime (2172733200 )},
1760
+ 14 : {makeLocalTime (2172733200 ), makeLocalTime (2172733200 ), makeLocalTime (2177452800 )},
1761
+ }
1762
+ for i , tt := range realTests {
1763
+ start , end := tt .giveTime .ZoneBounds ()
1764
+ if ! start .Equal (tt .wantStart ) || ! end .Equal (tt .wantEnd ) {
1765
+ t .Errorf ("#%d:: ZoneBounds of %v expects right bounds:\n got start=%v\n want start=%v\n got end=%v\n want end=%v" ,
1766
+ i , tt .giveTime , start , tt .wantStart , end , tt .wantEnd )
1767
+ }
1768
+ }
1769
+ }
0 commit comments