@@ -1571,19 +1571,20 @@ mod tests {
1571
1571
#[ track_caller]
1572
1572
fn single_test_roundtrip (
1573
1573
calendar : Ref < AnyCalendar > ,
1574
- era : Option < & str > ,
1574
+ era : Option < ( & str , Option < u8 > ) > ,
1575
1575
year : i32 ,
1576
1576
month_code : & str ,
1577
1577
day : u8 ,
1578
1578
) {
1579
1579
let month = types:: MonthCode ( month_code. parse ( ) . expect ( "month code must parse" ) ) ;
1580
1580
1581
- let date = Date :: try_new_from_codes ( era, year, month, day, calendar) . unwrap_or_else ( |e| {
1582
- panic ! (
1583
- "Failed to construct date for {} with {era:?}, {year}, {month}, {day}: {e:?}" ,
1584
- calendar. debug_name( ) ,
1585
- )
1586
- } ) ;
1581
+ let date = Date :: try_new_from_codes ( era. map ( |x| x. 0 ) , year, month, day, calendar)
1582
+ . unwrap_or_else ( |e| {
1583
+ panic ! (
1584
+ "Failed to construct date for {} with {era:?}, {year}, {month}, {day}: {e:?}" ,
1585
+ calendar. debug_name( ) ,
1586
+ )
1587
+ } ) ;
1587
1588
1588
1589
let roundtrip_year = date. year ( ) ;
1589
1590
// FIXME: these APIs should be improved
@@ -1598,6 +1599,19 @@ mod tests {
1598
1599
calendar. debug_name( )
1599
1600
) ;
1600
1601
1602
+ if let Some ( ( era_code, era_index) ) = era {
1603
+ let roundtrip_era_year = date. year ( ) . era ( ) . expect ( "year type should be era" ) ;
1604
+ assert_eq ! (
1605
+ ( era_code, era_index) ,
1606
+ (
1607
+ roundtrip_era_year. era. as_str( ) ,
1608
+ roundtrip_era_year. era_index
1609
+ ) ,
1610
+ "Failed to roundtrip era for calendar {}" ,
1611
+ calendar. debug_name( )
1612
+ )
1613
+ }
1614
+
1601
1615
let iso = date. to_iso ( ) ;
1602
1616
let reconstructed = Date :: new_from_iso ( iso, calendar) ;
1603
1617
assert_eq ! (
@@ -1609,15 +1623,15 @@ mod tests {
1609
1623
#[ track_caller]
1610
1624
fn single_test_error (
1611
1625
calendar : Ref < AnyCalendar > ,
1612
- era : Option < & str > ,
1626
+ era : Option < ( & str , Option < u8 > ) > ,
1613
1627
year : i32 ,
1614
1628
month_code : & str ,
1615
1629
day : u8 ,
1616
1630
error : DateError ,
1617
1631
) {
1618
1632
let month = types:: MonthCode ( month_code. parse ( ) . expect ( "month code must parse" ) ) ;
1619
1633
1620
- let date = Date :: try_new_from_codes ( era, year, month, day, calendar) ;
1634
+ let date = Date :: try_new_from_codes ( era. map ( |x| x . 0 ) , year, month, day, calendar) ;
1621
1635
assert_eq ! (
1622
1636
date,
1623
1637
Err ( error) ,
@@ -1663,38 +1677,38 @@ mod tests {
1663
1677
let persian = Ref ( & persian) ;
1664
1678
let roc = Ref ( & roc) ;
1665
1679
1666
- single_test_roundtrip ( buddhist, Some ( "be" ) , 100 , "M03" , 1 ) ;
1680
+ single_test_roundtrip ( buddhist, Some ( ( "be" , Some ( 0 ) ) ) , 100 , "M03" , 1 ) ;
1667
1681
single_test_roundtrip ( buddhist, None , 2000 , "M03" , 1 ) ;
1668
- single_test_roundtrip ( buddhist, Some ( "be" ) , -100 , "M03" , 1 ) ;
1682
+ single_test_roundtrip ( buddhist, Some ( ( "be" , Some ( 0 ) ) ) , -100 , "M03" , 1 ) ;
1669
1683
single_test_error (
1670
1684
buddhist,
1671
- Some ( "be" ) ,
1685
+ Some ( ( "be" , Some ( 0 ) ) ) ,
1672
1686
100 ,
1673
1687
"M13" ,
1674
1688
1 ,
1675
1689
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M13" ) ) ) ,
1676
1690
) ;
1677
1691
1678
- single_test_roundtrip ( coptic, Some ( "am" ) , 100 , "M03" , 1 ) ;
1692
+ single_test_roundtrip ( coptic, Some ( ( "am" , Some ( 0 ) ) ) , 100 , "M03" , 1 ) ;
1679
1693
single_test_roundtrip ( coptic, None , 2000 , "M03" , 1 ) ;
1680
- single_test_roundtrip ( coptic, Some ( "am" ) , -99 , "M03" , 1 ) ;
1681
- single_test_roundtrip ( coptic, Some ( "am" ) , 100 , "M13" , 1 ) ;
1694
+ single_test_roundtrip ( coptic, Some ( ( "am" , Some ( 0 ) ) ) , -99 , "M03" , 1 ) ;
1695
+ single_test_roundtrip ( coptic, Some ( ( "am" , Some ( 0 ) ) ) , 100 , "M13" , 1 ) ;
1682
1696
single_test_error (
1683
1697
coptic,
1684
- Some ( "am" ) ,
1698
+ Some ( ( "am" , Some ( 0 ) ) ) ,
1685
1699
100 ,
1686
1700
"M14" ,
1687
1701
1 ,
1688
1702
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M14" ) ) ) ,
1689
1703
) ;
1690
1704
1691
- single_test_roundtrip ( ethiopian, Some ( "am" ) , 100 , "M03" , 1 ) ;
1705
+ single_test_roundtrip ( ethiopian, Some ( ( "am" , Some ( 1 ) ) ) , 100 , "M03" , 1 ) ;
1692
1706
single_test_roundtrip ( ethiopian, None , 2000 , "M03" , 1 ) ;
1693
- single_test_roundtrip ( ethiopian, Some ( "am" ) , 2000 , "M13" , 1 ) ;
1694
- single_test_roundtrip ( ethiopian, Some ( "aa" ) , 5400 , "M03" , 1 ) ;
1707
+ single_test_roundtrip ( ethiopian, Some ( ( "am" , Some ( 1 ) ) ) , 2000 , "M13" , 1 ) ;
1708
+ single_test_roundtrip ( ethiopian, Some ( ( "aa" , Some ( 0 ) ) ) , 5400 , "M03" , 1 ) ;
1695
1709
single_test_error (
1696
1710
ethiopian,
1697
- Some ( "am" ) ,
1711
+ Some ( ( "am" , Some ( 0 ) ) ) ,
1698
1712
0 ,
1699
1713
"M03" ,
1700
1714
1 ,
@@ -1707,7 +1721,7 @@ mod tests {
1707
1721
) ;
1708
1722
single_test_error (
1709
1723
ethiopian,
1710
- Some ( "aa" ) ,
1724
+ Some ( ( "aa" , Some ( 0 ) ) ) ,
1711
1725
5600 ,
1712
1726
"M03" ,
1713
1727
1 ,
@@ -1720,31 +1734,31 @@ mod tests {
1720
1734
) ;
1721
1735
single_test_error (
1722
1736
ethiopian,
1723
- Some ( "am" ) ,
1737
+ Some ( ( "am" , Some ( 0 ) ) ) ,
1724
1738
100 ,
1725
1739
"M14" ,
1726
1740
1 ,
1727
1741
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M14" ) ) ) ,
1728
1742
) ;
1729
1743
1730
- single_test_roundtrip ( ethioaa, Some ( "aa" ) , 7000 , "M13" , 1 ) ;
1744
+ single_test_roundtrip ( ethioaa, Some ( ( "aa" , Some ( 0 ) ) ) , 7000 , "M13" , 1 ) ;
1731
1745
single_test_roundtrip ( ethioaa, None , 7000 , "M13" , 1 ) ;
1732
- single_test_roundtrip ( ethioaa, Some ( "aa" ) , 100 , "M03" , 1 ) ;
1746
+ single_test_roundtrip ( ethioaa, Some ( ( "aa" , Some ( 0 ) ) ) , 100 , "M03" , 1 ) ;
1733
1747
single_test_error (
1734
1748
ethiopian,
1735
- Some ( "aa" ) ,
1749
+ Some ( ( "aa" , Some ( 0 ) ) ) ,
1736
1750
100 ,
1737
1751
"M14" ,
1738
1752
1 ,
1739
1753
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M14" ) ) ) ,
1740
1754
) ;
1741
1755
1742
- single_test_roundtrip ( gregorian, Some ( "ce" ) , 100 , "M03" , 1 ) ;
1756
+ single_test_roundtrip ( gregorian, Some ( ( "ce" , Some ( 1 ) ) ) , 100 , "M03" , 1 ) ;
1743
1757
single_test_roundtrip ( gregorian, None , 2000 , "M03" , 1 ) ;
1744
- single_test_roundtrip ( gregorian, Some ( "bce" ) , 100 , "M03" , 1 ) ;
1758
+ single_test_roundtrip ( gregorian, Some ( ( "bce" , Some ( 0 ) ) ) , 100 , "M03" , 1 ) ;
1745
1759
single_test_error (
1746
1760
gregorian,
1747
- Some ( "ce" ) ,
1761
+ Some ( ( "ce" , Some ( 1 ) ) ) ,
1748
1762
0 ,
1749
1763
"M03" ,
1750
1764
1 ,
@@ -1757,7 +1771,7 @@ mod tests {
1757
1771
) ;
1758
1772
single_test_error (
1759
1773
gregorian,
1760
- Some ( "bce" ) ,
1774
+ Some ( ( "bce" , Some ( 0 ) ) ) ,
1761
1775
0 ,
1762
1776
"M03" ,
1763
1777
1 ,
@@ -1771,20 +1785,20 @@ mod tests {
1771
1785
1772
1786
single_test_error (
1773
1787
gregorian,
1774
- Some ( "bce" ) ,
1788
+ Some ( ( "bce" , Some ( 0 ) ) ) ,
1775
1789
100 ,
1776
1790
"M13" ,
1777
1791
1 ,
1778
1792
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M13" ) ) ) ,
1779
1793
) ;
1780
1794
1781
- single_test_roundtrip ( indian, Some ( "saka" ) , 100 , "M03" , 1 ) ;
1795
+ single_test_roundtrip ( indian, Some ( ( "saka" , Some ( 0 ) ) ) , 100 , "M03" , 1 ) ;
1782
1796
single_test_roundtrip ( indian, None , 2000 , "M12" , 1 ) ;
1783
1797
single_test_roundtrip ( indian, None , -100 , "M03" , 1 ) ;
1784
- single_test_roundtrip ( indian, Some ( "saka" ) , 0 , "M03" , 1 ) ;
1798
+ single_test_roundtrip ( indian, Some ( ( "saka" , Some ( 0 ) ) ) , 0 , "M03" , 1 ) ;
1785
1799
single_test_error (
1786
1800
indian,
1787
- Some ( "saka" ) ,
1801
+ Some ( ( "saka" , Some ( 0 ) ) ) ,
1788
1802
100 ,
1789
1803
"M13" ,
1790
1804
1 ,
@@ -1815,15 +1829,15 @@ mod tests {
1815
1829
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M00L" ) ) ) ,
1816
1830
) ;
1817
1831
1818
- single_test_roundtrip ( japanese, Some ( "reiwa" ) , 3 , "M03" , 1 ) ;
1819
- single_test_roundtrip ( japanese, Some ( "heisei" ) , 6 , "M12" , 1 ) ;
1820
- single_test_roundtrip ( japanese, Some ( "meiji" ) , 10 , "M03" , 1 ) ;
1821
- single_test_roundtrip ( japanese, Some ( "ce" ) , 1000 , "M03" , 1 ) ;
1832
+ single_test_roundtrip ( japanese, Some ( ( "reiwa" , None ) ) , 3 , "M03" , 1 ) ;
1833
+ single_test_roundtrip ( japanese, Some ( ( "heisei" , None ) ) , 6 , "M12" , 1 ) ;
1834
+ single_test_roundtrip ( japanese, Some ( ( "meiji" , None ) ) , 10 , "M03" , 1 ) ;
1835
+ single_test_roundtrip ( japanese, Some ( ( "ce" , None ) ) , 1000 , "M03" , 1 ) ;
1822
1836
single_test_roundtrip ( japanese, None , 1000 , "M03" , 1 ) ;
1823
- single_test_roundtrip ( japanese, Some ( "bce" ) , 10 , "M03" , 1 ) ;
1837
+ single_test_roundtrip ( japanese, Some ( ( "bce" , None ) ) , 10 , "M03" , 1 ) ;
1824
1838
single_test_error (
1825
1839
japanese,
1826
- Some ( "ce" ) ,
1840
+ Some ( ( "ce" , None ) ) ,
1827
1841
0 ,
1828
1842
"M03" ,
1829
1843
1 ,
@@ -1836,7 +1850,7 @@ mod tests {
1836
1850
) ;
1837
1851
single_test_error (
1838
1852
japanese,
1839
- Some ( "bce" ) ,
1853
+ Some ( ( "bce" , Some ( 0 ) ) ) ,
1840
1854
0 ,
1841
1855
"M03" ,
1842
1856
1 ,
@@ -1850,22 +1864,22 @@ mod tests {
1850
1864
1851
1865
single_test_error (
1852
1866
japanese,
1853
- Some ( "reiwa" ) ,
1867
+ Some ( ( "reiwa" , None ) ) ,
1854
1868
2 ,
1855
1869
"M13" ,
1856
1870
1 ,
1857
1871
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M13" ) ) ) ,
1858
1872
) ;
1859
1873
1860
- single_test_roundtrip ( japanext, Some ( "reiwa" ) , 3 , "M03" , 1 ) ;
1861
- single_test_roundtrip ( japanext, Some ( "heisei" ) , 6 , "M12" , 1 ) ;
1862
- single_test_roundtrip ( japanext, Some ( "meiji" ) , 10 , "M03" , 1 ) ;
1863
- single_test_roundtrip ( japanext, Some ( "tenpyokampo-749" ) , 1 , "M04" , 20 ) ;
1864
- single_test_roundtrip ( japanext, Some ( "ce" ) , 100 , "M03" , 1 ) ;
1865
- single_test_roundtrip ( japanext, Some ( "bce" ) , 10 , "M03" , 1 ) ;
1874
+ single_test_roundtrip ( japanext, Some ( ( "reiwa" , None ) ) , 3 , "M03" , 1 ) ;
1875
+ single_test_roundtrip ( japanext, Some ( ( "heisei" , None ) ) , 6 , "M12" , 1 ) ;
1876
+ single_test_roundtrip ( japanext, Some ( ( "meiji" , None ) ) , 10 , "M03" , 1 ) ;
1877
+ single_test_roundtrip ( japanext, Some ( ( "tenpyokampo-749" , None ) ) , 1 , "M04" , 20 ) ;
1878
+ single_test_roundtrip ( japanext, Some ( ( "ce" , None ) ) , 100 , "M03" , 1 ) ;
1879
+ single_test_roundtrip ( japanext, Some ( ( "bce" , None ) ) , 10 , "M03" , 1 ) ;
1866
1880
single_test_error (
1867
1881
japanext,
1868
- Some ( "ce" ) ,
1882
+ Some ( ( "ce" , None ) ) ,
1869
1883
0 ,
1870
1884
"M03" ,
1871
1885
1 ,
@@ -1878,7 +1892,7 @@ mod tests {
1878
1892
) ;
1879
1893
single_test_error (
1880
1894
japanext,
1881
- Some ( "bce" ) ,
1895
+ Some ( ( "bce" , Some ( 0 ) ) ) ,
1882
1896
0 ,
1883
1897
"M03" ,
1884
1898
1 ,
@@ -1892,83 +1906,83 @@ mod tests {
1892
1906
1893
1907
single_test_error (
1894
1908
japanext,
1895
- Some ( "reiwa" ) ,
1909
+ Some ( ( "reiwa" , None ) ) ,
1896
1910
2 ,
1897
1911
"M13" ,
1898
1912
1 ,
1899
1913
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M13" ) ) ) ,
1900
1914
) ;
1901
1915
1902
- single_test_roundtrip ( persian, Some ( "ap" ) , 477 , "M03" , 1 ) ;
1916
+ single_test_roundtrip ( persian, Some ( ( "ap" , Some ( 0 ) ) ) , 477 , "M03" , 1 ) ;
1903
1917
single_test_roundtrip ( persian, None , 2083 , "M07" , 21 ) ;
1904
- single_test_roundtrip ( persian, Some ( "ap" ) , 1600 , "M12" , 20 ) ;
1918
+ single_test_roundtrip ( persian, Some ( ( "ap" , Some ( 0 ) ) ) , 1600 , "M12" , 20 ) ;
1905
1919
single_test_error (
1906
1920
persian,
1907
- Some ( "ap" ) ,
1921
+ Some ( ( "ap" , Some ( 0 ) ) ) ,
1908
1922
100 ,
1909
1923
"M9" ,
1910
1924
1 ,
1911
1925
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M9" ) ) ) ,
1912
1926
) ;
1913
1927
1914
- single_test_roundtrip ( hebrew, Some ( "am" ) , 5773 , "M03" , 1 ) ;
1928
+ single_test_roundtrip ( hebrew, Some ( ( "am" , Some ( 0 ) ) ) , 5773 , "M03" , 1 ) ;
1915
1929
single_test_roundtrip ( hebrew, None , 4993 , "M07" , 21 ) ;
1916
- single_test_roundtrip ( hebrew, Some ( "am" ) , 5012 , "M12" , 20 ) ;
1930
+ single_test_roundtrip ( hebrew, Some ( ( "am" , Some ( 0 ) ) ) , 5012 , "M12" , 20 ) ;
1917
1931
single_test_error (
1918
1932
hebrew,
1919
- Some ( "am" ) ,
1933
+ Some ( ( "am" , Some ( 0 ) ) ) ,
1920
1934
100 ,
1921
1935
"M9" ,
1922
1936
1 ,
1923
1937
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M9" ) ) ) ,
1924
1938
) ;
1925
1939
1926
- single_test_roundtrip ( roc, Some ( "minguo" ) , 10 , "M05" , 3 ) ;
1927
- single_test_roundtrip ( roc, Some ( "minguo-qian" ) , 15 , "M01" , 10 ) ;
1940
+ single_test_roundtrip ( roc, Some ( ( "minguo" , Some ( 1 ) ) ) , 10 , "M05" , 3 ) ;
1941
+ single_test_roundtrip ( roc, Some ( ( "minguo-qian" , Some ( 0 ) ) ) , 15 , "M01" , 10 ) ;
1928
1942
single_test_roundtrip ( roc, None , 100 , "M10" , 30 ) ;
1929
1943
1930
- single_test_roundtrip ( hijri_simulated, Some ( "ah" ) , 477 , "M03" , 1 ) ;
1944
+ single_test_roundtrip ( hijri_simulated, Some ( ( "ah" , Some ( 0 ) ) ) , 477 , "M03" , 1 ) ;
1931
1945
single_test_roundtrip ( hijri_simulated, None , 2083 , "M07" , 21 ) ;
1932
- single_test_roundtrip ( hijri_simulated, Some ( "ah" ) , 1600 , "M12" , 20 ) ;
1946
+ single_test_roundtrip ( hijri_simulated, Some ( ( "ah" , Some ( 0 ) ) ) , 1600 , "M12" , 20 ) ;
1933
1947
single_test_error (
1934
1948
hijri_simulated,
1935
- Some ( "ah" ) ,
1949
+ Some ( ( "ah" , Some ( 0 ) ) ) ,
1936
1950
100 ,
1937
1951
"M9" ,
1938
1952
1 ,
1939
1953
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M9" ) ) ) ,
1940
1954
) ;
1941
1955
1942
- single_test_roundtrip ( hijri_civil, Some ( "ah" ) , 477 , "M03" , 1 ) ;
1956
+ single_test_roundtrip ( hijri_civil, Some ( ( "ah" , Some ( 0 ) ) ) , 477 , "M03" , 1 ) ;
1943
1957
single_test_roundtrip ( hijri_civil, None , 2083 , "M07" , 21 ) ;
1944
- single_test_roundtrip ( hijri_civil, Some ( "ah" ) , 1600 , "M12" , 20 ) ;
1958
+ single_test_roundtrip ( hijri_civil, Some ( ( "ah" , Some ( 0 ) ) ) , 1600 , "M12" , 20 ) ;
1945
1959
single_test_error (
1946
1960
hijri_civil,
1947
- Some ( "ah" ) ,
1961
+ Some ( ( "ah" , Some ( 0 ) ) ) ,
1948
1962
100 ,
1949
1963
"M9" ,
1950
1964
1 ,
1951
1965
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M9" ) ) ) ,
1952
1966
) ;
1953
1967
1954
- single_test_roundtrip ( hijri_umm_al_qura, Some ( "ah" ) , 477 , "M03" , 1 ) ;
1968
+ single_test_roundtrip ( hijri_umm_al_qura, Some ( ( "ah" , Some ( 0 ) ) ) , 477 , "M03" , 1 ) ;
1955
1969
single_test_roundtrip ( hijri_umm_al_qura, None , 2083 , "M07" , 21 ) ;
1956
- single_test_roundtrip ( hijri_umm_al_qura, Some ( "ah" ) , 1600 , "M12" , 20 ) ;
1970
+ single_test_roundtrip ( hijri_umm_al_qura, Some ( ( "ah" , Some ( 0 ) ) ) , 1600 , "M12" , 20 ) ;
1957
1971
single_test_error (
1958
1972
hijri_umm_al_qura,
1959
- Some ( "ah" ) ,
1973
+ Some ( ( "ah" , Some ( 0 ) ) ) ,
1960
1974
100 ,
1961
1975
"M9" ,
1962
1976
1 ,
1963
1977
DateError :: UnknownMonthCode ( MonthCode ( tinystr ! ( 4 , "M9" ) ) ) ,
1964
1978
) ;
1965
1979
1966
- single_test_roundtrip ( hijri_astronomical, Some ( "ah" ) , 477 , "M03" , 1 ) ;
1980
+ single_test_roundtrip ( hijri_astronomical, Some ( ( "ah" , Some ( 0 ) ) ) , 477 , "M03" , 1 ) ;
1967
1981
single_test_roundtrip ( hijri_astronomical, None , 2083 , "M07" , 21 ) ;
1968
- single_test_roundtrip ( hijri_astronomical, Some ( "ah" ) , 1600 , "M12" , 20 ) ;
1982
+ single_test_roundtrip ( hijri_astronomical, Some ( ( "ah" , Some ( 0 ) ) ) , 1600 , "M12" , 20 ) ;
1969
1983
single_test_error (
1970
1984
hijri_astronomical,
1971
- Some ( "ah" ) ,
1985
+ Some ( ( "ah" , Some ( 0 ) ) ) ,
1972
1986
100 ,
1973
1987
"M9" ,
1974
1988
1 ,
0 commit comments