jiff/civil/time.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463
use core::time::Duration as UnsignedDuration;
use crate::{
civil::{Date, DateTime},
duration::{Duration, SDuration},
error::{err, Error, ErrorContext},
fmt::{
self,
temporal::{self, DEFAULT_DATETIME_PARSER},
},
util::{
common::{from_day_nanosecond, to_day_nanosecond},
rangeint::{RFrom, RInto, TryRFrom},
round::increment,
t::{
self, CivilDayNanosecond, Hour, Microsecond, Millisecond, Minute,
Nanosecond, Second, SubsecNanosecond, C,
},
},
RoundMode, SignedDuration, Span, SpanRound, Unit, Zoned,
};
/// A representation of civil "wall clock" time.
///
/// Conceptually, a `Time` value corresponds to the typical hours and minutes
/// that you might see on a clock. This type also contains the second and
/// fractional subsecond (to nanosecond precision) associated with a time.
///
/// # Civil time
///
/// A `Time` value behaves as if it corresponds precisely to a single
/// nanosecond within a day, where all days have `86,400` seconds. That is,
/// any given `Time` value corresponds to a nanosecond in the inclusive range
/// `[0, 86399999999999]`, where `0` corresponds to `00:00:00.000000000`
/// ([`Time::MIN`]) and `86399999999999` corresponds to `23:59:59.999999999`
/// ([`Time::MAX`]). Moreover, in civil time, all hours have the same number of
/// minutes, all minutes have the same number of seconds and all seconds have
/// the same number of nanoseconds.
///
/// # Parsing and printing
///
/// The `Time` type provides convenient trait implementations of
/// [`std::str::FromStr`] and [`std::fmt::Display`]:
///
/// ```
/// use jiff::civil::Time;
///
/// let t: Time = "15:22:45".parse()?;
/// assert_eq!(t.to_string(), "15:22:45");
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// A civil `Time` can also be parsed from something that _contains_ a
/// time, but with perhaps other data (such as an offset or time zone):
///
/// ```
/// use jiff::civil::Time;
///
/// let t: Time = "2024-06-19T15:22:45-04[America/New_York]".parse()?;
/// assert_eq!(t.to_string(), "15:22:45");
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// For more information on the specific format supported, see the
/// [`fmt::temporal`](crate::fmt::temporal) module documentation.
///
/// # Default value
///
/// For convenience, this type implements the `Default` trait. Its default
/// value is midnight. i.e., `00:00:00.000000000`.
///
/// # Leap seconds
///
/// Jiff does not support leap seconds. Jiff behaves as if they don't exist.
/// The only exception is that if one parses a time with a second component
/// of `60`, then it is automatically constrained to `59`:
///
/// ```
/// use jiff::civil::{Time, time};
///
/// let t: Time = "23:59:60".parse()?;
/// assert_eq!(t, time(23, 59, 59, 0));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Comparisons
///
/// The `Time` type provides both `Eq` and `Ord` trait implementations to
/// facilitate easy comparisons. When a time `t1` occurs before a time `t2`,
/// then `t1 < t2`. For example:
///
/// ```
/// use jiff::civil::time;
///
/// let t1 = time(7, 30, 1, 0);
/// let t2 = time(8, 10, 0, 0);
/// assert!(t1 < t2);
/// ```
///
/// As mentioned above, `Time` values are not associated with timezones, and
/// thus transitions such as DST are not taken into account when comparing
/// `Time` values.
///
/// # Arithmetic
///
/// This type provides routines for adding and subtracting spans of time, as
/// well as computing the span of time between two `Time` values.
///
/// For adding or subtracting spans of time, one can use any of the following
/// routines:
///
/// * [`Time::wrapping_add`] or [`Time::wrapping_sub`] for wrapping arithmetic.
/// * [`Time::checked_add`] or [`Time::checked_sub`] for checked arithmetic.
/// * [`Time::saturating_add`] or [`Time::saturating_sub`] for saturating
/// arithmetic.
///
/// Additionally, wrapping arithmetic is available via the `Add` and `Sub`
/// trait implementations:
///
/// ```
/// use jiff::{civil::time, ToSpan};
///
/// let t = time(20, 10, 1, 0);
/// let span = 1.hours().minutes(49).seconds(59);
/// assert_eq!(t + span, time(22, 0, 0, 0));
///
/// // Overflow will result in wrap-around unless using checked
/// // arithmetic explicitly.
/// let t = time(23, 59, 59, 999_999_999);
/// assert_eq!(time(0, 0, 0, 0), t + 1.nanoseconds());
/// ```
///
/// Wrapping arithmetic is used by default because it corresponds to how clocks
/// showing the time of day behave in practice.
///
/// One can compute the span of time between two times using either
/// [`Time::until`] or [`Time::since`]. It's also possible to subtract two
/// `Time` values directly via a `Sub` trait implementation:
///
/// ```
/// use jiff::{civil::time, ToSpan};
///
/// let time1 = time(22, 0, 0, 0);
/// let time2 = time(20, 10, 1, 0);
/// assert_eq!(
/// time1 - time2,
/// 1.hours().minutes(49).seconds(59).fieldwise(),
/// );
/// ```
///
/// The `until` and `since` APIs are polymorphic and allow re-balancing and
/// rounding the span returned. For example, the default largest unit is hours
/// (as exemplified above), but we can ask for smaller units:
///
/// ```
/// use jiff::{civil::time, ToSpan, Unit};
///
/// let time1 = time(23, 30, 0, 0);
/// let time2 = time(7, 0, 0, 0);
/// assert_eq!(
/// time1.since((Unit::Minute, time2))?,
/// 990.minutes().fieldwise(),
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// Or even round the span returned:
///
/// ```
/// use jiff::{civil::{TimeDifference, time}, RoundMode, ToSpan, Unit};
///
/// let time1 = time(23, 30, 0, 0);
/// let time2 = time(23, 35, 59, 0);
/// assert_eq!(
/// time1.until(
/// TimeDifference::new(time2).smallest(Unit::Minute),
/// )?,
/// 5.minutes().fieldwise(),
/// );
/// // `TimeDifference` uses truncation as a rounding mode by default,
/// // but you can set the rounding mode to break ties away from zero:
/// assert_eq!(
/// time1.until(
/// TimeDifference::new(time2)
/// .smallest(Unit::Minute)
/// .mode(RoundMode::HalfExpand),
/// )?,
/// // Rounds up to 6 minutes.
/// 6.minutes().fieldwise(),
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Rounding
///
/// A `Time` can be rounded based on a [`TimeRound`] configuration of smallest
/// units, rounding increment and rounding mode. Here's an example showing how
/// to round to the nearest third hour:
///
/// ```
/// use jiff::{civil::{TimeRound, time}, Unit};
///
/// let t = time(16, 27, 29, 999_999_999);
/// assert_eq!(
/// t.round(TimeRound::new().smallest(Unit::Hour).increment(3))?,
/// time(15, 0, 0, 0),
/// );
/// // Or alternatively, make use of the `From<(Unit, i64)> for TimeRound`
/// // trait implementation:
/// assert_eq!(t.round((Unit::Hour, 3))?, time(15, 0, 0, 0));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// See [`Time::round`] for more details.
#[derive(Clone, Copy, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Time {
hour: Hour,
minute: Minute,
second: Second,
subsec_nanosecond: SubsecNanosecond,
}
impl Time {
/// The minimum representable time value.
///
/// This corresponds to `00:00:00.000000000`.
pub const MIN: Time = Time::midnight();
/// The maximum representable time value.
///
/// This corresponds to `23:59:59.999999999`.
pub const MAX: Time = Time::constant(23, 59, 59, 999_999_999);
/// Creates a new `Time` value from its component hour, minute, second and
/// fractional subsecond (up to nanosecond precision) values.
///
/// To set the component values of a time after creating it, use
/// [`TimeWith`] via [`Time::with`] to build a new [`Time`] from the fields
/// of an existing time.
///
/// # Errors
///
/// This returns an error unless *all* of the following conditions are
/// true:
///
/// * `0 <= hour <= 23`
/// * `0 <= minute <= 59`
/// * `0 <= second <= 59`
/// * `0 <= subsec_nanosecond <= 999,999,999`
///
/// # Example
///
/// This shows an example of a valid time:
///
/// ```
/// use jiff::civil::Time;
///
/// let t = Time::new(21, 30, 5, 123_456_789).unwrap();
/// assert_eq!(t.hour(), 21);
/// assert_eq!(t.minute(), 30);
/// assert_eq!(t.second(), 5);
/// assert_eq!(t.millisecond(), 123);
/// assert_eq!(t.microsecond(), 456);
/// assert_eq!(t.nanosecond(), 789);
/// ```
///
/// This shows an example of an invalid time:
///
/// ```
/// use jiff::civil::Time;
///
/// assert!(Time::new(21, 30, 60, 0).is_err());
/// ```
#[inline]
pub fn new(
hour: i8,
minute: i8,
second: i8,
subsec_nanosecond: i32,
) -> Result<Time, Error> {
let hour = Hour::try_new("hour", hour)?;
let minute = Minute::try_new("minute", minute)?;
let second = Second::try_new("second", second)?;
let subsec_nanosecond =
SubsecNanosecond::try_new("subsec_nanosecond", subsec_nanosecond)?;
Ok(Time::new_ranged(hour, minute, second, subsec_nanosecond))
}
/// Creates a new `Time` value in a `const` context.
///
/// # Panics
///
/// This panics if the given values do not correspond to a valid `Time`.
/// All of the following conditions must be true:
///
/// * `0 <= hour <= 23`
/// * `0 <= minute <= 59`
/// * `0 <= second <= 59`
/// * `0 <= subsec_nanosecond <= 999,999,999`
///
/// Similarly, when used in a const context, invalid parameters will
/// prevent your Rust program from compiling.
///
/// # Example
///
/// This shows an example of a valid time in a `const` context:
///
/// ```
/// use jiff::civil::Time;
///
/// const BEDTIME: Time = Time::constant(21, 30, 5, 123_456_789);
/// assert_eq!(BEDTIME.hour(), 21);
/// assert_eq!(BEDTIME.minute(), 30);
/// assert_eq!(BEDTIME.second(), 5);
/// assert_eq!(BEDTIME.millisecond(), 123);
/// assert_eq!(BEDTIME.microsecond(), 456);
/// assert_eq!(BEDTIME.nanosecond(), 789);
/// assert_eq!(BEDTIME.subsec_nanosecond(), 123_456_789);
/// ```
#[inline]
pub const fn constant(
hour: i8,
minute: i8,
second: i8,
subsec_nanosecond: i32,
) -> Time {
if !Hour::contains(hour) {
panic!("invalid hour");
}
if !Minute::contains(minute) {
panic!("invalid minute");
}
if !Second::contains(second) {
panic!("invalid second");
}
if !SubsecNanosecond::contains(subsec_nanosecond) {
panic!("invalid nanosecond");
}
let hour = Hour::new_unchecked(hour);
let minute = Minute::new_unchecked(minute);
let second = Second::new_unchecked(second);
let subsec_nanosecond =
SubsecNanosecond::new_unchecked(subsec_nanosecond);
Time { hour, minute, second, subsec_nanosecond }
}
/// Returns the first moment of time in a day.
///
/// Specifically, this has the `hour`, `minute`, `second`, `millisecond`,
/// `microsecond` and `nanosecond` fields all set to `0`.
///
/// # Example
///
/// ```
/// use jiff::civil::Time;
///
/// let t = Time::midnight();
/// assert_eq!(t.hour(), 0);
/// assert_eq!(t.minute(), 0);
/// assert_eq!(t.second(), 0);
/// assert_eq!(t.millisecond(), 0);
/// assert_eq!(t.microsecond(), 0);
/// assert_eq!(t.nanosecond(), 0);
/// ```
#[inline]
pub const fn midnight() -> Time {
Time::constant(0, 0, 0, 0)
}
/// Create a builder for constructing a `Time` from the fields of this
/// time.
///
/// See the methods on [`TimeWith`] for the different ways one can set the
/// fields of a new `Time`.
///
/// # Example
///
/// Unlike [`Date`], a [`Time`] is valid for all possible valid values
/// of its fields. That is, there is no way for two valid field values
/// to combine into an invalid `Time`. So, for `Time`, this builder does
/// have as much of a benefit versus an API design with methods like
/// `Time::with_hour` and `Time::with_minute`. Nevertheless, this builder
/// permits settings multiple fields at the same time and performing only
/// one validity check. Moreover, this provides a consistent API with other
/// date and time types in this crate.
///
/// ```
/// use jiff::civil::time;
///
/// let t1 = time(0, 0, 24, 0);
/// let t2 = t1.with().hour(15).minute(30).millisecond(10).build()?;
/// assert_eq!(t2, time(15, 30, 24, 10_000_000));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn with(self) -> TimeWith {
TimeWith::new(self)
}
/// Returns the "hour" component of this time.
///
/// The value returned is guaranteed to be in the range `0..=23`.
///
/// # Example
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(13, 35, 56, 123_456_789);
/// assert_eq!(t.hour(), 13);
/// ```
#[inline]
pub fn hour(self) -> i8 {
self.hour_ranged().get()
}
/// Returns the "minute" component of this time.
///
/// The value returned is guaranteed to be in the range `0..=59`.
///
/// # Example
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(13, 35, 56, 123_456_789);
/// assert_eq!(t.minute(), 35);
/// ```
#[inline]
pub fn minute(self) -> i8 {
self.minute_ranged().get()
}
/// Returns the "second" component of this time.
///
/// The value returned is guaranteed to be in the range `0..=59`.
///
/// # Example
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(13, 35, 56, 123_456_789);
/// assert_eq!(t.second(), 56);
/// ```
#[inline]
pub fn second(self) -> i8 {
self.second_ranged().get()
}
/// Returns the "millisecond" component of this time.
///
/// The value returned is guaranteed to be in the range `0..=999`.
///
/// # Example
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(13, 35, 56, 123_456_789);
/// assert_eq!(t.millisecond(), 123);
/// ```
#[inline]
pub fn millisecond(self) -> i16 {
self.millisecond_ranged().get()
}
/// Returns the "microsecond" component of this time.
///
/// The value returned is guaranteed to be in the range `0..=999`.
///
/// # Example
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(13, 35, 56, 123_456_789);
/// assert_eq!(t.microsecond(), 456);
/// ```
#[inline]
pub fn microsecond(self) -> i16 {
self.microsecond_ranged().get()
}
/// Returns the "nanosecond" component of this time.
///
/// The value returned is guaranteed to be in the range `0..=999`.
///
/// # Example
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(13, 35, 56, 123_456_789);
/// assert_eq!(t.nanosecond(), 789);
/// ```
#[inline]
pub fn nanosecond(self) -> i16 {
self.nanosecond_ranged().get()
}
/// Returns the fractional nanosecond for this `Time` value.
///
/// If you want to set this value on `Time`, then use
/// [`TimeWith::subsec_nanosecond`] via [`Time::with`].
///
/// The value returned is guaranteed to be in the range `0..=999_999_999`.
///
/// # Example
///
/// This shows the relationship between constructing a `Time` value
/// with routines like `with().millisecond()` and accessing the entire
/// fractional part as a nanosecond:
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(15, 21, 35, 0).with().millisecond(987).build()?;
/// assert_eq!(t.subsec_nanosecond(), 987_000_000);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: nanoseconds from a timestamp
///
/// This shows how the fractional nanosecond part of a `Time` value
/// manifests from a specific timestamp.
///
/// ```
/// use jiff::{civil, Timestamp};
///
/// // 1,234 nanoseconds after the Unix epoch.
/// let zdt = Timestamp::new(0, 1_234)?.in_tz("UTC")?;
/// let time = zdt.datetime().time();
/// assert_eq!(time.subsec_nanosecond(), 1_234);
///
/// // 1,234 nanoseconds before the Unix epoch.
/// let zdt = Timestamp::new(0, -1_234)?.in_tz("UTC")?;
/// let time = zdt.datetime().time();
/// // The nanosecond is equal to `1_000_000_000 - 1_234`.
/// assert_eq!(time.subsec_nanosecond(), 999998766);
/// // Looking at the other components of the time value might help.
/// assert_eq!(time.hour(), 23);
/// assert_eq!(time.minute(), 59);
/// assert_eq!(time.second(), 59);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn subsec_nanosecond(self) -> i32 {
self.subsec_nanosecond_ranged().get()
}
/// Given a [`Date`], this constructs a [`DateTime`] value with its time
/// component equal to this time.
///
/// This is a convenience function for [`DateTime::from_parts`].
///
/// # Example
///
/// ```
/// use jiff::civil::{DateTime, date, time};
///
/// let d = date(2010, 3, 14);
/// let t = time(2, 30, 0, 0);
/// assert_eq!(DateTime::from_parts(d, t), t.to_datetime(d));
/// ```
#[inline]
pub const fn to_datetime(self, date: Date) -> DateTime {
DateTime::from_parts(date, self)
}
/// A convenience function for constructing a [`DateTime`] from this time
/// on the date given by its components.
///
/// # Example
///
/// ```
/// use jiff::civil::time;
///
/// assert_eq!(
/// time(2, 30, 0, 0).on(2010, 3, 14).to_string(),
/// "2010-03-14T02:30:00",
/// );
/// ```
///
/// One can also flip the order by making use of [`Date::at`]:
///
/// ```
/// use jiff::civil::date;
///
/// assert_eq!(
/// date(2010, 3, 14).at(2, 30, 0, 0).to_string(),
/// "2010-03-14T02:30:00",
/// );
/// ```
#[inline]
pub const fn on(self, year: i16, month: i8, day: i8) -> DateTime {
DateTime::from_parts(Date::constant(year, month, day), self)
}
/// Add the given span to this time and wrap around on overflow.
///
/// This operation accepts three different duration types: [`Span`],
/// [`SignedDuration`] or [`std::time::Duration`]. This is achieved via
/// `From` trait implementations for the [`TimeArithmetic`] type.
///
/// # Properties
///
/// Given times `t1` and `t2`, and a span `s`, with `t2 = t1 + s`, it
/// follows then that `t1 = t2 - s` for all values of `t1` and `s` that sum
/// to `t2`.
///
/// In short, subtracting the given span from the sum returned by this
/// function is guaranteed to result in precisely the original time.
///
/// # Example: available via addition operator
///
/// This routine can be used via the `+` operator.
///
/// ```
/// use jiff::{civil::time, ToSpan};
///
/// let t = time(20, 10, 1, 0);
/// assert_eq!(
/// t + 1.hours().minutes(49).seconds(59),
/// time(22, 0, 0, 0),
/// );
/// ```
///
/// # Example: add nanoseconds to a `Time`
///
/// ```
/// use jiff::{civil::time, ToSpan};
///
/// let t = time(22, 35, 1, 0);
/// assert_eq!(
/// time(22, 35, 3, 500_000_000),
/// t.wrapping_add(2_500_000_000i64.nanoseconds()),
/// );
/// ```
///
/// # Example: add span with multiple units
///
/// ```
/// use jiff::{civil::time, ToSpan};
///
/// let t = time(20, 10, 1, 0);
/// assert_eq!(
/// time(22, 0, 0, 0),
/// t.wrapping_add(1.hours().minutes(49).seconds(59)),
/// );
/// ```
///
/// # Example: adding an empty span is a no-op
///
/// ```
/// use jiff::{civil::time, Span};
///
/// let t = time(20, 10, 1, 0);
/// assert_eq!(t, t.wrapping_add(Span::new()));
/// ```
///
/// # Example: addition wraps on overflow
///
/// ```
/// use jiff::{civil::time, SignedDuration, ToSpan};
///
/// let t = time(23, 59, 59, 999_999_999);
/// assert_eq!(
/// t.wrapping_add(1.nanoseconds()),
/// time(0, 0, 0, 0),
/// );
/// assert_eq!(
/// t.wrapping_add(SignedDuration::from_nanos(1)),
/// time(0, 0, 0, 0),
/// );
/// assert_eq!(
/// t.wrapping_add(std::time::Duration::from_nanos(1)),
/// time(0, 0, 0, 0),
/// );
/// ```
///
/// Similarly, if there are any non-zero units greater than hours in the
/// given span, then they also result in wrapping behavior (i.e., they are
/// ignored):
///
/// ```
/// use jiff::{civil::time, ToSpan};
///
/// // doesn't matter what our time value is in this example
/// let t = time(0, 0, 0, 0);
/// assert_eq!(t, t.wrapping_add(1.days()));
/// ```
#[inline]
pub fn wrapping_add<A: Into<TimeArithmetic>>(self, duration: A) -> Time {
let duration: TimeArithmetic = duration.into();
duration.wrapping_add(self)
}
#[inline]
fn wrapping_add_span(self, span: Span) -> Time {
let mut sum = self.to_nanosecond().without_bounds();
sum = sum.wrapping_add(
span.get_hours_ranged()
.without_bounds()
.wrapping_mul(t::NANOS_PER_HOUR),
);
sum = sum.wrapping_add(
span.get_minutes_ranged()
.without_bounds()
.wrapping_mul(t::NANOS_PER_MINUTE),
);
sum = sum.wrapping_add(
span.get_seconds_ranged()
.without_bounds()
.wrapping_mul(t::NANOS_PER_SECOND),
);
sum = sum.wrapping_add(
span.get_milliseconds_ranged()
.without_bounds()
.wrapping_mul(t::NANOS_PER_MILLI),
);
sum = sum.wrapping_add(
span.get_microseconds_ranged()
.without_bounds()
.wrapping_mul(t::NANOS_PER_MICRO),
);
sum = sum.wrapping_add(span.get_nanoseconds_ranged().without_bounds());
let civil_day_nanosecond = sum % t::NANOS_PER_CIVIL_DAY;
Time::from_nanosecond(civil_day_nanosecond.rinto())
}
#[inline]
fn wrapping_add_signed_duration(self, duration: SignedDuration) -> Time {
let start = t::NoUnits128::rfrom(self.to_nanosecond());
let duration = t::NoUnits128::new_unchecked(duration.as_nanos());
let end = start.wrapping_add(duration) % t::NANOS_PER_CIVIL_DAY;
Time::from_nanosecond(end.rinto())
}
#[inline]
fn wrapping_add_unsigned_duration(
self,
duration: UnsignedDuration,
) -> Time {
let start = t::NoUnits128::rfrom(self.to_nanosecond());
// OK because 96-bit unsigned integer can't overflow i128.
let duration = i128::try_from(duration.as_nanos()).unwrap();
let duration = t::NoUnits128::new_unchecked(duration);
let duration = duration % t::NANOS_PER_CIVIL_DAY;
let end = start.wrapping_add(duration) % t::NANOS_PER_CIVIL_DAY;
Time::from_nanosecond(end.rinto())
}
/// This routine is identical to [`Time::wrapping_add`] with the duration
/// negated.
///
/// # Example
///
/// ```
/// use jiff::{civil::time, SignedDuration, ToSpan};
///
/// let t = time(0, 0, 0, 0);
/// assert_eq!(
/// t.wrapping_sub(1.nanoseconds()),
/// time(23, 59, 59, 999_999_999),
/// );
/// assert_eq!(
/// t.wrapping_sub(SignedDuration::from_nanos(1)),
/// time(23, 59, 59, 999_999_999),
/// );
/// assert_eq!(
/// t.wrapping_sub(std::time::Duration::from_nanos(1)),
/// time(23, 59, 59, 999_999_999),
/// );
///
/// assert_eq!(
/// t.wrapping_sub(SignedDuration::MIN),
/// time(15, 30, 8, 999_999_999),
/// );
/// assert_eq!(
/// t.wrapping_sub(SignedDuration::MAX),
/// time(8, 29, 52, 1),
/// );
/// assert_eq!(
/// t.wrapping_sub(std::time::Duration::MAX),
/// time(16, 59, 44, 1),
/// );
/// ```
#[inline]
pub fn wrapping_sub<A: Into<TimeArithmetic>>(self, duration: A) -> Time {
let duration: TimeArithmetic = duration.into();
duration.wrapping_sub(self)
}
#[inline]
fn wrapping_sub_unsigned_duration(
self,
duration: UnsignedDuration,
) -> Time {
let start = t::NoUnits128::rfrom(self.to_nanosecond());
// OK because 96-bit unsigned integer can't overflow i128.
let duration = i128::try_from(duration.as_nanos()).unwrap();
let duration = t::NoUnits128::new_unchecked(duration);
let end = start.wrapping_sub(duration) % t::NANOS_PER_CIVIL_DAY;
Time::from_nanosecond(end.rinto())
}
/// Add the given span to this time and return an error if the result would
/// otherwise overflow.
///
/// This operation accepts three different duration types: [`Span`],
/// [`SignedDuration`] or [`std::time::Duration`]. This is achieved via
/// `From` trait implementations for the [`TimeArithmetic`] type.
///
/// # Properties
///
/// Given a time `t1` and a span `s`, and assuming `t2 = t1 + s` exists, it
/// follows then that `t1 = t2 - s` for all values of `t1` and `s` that sum
/// to a valid `t2`.
///
/// In short, subtracting the given span from the sum returned by this
/// function is guaranteed to result in precisely the original time.
///
/// # Errors
///
/// If the sum would overflow the minimum or maximum timestamp values, then
/// an error is returned.
///
/// If the given span has any non-zero units greater than hours, then an
/// error is returned.
///
/// # Example: add nanoseconds to a `Time`
///
/// ```
/// use jiff::{civil::time, ToSpan};
///
/// let t = time(22, 35, 1, 0);
/// assert_eq!(
/// time(22, 35, 3, 500_000_000),
/// t.checked_add(2_500_000_000i64.nanoseconds())?,
/// );
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: add span with multiple units
///
/// ```
/// use jiff::{civil::time, ToSpan};
///
/// let t = time(20, 10, 1, 0);
/// assert_eq!(
/// time(22, 0, 0, 0),
/// t.checked_add(1.hours().minutes(49).seconds(59))?,
/// );
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: adding an empty span is a no-op
///
/// ```
/// use jiff::{civil::time, Span};
///
/// let t = time(20, 10, 1, 0);
/// assert_eq!(t, t.checked_add(Span::new())?);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: error on overflow
///
/// ```
/// use jiff::{civil::time, ToSpan};
///
/// // okay
/// let t = time(23, 59, 59, 999_999_998);
/// assert_eq!(
/// t.with().nanosecond(999).build()?,
/// t.checked_add(1.nanoseconds())?,
/// );
///
/// // not okay
/// let t = time(23, 59, 59, 999_999_999);
/// assert!(t.checked_add(1.nanoseconds()).is_err());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// Similarly, if there are any non-zero units greater than hours in the
/// given span, then they also result in overflow (and thus an error):
///
/// ```
/// use jiff::{civil::time, ToSpan};
///
/// // doesn't matter what our time value is in this example
/// let t = time(0, 0, 0, 0);
/// assert!(t.checked_add(1.days()).is_err());
/// ```
///
/// # Example: adding absolute durations
///
/// This shows how to add signed and unsigned absolute durations to a
/// `Time`. As with adding a `Span`, any overflow that occurs results in
/// an error.
///
/// ```
/// use std::time::Duration;
///
/// use jiff::{civil::time, SignedDuration};
///
/// let t = time(23, 0, 0, 0);
///
/// let dur = SignedDuration::from_mins(30);
/// assert_eq!(t.checked_add(dur)?, time(23, 30, 0, 0));
/// assert_eq!(t.checked_add(-dur)?, time(22, 30, 0, 0));
///
/// let dur = Duration::new(0, 1);
/// assert_eq!(t.checked_add(dur)?, time(23, 0, 0, 1));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn checked_add<A: Into<TimeArithmetic>>(
self,
duration: A,
) -> Result<Time, Error> {
let duration: TimeArithmetic = duration.into();
duration.checked_add(self)
}
#[inline]
fn checked_add_span(self, span: Span) -> Result<Time, Error> {
let (time, span) = self.overflowing_add(span)?;
if let Some(err) = span.smallest_non_time_non_zero_unit_error() {
return Err(err);
}
Ok(time)
}
#[inline]
fn checked_add_duration(
self,
duration: SignedDuration,
) -> Result<Time, Error> {
let original = duration;
let start = t::NoUnits128::rfrom(self.to_nanosecond());
let duration = t::NoUnits128::new_unchecked(duration.as_nanos());
// This can never fail because the maximum duration fits into a
// 96-bit integer, and adding any 96-bit integer to any 64-bit
// integer can never overflow a 128-bit integer.
let end = start.try_checked_add("nanoseconds", duration).unwrap();
let end = CivilDayNanosecond::try_rfrom("nanoseconds", end)
.with_context(|| {
err!(
"adding signed duration {duration:?}, equal to
{nanos} nanoseconds, to {time} overflowed",
duration = original,
nanos = original.as_nanos(),
time = self,
)
})?;
Ok(Time::from_nanosecond(end))
}
/// This routine is identical to [`Time::checked_add`] with the duration
/// negated.
///
/// # Errors
///
/// This has the same error conditions as [`Time::checked_add`].
///
/// # Example
///
/// ```
/// use std::time::Duration;
///
/// use jiff::{civil::time, SignedDuration, ToSpan};
///
/// let t = time(22, 0, 0, 0);
/// assert_eq!(
/// t.checked_sub(1.hours().minutes(49).seconds(59))?,
/// time(20, 10, 1, 0),
/// );
/// assert_eq!(
/// t.checked_sub(SignedDuration::from_hours(1))?,
/// time(21, 0, 0, 0),
/// );
/// assert_eq!(
/// t.checked_sub(Duration::from_secs(60 * 60))?,
/// time(21, 0, 0, 0),
/// );
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn checked_sub<A: Into<TimeArithmetic>>(
self,
duration: A,
) -> Result<Time, Error> {
let duration: TimeArithmetic = duration.into();
duration.checked_neg().and_then(|ta| ta.checked_add(self))
}
/// This routine is identical to [`Time::checked_add`], except the
/// result saturates on overflow. That is, instead of overflow, either
/// [`Time::MIN`] or [`Time::MAX`] is returned.
///
/// # Example
///
/// ```
/// use jiff::{civil::{Time, time}, SignedDuration, ToSpan};
///
/// // no saturation
/// let t = time(23, 59, 59, 999_999_998);
/// assert_eq!(
/// t.with().nanosecond(999).build()?,
/// t.saturating_add(1.nanoseconds()),
/// );
///
/// // saturates
/// let t = time(23, 59, 59, 999_999_999);
/// assert_eq!(Time::MAX, t.saturating_add(1.nanoseconds()));
/// assert_eq!(Time::MAX, t.saturating_add(SignedDuration::MAX));
/// assert_eq!(Time::MIN, t.saturating_add(SignedDuration::MIN));
/// assert_eq!(Time::MAX, t.saturating_add(std::time::Duration::MAX));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// Similarly, if there are any non-zero units greater than hours in the
/// given span, then they also result in overflow (and thus saturation):
///
/// ```
/// use jiff::{civil::{Time, time}, ToSpan};
///
/// // doesn't matter what our time value is in this example
/// let t = time(0, 0, 0, 0);
/// assert_eq!(Time::MAX, t.saturating_add(1.days()));
/// ```
#[inline]
pub fn saturating_add<A: Into<TimeArithmetic>>(self, duration: A) -> Time {
let duration: TimeArithmetic = duration.into();
self.checked_add(duration).unwrap_or_else(|_| {
if duration.is_negative() {
Time::MIN
} else {
Time::MAX
}
})
}
/// This routine is identical to [`Time::saturating_add`] with the duration
/// negated.
///
/// # Example
///
/// ```
/// use jiff::{civil::{Time, time}, SignedDuration, ToSpan};
///
/// // no saturation
/// let t = time(0, 0, 0, 1);
/// assert_eq!(
/// t.with().nanosecond(0).build()?,
/// t.saturating_sub(1.nanoseconds()),
/// );
///
/// // saturates
/// let t = time(0, 0, 0, 0);
/// assert_eq!(Time::MIN, t.saturating_sub(1.nanoseconds()));
/// assert_eq!(Time::MIN, t.saturating_sub(SignedDuration::MAX));
/// assert_eq!(Time::MAX, t.saturating_sub(SignedDuration::MIN));
/// assert_eq!(Time::MIN, t.saturating_sub(std::time::Duration::MAX));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn saturating_sub<A: Into<TimeArithmetic>>(self, duration: A) -> Time {
let duration: TimeArithmetic = duration.into();
let Ok(duration) = duration.checked_neg() else { return Time::MIN };
self.saturating_add(duration)
}
/// Adds the given span to the this time value, and returns the resulting
/// time with any overflowing amount in the span returned.
///
/// This isn't part of the public API because it seems a little odd, and
/// I'm unsure of its use case. Overall this routine is a bit specialized
/// and I'm not sure how generally useful it is. But it is used in crucial
/// points in other parts of this crate.
///
/// If you want this public, please file an issue and discuss your use
/// case: https://github.com/BurntSushi/jiff/issues/new
#[inline]
pub(crate) fn overflowing_add(
self,
span: Span,
) -> Result<(Time, Span), Error> {
if let Some(err) = span.smallest_non_time_non_zero_unit_error() {
return Err(err);
}
let span_nanos = span.to_invariant_nanoseconds();
let time_nanos = self.to_nanosecond();
let sum = span_nanos + time_nanos;
let days = t::SpanDays::try_new(
"overflowing-days",
sum.div_floor(t::NANOS_PER_CIVIL_DAY),
)?;
let time_nanos = sum.rem_floor(t::NANOS_PER_CIVIL_DAY);
let time = Time::from_nanosecond(time_nanos.rinto());
Ok((time, Span::new().days_ranged(days)))
}
/// Like `overflowing_add`, but with `SignedDuration`.
///
/// This is used for datetime arithmetic, when adding to the time
/// component overflows into days (always 24 hours).
#[inline]
pub(crate) fn overflowing_add_duration(
self,
duration: SignedDuration,
) -> Result<(Time, SignedDuration), Error> {
let start = t::NoUnits128::rfrom(self.to_nanosecond());
let duration = t::NoUnits96::new_unchecked(duration.as_nanos());
// This can never fail because the maximum duration fits into a
// 96-bit integer, and adding any 96-bit integer to any 64-bit
// integer can never overflow a 128-bit integer.
let sum = start.try_checked_add("nanoseconds", duration).unwrap();
let days = t::SpanDays::try_new(
"overflowing-days",
sum.div_floor(t::NANOS_PER_CIVIL_DAY),
)?;
let time_nanos = sum.rem_floor(t::NANOS_PER_CIVIL_DAY);
let time = Time::from_nanosecond(time_nanos.rinto());
// OK because of the constraint imposed by t::SpanDays.
let hours = i64::from(days).checked_mul(24).unwrap();
Ok((time, SignedDuration::from_hours(hours)))
}
/// Returns a span representing the elapsed time from this time until
/// the given `other` time.
///
/// When `other` is earlier than this time, the span returned will be
/// negative.
///
/// Depending on the input provided, the span returned is rounded. It may
/// also be balanced down to smaller units than the default. By default,
/// the span returned is balanced such that the biggest possible unit is
/// hours.
///
/// This operation is configured by providing a [`TimeDifference`]
/// value. Since this routine accepts anything that implements
/// `Into<TimeDifference>`, once can pass a `Time` directly. One
/// can also pass a `(Unit, Time)`, where `Unit` is treated as
/// [`TimeDifference::largest`].
///
/// # Properties
///
/// As long as no rounding is requested, it is guaranteed that adding the
/// span returned to the `other` time will always equal this time.
///
/// # Errors
///
/// An error can occur if `TimeDifference` is misconfigured. For example,
/// if the smallest unit provided is bigger than the largest unit, or if
/// the largest unit is bigger than [`Unit::Hour`].
///
/// It is guaranteed that if one provides a time with the default
/// [`TimeDifference`] configuration, then this routine will never fail.
///
/// # Examples
///
/// ```
/// use jiff::{civil::time, ToSpan};
///
/// let t1 = time(22, 35, 1, 0);
/// let t2 = time(22, 35, 3, 500_000_000);
/// assert_eq!(t1.until(t2)?, 2.seconds().milliseconds(500).fieldwise());
/// // Flipping the dates is fine, but you'll get a negative span.
/// assert_eq!(t2.until(t1)?, -2.seconds().milliseconds(500).fieldwise());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: using smaller units
///
/// This example shows how to contract the span returned to smaller units.
/// This makes use of a `From<(Unit, Time)> for TimeDifference`
/// trait implementation.
///
/// ```
/// use jiff::{civil::time, Unit, ToSpan};
///
/// let t1 = time(3, 24, 30, 3500);
/// let t2 = time(15, 30, 0, 0);
///
/// // The default limits spans to using "hours" as the biggest unit.
/// let span = t1.until(t2)?;
/// assert_eq!(span.to_string(), "PT12H5M29.9999965S");
///
/// // But we can ask for smaller units, like capping the biggest unit
/// // to minutes instead of hours.
/// let span = t1.until((Unit::Minute, t2))?;
/// assert_eq!(span.to_string(), "PT725M29.9999965S");
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn until<A: Into<TimeDifference>>(
self,
other: A,
) -> Result<Span, Error> {
let args: TimeDifference = other.into();
let span = args.until_with_largest_unit(self)?;
if args.rounding_may_change_span() {
span.round(args.round)
} else {
Ok(span)
}
}
/// This routine is identical to [`Time::until`], but the order of the
/// parameters is flipped.
///
/// # Errors
///
/// This has the same error conditions as [`Time::until`].
///
/// # Example
///
/// This routine can be used via the `-` operator. Since the default
/// configuration is used and because a `Span` can represent the difference
/// between any two possible times, it will never panic.
///
/// ```
/// use jiff::{civil::time, ToSpan};
///
/// let earlier = time(1, 0, 0, 0);
/// let later = time(22, 30, 0, 0);
/// assert_eq!(later - earlier, 21.hours().minutes(30).fieldwise());
/// ```
#[inline]
pub fn since<A: Into<TimeDifference>>(
self,
other: A,
) -> Result<Span, Error> {
let args: TimeDifference = other.into();
let span = -args.until_with_largest_unit(self)?;
if args.rounding_may_change_span() {
span.round(args.round)
} else {
Ok(span)
}
}
/// Returns an absolute duration representing the elapsed time from this
/// time until the given `other` time.
///
/// When `other` occurs before this time, then the duration returned will
/// be negative.
///
/// Unlike [`Time::until`], this returns a duration corresponding to a
/// 96-bit integer of nanoseconds between two times. In this case of
/// computing durations between civil times where all days are assumed to
/// be 24 hours long, the duration returned will always be less than 24
/// hours.
///
/// # Fallibility
///
/// This routine never panics or returns an error. Since there are no
/// configuration options that can be incorrectly provided, no error is
/// possible when calling this routine. In contrast, [`Time::until`] can
/// return an error in some cases due to misconfiguration. But like this
/// routine, [`Time::until`] never panics or returns an error in its
/// default configuration.
///
/// # When should I use this versus [`Time::until`]?
///
/// See the type documentation for [`SignedDuration`] for the section on
/// when one should use [`Span`] and when one should use `SignedDuration`.
/// In short, use `Span` (and therefore `Time::until`) unless you have a
/// specific reason to do otherwise.
///
/// # Example
///
/// ```
/// use jiff::{civil::time, SignedDuration};
///
/// let t1 = time(22, 35, 1, 0);
/// let t2 = time(22, 35, 3, 500_000_000);
/// assert_eq!(t1.duration_until(t2), SignedDuration::new(2, 500_000_000));
/// // Flipping the time is fine, but you'll get a negative duration.
/// assert_eq!(t2.duration_until(t1), -SignedDuration::new(2, 500_000_000));
/// ```
///
/// # Example: difference with [`Time::until`]
///
/// Since the difference between two civil times is always expressed in
/// units of hours or smaller, and units of hours or smaller are always
/// uniform, there is no "expressive" difference between this routine and
/// `Time::until`. The only difference is that this routine returns a
/// `SignedDuration` and `Time::until` returns a [`Span`]. Moreover, since
/// the difference is always less than 24 hours, the return values can
/// always be infallibly and losslessly converted between each other:
///
/// ```
/// use jiff::{civil::time, SignedDuration, Span};
///
/// let t1 = time(22, 35, 1, 0);
/// let t2 = time(22, 35, 3, 500_000_000);
/// let dur = t1.duration_until(t2);
/// // Guaranteed to never fail because the duration
/// // between two civil times never exceeds the limits
/// // of a `Span`.
/// let span = Span::try_from(dur).unwrap();
/// assert_eq!(span, Span::new().seconds(2).milliseconds(500).fieldwise());
/// // Guaranteed to succeed and always return the original
/// // duration because the units are always hours or smaller,
/// // and thus uniform. This means a relative datetime is
/// // never required to do this conversion.
/// let dur = SignedDuration::try_from(span).unwrap();
/// assert_eq!(dur, SignedDuration::new(2, 500_000_000));
/// ```
///
/// This conversion guarantee also applies to [`Time::until`] since it
/// always returns a balanced span. That is, it never returns spans like
/// `1 second 1000 milliseconds`. (Those cannot be losslessly converted to
/// a `SignedDuration` since a `SignedDuration` is only represented as a
/// single 96-bit integer of nanoseconds.)
///
/// # Example: getting an unsigned duration
///
/// If you're looking to find the duration between two times as a
/// [`std::time::Duration`], you'll need to use this method to get a
/// [`SignedDuration`] and then convert it to a `std::time::Duration`:
///
/// ```
/// use std::time::Duration;
///
/// use jiff::{civil::time, SignedDuration, Span};
///
/// let t1 = time(22, 35, 1, 0);
/// let t2 = time(22, 35, 3, 500_000_000);
/// let dur = Duration::try_from(t1.duration_until(t2))?;;
/// assert_eq!(dur, Duration::new(2, 500_000_000));
///
/// // Note that unsigned durations cannot represent all
/// // possible differences! If the duration would be negative,
/// // then the conversion fails:
/// assert!(Duration::try_from(t2.duration_until(t1)).is_err());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn duration_until(self, other: Time) -> SignedDuration {
SignedDuration::time_until(self, other)
}
/// This routine is identical to [`Time::duration_until`], but the order of
/// the parameters is flipped.
///
/// # Example
///
/// ```
/// use jiff::{civil::time, SignedDuration};
///
/// let earlier = time(1, 0, 0, 0);
/// let later = time(22, 30, 0, 0);
/// assert_eq!(
/// later.duration_since(earlier),
/// SignedDuration::from_secs((21 * 60 * 60) + (30 * 60)),
/// );
/// ```
#[inline]
pub fn duration_since(self, other: Time) -> SignedDuration {
SignedDuration::time_until(other, self)
}
/// Rounds this time according to the [`TimeRound`] configuration given.
///
/// The principal option is [`TimeRound::smallest`], which allows one
/// to configure the smallest units in the returned time. Rounding
/// is what determines whether that unit should keep its current value
/// or whether it should be incremented. Moreover, the amount it should
/// be incremented can be configured via [`TimeRound::increment`].
/// Finally, the rounding strategy itself can be configured via
/// [`TimeRound::mode`].
///
/// Note that this routine is generic and accepts anything that
/// implements `Into<TimeRound>`. Some notable implementations are:
///
/// * `From<Unit> for Round`, which will automatically create a
/// `TimeRound::new().smallest(unit)` from the unit provided.
/// * `From<(Unit, i64)> for Round`, which will automatically create a
/// `TimeRound::new().smallest(unit).increment(number)` from the unit
/// and increment provided.
///
/// # Errors
///
/// This returns an error if the smallest unit configured on the given
/// [`TimeRound`] is bigger than hours.
///
/// The rounding increment must divide evenly into the next highest unit
/// after the smallest unit configured (and must not be equivalent to it).
/// For example, if the smallest unit is [`Unit::Nanosecond`], then *some*
/// of the valid values for the rounding increment are `1`, `2`, `4`, `5`,
/// `100` and `500`. Namely, any integer that divides evenly into `1,000`
/// nanoseconds since there are `1,000` nanoseconds in the next highest
/// unit (microseconds).
///
/// This can never fail because of overflow for any input. The only
/// possible errors are "configuration" errors.
///
/// # Example
///
/// This is a basic example that demonstrates rounding a datetime to the
/// nearest second. This also demonstrates calling this method with the
/// smallest unit directly, instead of constructing a `TimeRound` manually.
///
/// ```
/// use jiff::{civil::time, Unit};
///
/// let t = time(15, 45, 10, 123_456_789);
/// assert_eq!(
/// t.round(Unit::Second)?,
/// time(15, 45, 10, 0),
/// );
/// let t = time(15, 45, 10, 500_000_001);
/// assert_eq!(
/// t.round(Unit::Second)?,
/// time(15, 45, 11, 0),
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: changing the rounding mode
///
/// The default rounding mode is [`RoundMode::HalfExpand`], which
/// breaks ties by rounding away from zero. But other modes like
/// [`RoundMode::Trunc`] can be used too:
///
/// ```
/// use jiff::{civil::{TimeRound, time}, RoundMode, Unit};
///
/// let t = time(15, 45, 10, 999_999_999);
/// assert_eq!(
/// t.round(Unit::Second)?,
/// time(15, 45, 11, 0),
/// );
/// // The default will round up to the next second for any fraction
/// // greater than or equal to 0.5. But truncation will always round
/// // toward zero.
/// assert_eq!(
/// t.round(
/// TimeRound::new().smallest(Unit::Second).mode(RoundMode::Trunc),
/// )?,
/// time(15, 45, 10, 0),
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: rounding to the nearest 5 minute increment
///
/// ```
/// use jiff::{civil::time, Unit};
///
/// // rounds down
/// let t = time(15, 27, 29, 999_999_999);
/// assert_eq!(t.round((Unit::Minute, 5))?, time(15, 25, 0, 0));
/// // rounds up
/// let t = time(15, 27, 30, 0);
/// assert_eq!(t.round((Unit::Minute, 5))?, time(15, 30, 0, 0));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: rounding wraps around on overflow
///
/// This example demonstrates that it's possible for this operation to
/// overflow, and as a result, have the time wrap around.
///
/// ```
/// use jiff::{civil::Time, Unit};
///
/// let t = Time::MAX;
/// assert_eq!(t.round(Unit::Hour)?, Time::MIN);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn round<R: Into<TimeRound>>(self, options: R) -> Result<Time, Error> {
let options: TimeRound = options.into();
options.round(self)
}
/// Return an iterator of periodic times determined by the given span.
///
/// The given span may be negative, in which case, the iterator will move
/// backwards through time. The iterator won't stop until either the span
/// itself overflows, or it would otherwise exceed the minimum or maximum
/// `Time` value.
///
/// # Example: visiting every third hour
///
/// This shows how to visit each third hour of a 24 hour time interval:
///
/// ```
/// use jiff::{civil::{Time, time}, ToSpan};
///
/// let start = Time::MIN;
/// let mut every_third_hour = vec![];
/// for t in start.series(3.hours()) {
/// every_third_hour.push(t);
/// }
/// assert_eq!(every_third_hour, vec![
/// time(0, 0, 0, 0),
/// time(3, 0, 0, 0),
/// time(6, 0, 0, 0),
/// time(9, 0, 0, 0),
/// time(12, 0, 0, 0),
/// time(15, 0, 0, 0),
/// time(18, 0, 0, 0),
/// time(21, 0, 0, 0),
/// ]);
/// ```
///
/// Or go backwards every 6.5 hours:
///
/// ```
/// use jiff::{civil::{Time, time}, ToSpan};
///
/// let start = time(23, 0, 0, 0);
/// let times: Vec<Time> = start.series(-6.hours().minutes(30)).collect();
/// assert_eq!(times, vec![
/// time(23, 0, 0, 0),
/// time(16, 30, 0, 0),
/// time(10, 0, 0, 0),
/// time(3, 30, 0, 0),
/// ]);
/// ```
#[inline]
pub fn series(self, period: Span) -> TimeSeries {
TimeSeries { start: self, period, step: 0 }
}
}
/// Parsing and formatting using a "printf"-style API.
impl Time {
/// Parses a civil time in `input` matching the given `format`.
///
/// The format string uses a "printf"-style API where conversion
/// specifiers can be used as place holders to match components of
/// a datetime. For details on the specifiers supported, see the
/// [`fmt::strtime`] module documentation.
///
/// # Errors
///
/// This returns an error when parsing failed. This might happen because
/// the format string itself was invalid, or because the input didn't match
/// the format string.
///
/// This also returns an error if there wasn't sufficient information to
/// construct a civil time. For example, if an offset wasn't parsed.
///
/// # Example
///
/// This example shows how to parse a civil time:
///
/// ```
/// use jiff::civil::Time;
///
/// // Parse with a 12-hour clock.
/// let time = Time::strptime("%I:%M%P", "4:30pm")?;
/// assert_eq!(time.to_string(), "16:30:00");
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn strptime(
format: impl AsRef<[u8]>,
input: impl AsRef<[u8]>,
) -> Result<Time, Error> {
fmt::strtime::parse(format, input).and_then(|tm| tm.to_time())
}
/// Formats this civil time according to the given `format`.
///
/// The format string uses a "printf"-style API where conversion
/// specifiers can be used as place holders to format components of
/// a datetime. For details on the specifiers supported, see the
/// [`fmt::strtime`] module documentation.
///
/// # Errors and panics
///
/// While this routine itself does not error or panic, using the value
/// returned may result in a panic if formatting fails. See the
/// documentation on [`fmt::strtime::Display`] for more information.
///
/// To format in a way that surfaces errors without panicking, use either
/// [`fmt::strtime::format`] or [`fmt::strtime::BrokenDownTime::format`].
///
/// # Example
///
/// This example shows how to format a civil time in a 12 hour clock with
/// no padding for the hour:
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(16, 30, 59, 0);
/// let string = t.strftime("%-I:%M%P").to_string();
/// assert_eq!(string, "4:30pm");
/// ```
///
/// Note that one can round a `Time` before formatting. For example, to
/// round to the nearest minute:
///
/// ```
/// use jiff::{civil::time, Unit};
///
/// let t = time(16, 30, 59, 0);
/// let string = t.round(Unit::Minute)?.strftime("%-I:%M%P").to_string();
/// assert_eq!(string, "4:31pm");
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn strftime<'f, F: 'f + ?Sized + AsRef<[u8]>>(
&self,
format: &'f F,
) -> fmt::strtime::Display<'f> {
fmt::strtime::Display { fmt: format.as_ref(), tm: (*self).into() }
}
}
/// Crate internal APIs.
///
/// Many of these are mirrors of the public API, but on ranged types. These
/// are often much more convenient to use in composition with other parts of
/// the crate that also use ranged integer types. And this often permits the
/// routines to be infallible and (possibly) zero-cost.
impl Time {
#[inline]
pub(crate) fn new_ranged(
hour: impl RInto<Hour>,
minute: impl RInto<Minute>,
second: impl RInto<Second>,
subsec_nanosecond: impl RInto<SubsecNanosecond>,
) -> Time {
Time {
hour: hour.rinto(),
minute: minute.rinto(),
second: second.rinto(),
subsec_nanosecond: subsec_nanosecond.rinto(),
}
}
#[inline]
pub(crate) fn new_ranged_unchecked(
hour: Hour,
minute: Minute,
second: Second,
subsec_nanosecond: SubsecNanosecond,
) -> Time {
Time { hour, minute, second, subsec_nanosecond }
}
/// Set the fractional parts of this time to the given units via ranged
/// types.
#[inline]
fn with_subsec_parts_ranged(
self,
millisecond: impl RInto<Millisecond>,
microsecond: impl RInto<Microsecond>,
nanosecond: impl RInto<Nanosecond>,
) -> Time {
let millisecond = SubsecNanosecond::rfrom(millisecond.rinto());
let microsecond = SubsecNanosecond::rfrom(microsecond.rinto());
let nanosecond = SubsecNanosecond::rfrom(nanosecond.rinto());
let mut subsec_nanosecond =
millisecond * t::MICROS_PER_MILLI * t::NANOS_PER_MICRO;
subsec_nanosecond += microsecond * t::NANOS_PER_MICRO;
subsec_nanosecond += nanosecond;
Time { subsec_nanosecond: subsec_nanosecond.rinto(), ..self }
}
#[inline]
pub(crate) fn hour_ranged(self) -> Hour {
self.hour
}
#[inline]
pub(crate) fn minute_ranged(self) -> Minute {
self.minute
}
#[inline]
pub(crate) fn second_ranged(self) -> Second {
self.second
}
#[inline]
pub(crate) fn millisecond_ranged(self) -> Millisecond {
let micros = self.subsec_nanosecond_ranged() / t::NANOS_PER_MICRO;
let millis = micros / t::MICROS_PER_MILLI;
millis.rinto()
}
#[inline]
pub(crate) fn microsecond_ranged(self) -> Microsecond {
let micros = self.subsec_nanosecond_ranged() / t::NANOS_PER_MICRO;
let only_micros = micros % t::MICROS_PER_MILLI;
only_micros.rinto()
}
#[inline]
pub(crate) fn nanosecond_ranged(self) -> Nanosecond {
let only_nanos = self.subsec_nanosecond_ranged() % t::NANOS_PER_MICRO;
only_nanos.rinto()
}
#[inline]
pub(crate) fn subsec_nanosecond_ranged(self) -> SubsecNanosecond {
self.subsec_nanosecond
}
#[inline]
pub(crate) fn until_nanoseconds(self, other: Time) -> t::SpanNanoseconds {
let t1 = t::SpanNanoseconds::rfrom(self.to_nanosecond());
let t2 = t::SpanNanoseconds::rfrom(other.to_nanosecond());
t2 - t1
}
/// Converts this time value to the number of nanoseconds that has elapsed
/// since `00:00:00.000000000`.
///
/// The maximum possible value that can be returned represents the time
/// `23:59:59.999999999`.
#[inline]
pub(crate) fn to_nanosecond(&self) -> CivilDayNanosecond {
#[cfg(not(debug_assertions))]
{
CivilDayNanosecond {
val: to_day_nanosecond(
self.hour.val,
self.minute.val,
self.second.val,
self.subsec_nanosecond.val,
),
}
}
#[cfg(debug_assertions)]
{
let val = to_day_nanosecond(
self.hour.val,
self.minute.val,
self.second.val,
self.subsec_nanosecond.val,
);
let min = to_day_nanosecond(
self.hour.min,
self.minute.min,
self.second.min,
self.subsec_nanosecond.min,
);
let max = to_day_nanosecond(
self.hour.max,
self.minute.max,
self.second.max,
self.subsec_nanosecond.max,
);
CivilDayNanosecond { val, min, max }
}
}
/// Converts the given nanosecond to a time value. The nanosecond should
/// correspond to the number of nanoseconds that have elapsed since
/// `00:00:00.000000000`.
#[inline(always)]
pub(crate) fn from_nanosecond(nanosecond: CivilDayNanosecond) -> Time {
#[cfg(not(debug_assertions))]
{
let (hour, minute, second, subsec) =
from_day_nanosecond(nanosecond.val);
Time {
hour: Hour { val: hour },
minute: Minute { val: minute },
second: Second { val: second },
subsec_nanosecond: SubsecNanosecond { val: subsec },
}
}
#[cfg(debug_assertions)]
{
let (hour, minute, second, subsec) =
from_day_nanosecond(nanosecond.val);
let (min_hour, min_minute, min_second, min_subsec) =
from_day_nanosecond(nanosecond.min);
let (max_hour, max_minute, max_second, max_subsec) =
from_day_nanosecond(nanosecond.max);
let hour = Hour { val: hour, min: min_hour, max: max_hour };
let minute =
Minute { val: minute, min: min_minute, max: max_minute };
let second =
Second { val: second, min: min_second, max: max_second };
let subsec = SubsecNanosecond {
val: subsec,
min: min_subsec,
max: max_subsec,
};
Time { hour, minute, second, subsec_nanosecond: subsec }
}
}
}
impl Default for Time {
#[inline]
fn default() -> Time {
Time::midnight()
}
}
/// Converts a `Time` into a human readable time string.
///
/// (This `Debug` representation currently emits the same string as the
/// `Display` representation, but this is not a guarantee.)
///
/// Options currently supported:
///
/// * [`std::fmt::Formatter::precision`] can be set to control the precision
/// of the fractional second component.
///
/// # Example
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(7, 0, 0, 123_000_000);
/// assert_eq!(format!("{t:.6?}"), "07:00:00.123000");
/// // Precision values greater than 9 are clamped to 9.
/// assert_eq!(format!("{t:.300?}"), "07:00:00.123000000");
/// // A precision of 0 implies the entire fractional
/// // component is always truncated.
/// assert_eq!(format!("{t:.0?}"), "07:00:00");
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
impl core::fmt::Debug for Time {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
core::fmt::Display::fmt(self, f)
}
}
/// Converts a `Time` into an ISO 8601 compliant string.
///
/// Options currently supported:
///
/// * [`std::fmt::Formatter::precision`] can be set to control the precision
/// of the fractional second component.
///
/// # Example
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(7, 0, 0, 123_000_000);
/// assert_eq!(format!("{t:.6}"), "07:00:00.123000");
/// // Precision values greater than 9 are clamped to 9.
/// assert_eq!(format!("{t:.300}"), "07:00:00.123000000");
/// // A precision of 0 implies the entire fractional
/// // component is always truncated.
/// assert_eq!(format!("{t:.0}"), "07:00:00");
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
impl core::fmt::Display for Time {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
use crate::fmt::StdFmtWrite;
let precision =
f.precision().map(|p| u8::try_from(p).unwrap_or(u8::MAX));
temporal::DateTimePrinter::new()
.precision(precision)
.print_time(self, StdFmtWrite(f))
.map_err(|_| core::fmt::Error)
}
}
impl core::str::FromStr for Time {
type Err = Error;
#[inline]
fn from_str(string: &str) -> Result<Time, Error> {
DEFAULT_DATETIME_PARSER.parse_time(string)
}
}
/// Adds a span of time. This uses wrapping arithmetic.
///
/// For checked arithmetic, see [`Time::checked_add`].
impl core::ops::Add<Span> for Time {
type Output = Time;
#[inline]
fn add(self, rhs: Span) -> Time {
self.wrapping_add(rhs)
}
}
/// Adds a span of time in place. This uses wrapping arithmetic.
///
/// For checked arithmetic, see [`Time::checked_add`].
impl core::ops::AddAssign<Span> for Time {
#[inline]
fn add_assign(&mut self, rhs: Span) {
*self = *self + rhs;
}
}
/// Subtracts a span of time. This uses wrapping arithmetic.
///
/// For checked arithmetic, see [`Time::checked_sub`].
impl core::ops::Sub<Span> for Time {
type Output = Time;
#[inline]
fn sub(self, rhs: Span) -> Time {
self.wrapping_sub(rhs)
}
}
/// Subtracts a span of time in place. This uses wrapping arithmetic.
///
/// For checked arithmetic, see [`Time::checked_sub`].
impl core::ops::SubAssign<Span> for Time {
#[inline]
fn sub_assign(&mut self, rhs: Span) {
*self = *self - rhs;
}
}
/// Computes the span of time between two times.
///
/// This will return a negative span when the time being subtracted is greater.
///
/// Since this uses the default configuration for calculating a span between
/// two times (no rounding and largest units is hours), this will never panic
/// or fail in any way.
///
/// To configure the largest unit or enable rounding, use [`Time::since`].
impl core::ops::Sub for Time {
type Output = Span;
#[inline]
fn sub(self, rhs: Time) -> Span {
self.since(rhs).expect("since never fails when given Time")
}
}
/// Adds a signed duration of time. This uses wrapping arithmetic.
///
/// For checked arithmetic, see [`Time::checked_add`].
impl core::ops::Add<SignedDuration> for Time {
type Output = Time;
#[inline]
fn add(self, rhs: SignedDuration) -> Time {
self.wrapping_add(rhs)
}
}
/// Adds a signed duration of time in place. This uses wrapping arithmetic.
///
/// For checked arithmetic, see [`Time::checked_add`].
impl core::ops::AddAssign<SignedDuration> for Time {
#[inline]
fn add_assign(&mut self, rhs: SignedDuration) {
*self = *self + rhs;
}
}
/// Subtracts a signed duration of time. This uses wrapping arithmetic.
///
/// For checked arithmetic, see [`Time::checked_sub`].
impl core::ops::Sub<SignedDuration> for Time {
type Output = Time;
#[inline]
fn sub(self, rhs: SignedDuration) -> Time {
self.wrapping_sub(rhs)
}
}
/// Subtracts a signed duration of time in place. This uses wrapping arithmetic.
///
/// For checked arithmetic, see [`Time::checked_sub`].
impl core::ops::SubAssign<SignedDuration> for Time {
#[inline]
fn sub_assign(&mut self, rhs: SignedDuration) {
*self = *self - rhs;
}
}
/// Adds an unsigned duration of time. This uses wrapping arithmetic.
///
/// For checked arithmetic, see [`Time::checked_add`].
impl core::ops::Add<UnsignedDuration> for Time {
type Output = Time;
#[inline]
fn add(self, rhs: UnsignedDuration) -> Time {
self.wrapping_add(rhs)
}
}
/// Adds an unsigned duration of time in place. This uses wrapping arithmetic.
///
/// For checked arithmetic, see [`Time::checked_add`].
impl core::ops::AddAssign<UnsignedDuration> for Time {
#[inline]
fn add_assign(&mut self, rhs: UnsignedDuration) {
*self = *self + rhs;
}
}
/// Subtracts an unsigned duration of time. This uses wrapping arithmetic.
///
/// For checked arithmetic, see [`Time::checked_sub`].
impl core::ops::Sub<UnsignedDuration> for Time {
type Output = Time;
#[inline]
fn sub(self, rhs: UnsignedDuration) -> Time {
self.wrapping_sub(rhs)
}
}
/// Subtracts an unsigned duration of time in place. This uses wrapping
/// arithmetic.
///
/// For checked arithmetic, see [`Time::checked_sub`].
impl core::ops::SubAssign<UnsignedDuration> for Time {
#[inline]
fn sub_assign(&mut self, rhs: UnsignedDuration) {
*self = *self - rhs;
}
}
impl From<DateTime> for Time {
#[inline]
fn from(dt: DateTime) -> Time {
dt.time()
}
}
impl From<Zoned> for Time {
#[inline]
fn from(zdt: Zoned) -> Time {
zdt.datetime().time()
}
}
impl<'a> From<&'a Zoned> for Time {
#[inline]
fn from(zdt: &'a Zoned) -> Time {
zdt.datetime().time()
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for Time {
#[inline]
fn serialize<S: serde::Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
serializer.collect_str(self)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Time {
#[inline]
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Time, D::Error> {
use serde::de;
struct TimeVisitor;
impl<'de> de::Visitor<'de> for TimeVisitor {
type Value = Time;
fn expecting(
&self,
f: &mut core::fmt::Formatter,
) -> core::fmt::Result {
f.write_str("a time string")
}
#[inline]
fn visit_bytes<E: de::Error>(
self,
value: &[u8],
) -> Result<Time, E> {
DEFAULT_DATETIME_PARSER
.parse_time(value)
.map_err(de::Error::custom)
}
#[inline]
fn visit_str<E: de::Error>(self, value: &str) -> Result<Time, E> {
self.visit_bytes(value.as_bytes())
}
}
deserializer.deserialize_str(TimeVisitor)
}
}
#[cfg(test)]
impl quickcheck::Arbitrary for Time {
fn arbitrary(g: &mut quickcheck::Gen) -> Time {
let hour = Hour::arbitrary(g);
let minute = Minute::arbitrary(g);
let second = Second::arbitrary(g);
let subsec_nanosecond = SubsecNanosecond::arbitrary(g);
Time::new_ranged(hour, minute, second, subsec_nanosecond)
}
fn shrink(&self) -> alloc::boxed::Box<dyn Iterator<Item = Time>> {
alloc::boxed::Box::new(
(
self.hour_ranged(),
self.minute_ranged(),
self.second_ranged(),
self.subsec_nanosecond_ranged(),
)
.shrink()
.map(
|(hour, minute, second, subsec_nanosecond)| {
Time::new_ranged(
hour,
minute,
second,
subsec_nanosecond,
)
},
),
)
}
}
/// An iterator over periodic times, created by [`Time::series`].
///
/// It is exhausted when the next value would exceed a [`Span`] or [`Time`]
/// value.
#[derive(Clone, Debug)]
pub struct TimeSeries {
start: Time,
period: Span,
step: i64,
}
impl Iterator for TimeSeries {
type Item = Time;
#[inline]
fn next(&mut self) -> Option<Time> {
let span = self.period.checked_mul(self.step).ok()?;
self.step = self.step.checked_add(1)?;
let time = self.start.checked_add(span).ok()?;
Some(time)
}
}
/// Options for [`Time::checked_add`] and [`Time::checked_sub`].
///
/// This type provides a way to ergonomically add one of a few different
/// duration types to a [`Time`].
///
/// The main way to construct values of this type is with its `From` trait
/// implementations:
///
/// * `From<Span> for TimeArithmetic` adds (or subtracts) the given span to the
/// receiver time.
/// * `From<SignedDuration> for TimeArithmetic` adds (or subtracts)
/// the given signed duration to the receiver time.
/// * `From<std::time::Duration> for TimeArithmetic` adds (or subtracts)
/// the given unsigned duration to the receiver time.
///
/// # Example
///
/// ```
/// use std::time::Duration;
///
/// use jiff::{civil::time, SignedDuration, ToSpan};
///
/// let t = time(0, 0, 0, 0);
/// assert_eq!(t.checked_add(2.hours())?, time(2, 0, 0, 0));
/// assert_eq!(t.checked_add(SignedDuration::from_hours(2))?, time(2, 0, 0, 0));
/// assert_eq!(t.checked_add(Duration::from_secs(2 * 60 * 60))?, time(2, 0, 0, 0));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Clone, Copy, Debug)]
pub struct TimeArithmetic {
duration: Duration,
}
impl TimeArithmetic {
#[inline]
fn wrapping_add(self, time: Time) -> Time {
match self.duration {
Duration::Span(span) => time.wrapping_add_span(span),
Duration::Signed(sdur) => time.wrapping_add_signed_duration(sdur),
Duration::Unsigned(udur) => {
time.wrapping_add_unsigned_duration(udur)
}
}
}
#[inline]
fn wrapping_sub(self, time: Time) -> Time {
match self.duration {
Duration::Span(span) => time.wrapping_add_span(span.negate()),
Duration::Signed(sdur) => {
if let Some(sdur) = sdur.checked_neg() {
time.wrapping_add_signed_duration(sdur)
} else {
let udur = UnsignedDuration::new(
i64::MIN.unsigned_abs(),
sdur.subsec_nanos().unsigned_abs(),
);
time.wrapping_add_unsigned_duration(udur)
}
}
Duration::Unsigned(udur) => {
time.wrapping_sub_unsigned_duration(udur)
}
}
}
#[inline]
fn checked_add(self, time: Time) -> Result<Time, Error> {
match self.duration.to_signed()? {
SDuration::Span(span) => time.checked_add_span(span),
SDuration::Absolute(sdur) => time.checked_add_duration(sdur),
}
}
#[inline]
fn checked_neg(self) -> Result<TimeArithmetic, Error> {
let duration = self.duration.checked_neg()?;
Ok(TimeArithmetic { duration })
}
#[inline]
fn is_negative(&self) -> bool {
self.duration.is_negative()
}
}
impl From<Span> for TimeArithmetic {
fn from(span: Span) -> TimeArithmetic {
let duration = Duration::from(span);
TimeArithmetic { duration }
}
}
impl From<SignedDuration> for TimeArithmetic {
fn from(sdur: SignedDuration) -> TimeArithmetic {
let duration = Duration::from(sdur);
TimeArithmetic { duration }
}
}
impl From<UnsignedDuration> for TimeArithmetic {
fn from(udur: UnsignedDuration) -> TimeArithmetic {
let duration = Duration::from(udur);
TimeArithmetic { duration }
}
}
impl<'a> From<&'a Span> for TimeArithmetic {
fn from(span: &'a Span) -> TimeArithmetic {
TimeArithmetic::from(*span)
}
}
impl<'a> From<&'a SignedDuration> for TimeArithmetic {
fn from(sdur: &'a SignedDuration) -> TimeArithmetic {
TimeArithmetic::from(*sdur)
}
}
impl<'a> From<&'a UnsignedDuration> for TimeArithmetic {
fn from(udur: &'a UnsignedDuration) -> TimeArithmetic {
TimeArithmetic::from(*udur)
}
}
/// Options for [`Time::since`] and [`Time::until`].
///
/// This type provides a way to configure the calculation of spans between two
/// [`Time`] values. In particular, both `Time::since` and `Time::until` accept
/// anything that implements `Into<TimeDifference>`. There are a few key trait
/// implementations that make this convenient:
///
/// * `From<Time> for TimeDifference` will construct a configuration consisting
/// of just the time. So for example, `time1.until(time2)` will return the span
/// from `time1` to `time2`.
/// * `From<DateTime> for TimeDifference` will construct a configuration
/// consisting of just the time from the given datetime. So for example,
/// `time.since(datetime)` returns the span from `datetime.time()` to `time`.
/// * `From<(Unit, Time)>` is a convenient way to specify the largest units
/// that should be present on the span returned. By default, the largest units
/// are hours. Using this trait implementation is equivalent to
/// `TimeDifference::new(time).largest(unit)`.
/// * `From<(Unit, DateTime)>` is like the one above, but with the time from
/// the given datetime.
///
/// One can also provide a `TimeDifference` value directly. Doing so
/// is necessary to use the rounding features of calculating a span. For
/// example, setting the smallest unit (defaults to [`Unit::Nanosecond`]), the
/// rounding mode (defaults to [`RoundMode::Trunc`]) and the rounding increment
/// (defaults to `1`). The defaults are selected such that no rounding occurs.
///
/// Rounding a span as part of calculating it is provided as a convenience.
/// Callers may choose to round the span as a distinct step via
/// [`Span::round`].
///
/// # Example
///
/// This example shows how to round a span between two datetimes to the nearest
/// half-hour, with ties breaking away from zero.
///
/// ```
/// use jiff::{civil::{Time, TimeDifference}, RoundMode, ToSpan, Unit};
///
/// let t1 = "08:14:00.123456789".parse::<Time>()?;
/// let t2 = "15:00".parse::<Time>()?;
/// let span = t1.until(
/// TimeDifference::new(t2)
/// .smallest(Unit::Minute)
/// .mode(RoundMode::HalfExpand)
/// .increment(30),
/// )?;
/// assert_eq!(span, 7.hours().fieldwise());
///
/// // One less minute, and because of the HalfExpand mode, the span would
/// // get rounded down.
/// let t2 = "14:59".parse::<Time>()?;
/// let span = t1.until(
/// TimeDifference::new(t2)
/// .smallest(Unit::Minute)
/// .mode(RoundMode::HalfExpand)
/// .increment(30),
/// )?;
/// assert_eq!(span, 6.hours().minutes(30).fieldwise());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Clone, Copy, Debug)]
pub struct TimeDifference {
time: Time,
round: SpanRound<'static>,
}
impl TimeDifference {
/// Create a new default configuration for computing the span between
/// the given time and some other time (specified as the receiver in
/// [`Time::since`] or [`Time::until`]).
#[inline]
pub fn new(time: Time) -> TimeDifference {
// We use truncation rounding by default since it seems that's
// what is generally expected when computing the difference between
// datetimes.
//
// See: https://github.com/tc39/proposal-temporal/issues/1122
let round = SpanRound::new().mode(RoundMode::Trunc);
TimeDifference { time, round }
}
/// Set the smallest units allowed in the span returned.
///
/// # Errors
///
/// The smallest units must be no greater than the largest units. If this
/// is violated, then computing a span with this configuration will result
/// in an error.
///
/// # Example
///
/// This shows how to round a span between two times to units no less than
/// seconds.
///
/// ```
/// use jiff::{civil::{Time, TimeDifference}, RoundMode, ToSpan, Unit};
///
/// let t1 = "08:14:02.5001".parse::<Time>()?;
/// let t2 = "08:30:03.0001".parse::<Time>()?;
/// let span = t1.until(
/// TimeDifference::new(t2)
/// .smallest(Unit::Second)
/// .mode(RoundMode::HalfExpand),
/// )?;
/// assert_eq!(span, 16.minutes().seconds(1).fieldwise());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn smallest(self, unit: Unit) -> TimeDifference {
TimeDifference { round: self.round.smallest(unit), ..self }
}
/// Set the largest units allowed in the span returned.
///
/// When a largest unit is not specified, computing a span between times
/// behaves as if it were set to [`Unit::Hour`].
///
/// # Errors
///
/// The largest units, when set, must be at least as big as the smallest
/// units (which defaults to [`Unit::Nanosecond`]). If this is violated,
/// then computing a span with this configuration will result in an error.
///
/// # Example
///
/// This shows how to round a span between two times to units no
/// bigger than seconds.
///
/// ```
/// use jiff::{civil::{Time, TimeDifference}, ToSpan, Unit};
///
/// let t1 = "08:14".parse::<Time>()?;
/// let t2 = "08:30".parse::<Time>()?;
/// let span = t1.until(TimeDifference::new(t2).largest(Unit::Second))?;
/// assert_eq!(span, 960.seconds().fieldwise());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn largest(self, unit: Unit) -> TimeDifference {
TimeDifference { round: self.round.largest(unit), ..self }
}
/// Set the rounding mode.
///
/// This defaults to [`RoundMode::Trunc`] since it's plausible that
/// rounding "up" in the context of computing the span between two times
/// could be surprising in a number of cases. The [`RoundMode::HalfExpand`]
/// mode corresponds to typical rounding you might have learned about in
/// school. But a variety of other rounding modes exist.
///
/// # Example
///
/// This shows how to always round "up" towards positive infinity.
///
/// ```
/// use jiff::{civil::{Time, TimeDifference}, RoundMode, ToSpan, Unit};
///
/// let t1 = "08:10".parse::<Time>()?;
/// let t2 = "08:11".parse::<Time>()?;
/// let span = t1.until(
/// TimeDifference::new(t2)
/// .smallest(Unit::Hour)
/// .mode(RoundMode::Ceil),
/// )?;
/// // Only one minute elapsed, but we asked to always round up!
/// assert_eq!(span, 1.hour().fieldwise());
///
/// // Since `Ceil` always rounds toward positive infinity, the behavior
/// // flips for a negative span.
/// let span = t1.since(
/// TimeDifference::new(t2)
/// .smallest(Unit::Hour)
/// .mode(RoundMode::Ceil),
/// )?;
/// assert_eq!(span, 0.hour().fieldwise());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn mode(self, mode: RoundMode) -> TimeDifference {
TimeDifference { round: self.round.mode(mode), ..self }
}
/// Set the rounding increment for the smallest unit.
///
/// The default value is `1`. Other values permit rounding the smallest
/// unit to the nearest integer increment specified. For example, if the
/// smallest unit is set to [`Unit::Minute`], then a rounding increment of
/// `30` would result in rounding in increments of a half hour. That is,
/// the only minute value that could result would be `0` or `30`.
///
/// # Errors
///
/// The rounding increment must divide evenly into the next highest unit
/// after the smallest unit configured (and must not be equivalent to it).
/// For example, if the smallest unit is [`Unit::Nanosecond`], then *some*
/// of the valid values for the rounding increment are `1`, `2`, `4`, `5`,
/// `100` and `500`. Namely, any integer that divides evenly into `1,000`
/// nanoseconds since there are `1,000` nanoseconds in the next highest
/// unit (microseconds).
///
/// The error will occur when computing the span, and not when setting
/// the increment here.
///
/// # Example
///
/// This shows how to round the span between two times to the nearest 5
/// minute increment.
///
/// ```
/// use jiff::{civil::{Time, TimeDifference}, RoundMode, ToSpan, Unit};
///
/// let t1 = "08:19".parse::<Time>()?;
/// let t2 = "12:52".parse::<Time>()?;
/// let span = t1.until(
/// TimeDifference::new(t2)
/// .smallest(Unit::Minute)
/// .increment(5)
/// .mode(RoundMode::HalfExpand),
/// )?;
/// assert_eq!(span, 4.hour().minutes(35).fieldwise());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn increment(self, increment: i64) -> TimeDifference {
TimeDifference { round: self.round.increment(increment), ..self }
}
/// Returns true if and only if this configuration could change the span
/// via rounding.
#[inline]
fn rounding_may_change_span(&self) -> bool {
self.round.rounding_may_change_span_ignore_largest()
}
/// Returns the span of time from `t1` to the time in this configuration.
/// The biggest units allowed are determined by the `smallest` and
/// `largest` settings, but defaults to `Unit::Hour`.
#[inline]
fn until_with_largest_unit(&self, t1: Time) -> Result<Span, Error> {
let t2 = self.time;
if t1 == t2 {
return Ok(Span::new());
}
let largest = self.round.get_largest().unwrap_or(Unit::Hour);
if largest > Unit::Hour {
return Err(err!(
"rounding the span between two times must use hours \
or smaller for its units, but found {units}",
units = largest.plural(),
));
}
let start = t1.to_nanosecond();
let end = t2.to_nanosecond();
let span = Span::from_invariant_nanoseconds(largest, end - start)
.expect("difference in civil times is always in bounds");
Ok(span)
}
}
impl From<Time> for TimeDifference {
#[inline]
fn from(time: Time) -> TimeDifference {
TimeDifference::new(time)
}
}
impl From<DateTime> for TimeDifference {
#[inline]
fn from(dt: DateTime) -> TimeDifference {
TimeDifference::from(Time::from(dt))
}
}
impl From<Zoned> for TimeDifference {
#[inline]
fn from(zdt: Zoned) -> TimeDifference {
TimeDifference::from(Time::from(zdt))
}
}
impl<'a> From<&'a Zoned> for TimeDifference {
#[inline]
fn from(zdt: &'a Zoned) -> TimeDifference {
TimeDifference::from(zdt.datetime())
}
}
impl From<(Unit, Time)> for TimeDifference {
#[inline]
fn from((largest, time): (Unit, Time)) -> TimeDifference {
TimeDifference::from(time).largest(largest)
}
}
impl From<(Unit, DateTime)> for TimeDifference {
#[inline]
fn from((largest, dt): (Unit, DateTime)) -> TimeDifference {
TimeDifference::from((largest, Time::from(dt)))
}
}
impl From<(Unit, Zoned)> for TimeDifference {
#[inline]
fn from((largest, zdt): (Unit, Zoned)) -> TimeDifference {
TimeDifference::from((largest, Time::from(zdt)))
}
}
impl<'a> From<(Unit, &'a Zoned)> for TimeDifference {
#[inline]
fn from((largest, zdt): (Unit, &'a Zoned)) -> TimeDifference {
TimeDifference::from((largest, zdt.datetime()))
}
}
/// Options for [`Time::round`].
///
/// This type provides a way to configure the rounding of a civil time.
/// In particular, `Time::round` accepts anything that implements the
/// `Into<TimeRound>` trait. There are some trait implementations that
/// therefore make calling `Time::round` in some common cases more ergonomic:
///
/// * `From<Unit> for TimeRound` will construct a rounding configuration that
/// rounds to the unit given. Specifically, `TimeRound::new().smallest(unit)`.
/// * `From<(Unit, i64)> for TimeRound` is like the one above, but also
/// specifies the rounding increment for [`TimeRound::increment`].
///
/// Note that in the default configuration, no rounding occurs.
///
/// # Example
///
/// This example shows how to round a time to the nearest second:
///
/// ```
/// use jiff::{civil::{Time, time}, Unit};
///
/// let t: Time = "16:24:59.5".parse()?;
/// assert_eq!(
/// t.round(Unit::Second)?,
/// // The second rounds up and causes minutes to increase.
/// time(16, 25, 0, 0),
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// The above makes use of the fact that `Unit` implements
/// `Into<TimeRound>`. If you want to change the rounding mode to, say,
/// truncation, then you'll need to construct a `TimeRound` explicitly
/// since there are no convenience `Into` trait implementations for
/// [`RoundMode`].
///
/// ```
/// use jiff::{civil::{Time, TimeRound, time}, RoundMode, Unit};
///
/// let t: Time = "2024-06-20 16:24:59.5".parse()?;
/// assert_eq!(
/// t.round(
/// TimeRound::new().smallest(Unit::Second).mode(RoundMode::Trunc),
/// )?,
/// // The second just gets truncated as if it wasn't there.
/// time(16, 24, 59, 0),
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Clone, Copy, Debug)]
pub struct TimeRound {
smallest: Unit,
mode: RoundMode,
increment: i64,
}
impl TimeRound {
/// Create a new default configuration for rounding a [`Time`].
#[inline]
pub fn new() -> TimeRound {
TimeRound {
smallest: Unit::Nanosecond,
mode: RoundMode::HalfExpand,
increment: 1,
}
}
/// Set the smallest units allowed in the time returned after rounding.
///
/// Any units below the smallest configured unit will be used, along with
/// the rounding increment and rounding mode, to determine the value of the
/// smallest unit. For example, when rounding `03:25:30` to the
/// nearest minute, the `30` second unit will result in rounding the minute
/// unit of `25` up to `26` and zeroing out everything below minutes.
///
/// This defaults to [`Unit::Nanosecond`].
///
/// # Errors
///
/// The smallest units must be no greater than [`Unit::Hour`].
///
/// # Example
///
/// ```
/// use jiff::{civil::{TimeRound, time}, Unit};
///
/// let t = time(3, 25, 30, 0);
/// assert_eq!(
/// t.round(TimeRound::new().smallest(Unit::Minute))?,
/// time(3, 26, 0, 0),
/// );
/// // Or, utilize the `From<Unit> for TimeRound` impl:
/// assert_eq!(t.round(Unit::Minute)?, time(3, 26, 0, 0));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn smallest(self, unit: Unit) -> TimeRound {
TimeRound { smallest: unit, ..self }
}
/// Set the rounding mode.
///
/// This defaults to [`RoundMode::HalfExpand`], which rounds away from
/// zero. It matches the kind of rounding you might have been taught in
/// school.
///
/// # Example
///
/// This shows how to always round times up towards positive infinity.
///
/// ```
/// use jiff::{civil::{Time, TimeRound, time}, RoundMode, Unit};
///
/// let t: Time = "03:25:01".parse()?;
/// assert_eq!(
/// t.round(
/// TimeRound::new()
/// .smallest(Unit::Minute)
/// .mode(RoundMode::Ceil),
/// )?,
/// time(3, 26, 0, 0),
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn mode(self, mode: RoundMode) -> TimeRound {
TimeRound { mode, ..self }
}
/// Set the rounding increment for the smallest unit.
///
/// The default value is `1`. Other values permit rounding the smallest
/// unit to the nearest integer increment specified. For example, if the
/// smallest unit is set to [`Unit::Minute`], then a rounding increment of
/// `30` would result in rounding in increments of a half hour. That is,
/// the only minute value that could result would be `0` or `30`.
///
/// # Errors
///
/// The rounding increment must divide evenly into the
/// next highest unit above the smallest unit set. The rounding increment
/// must also not be equal to the next highest unit. For example, if the
/// smallest unit is [`Unit::Nanosecond`], then *some* of the valid values
/// for the rounding increment are `1`, `2`, `4`, `5`, `100` and `500`.
/// Namely, any integer that divides evenly into `1,000` nanoseconds since
/// there are `1,000` nanoseconds in the next highest unit (microseconds).
///
/// # Example
///
/// This example shows how to round a time to the nearest 10 minute
/// increment.
///
/// ```
/// use jiff::{civil::{Time, TimeRound, time}, RoundMode, Unit};
///
/// let t: Time = "03:24:59".parse()?;
/// assert_eq!(t.round((Unit::Minute, 10))?, time(3, 20, 0, 0));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn increment(self, increment: i64) -> TimeRound {
TimeRound { increment, ..self }
}
/// Does the actual rounding.
pub(crate) fn round(&self, t: Time) -> Result<Time, Error> {
let increment = increment::for_time(self.smallest, self.increment)?;
let nanos = t.to_nanosecond();
let rounded = self.mode.round_by_unit_in_nanoseconds(
nanos,
self.smallest,
increment,
);
let limit =
t::NoUnits128::rfrom(t::CivilDayNanosecond::MAX_SELF) + C(1);
Ok(Time::from_nanosecond((rounded % limit).rinto()))
}
}
impl Default for TimeRound {
#[inline]
fn default() -> TimeRound {
TimeRound::new()
}
}
impl From<Unit> for TimeRound {
#[inline]
fn from(unit: Unit) -> TimeRound {
TimeRound::default().smallest(unit)
}
}
impl From<(Unit, i64)> for TimeRound {
#[inline]
fn from((unit, increment): (Unit, i64)) -> TimeRound {
TimeRound::from(unit).increment(increment)
}
}
/// A builder for setting the fields on a [`Time`].
///
/// This builder is constructed via [`Time::with`].
///
/// # Example
///
/// Unlike [`Date`], a [`Time`] is valid for all possible valid values of its
/// fields. That is, there is no way for two valid field values to combine
/// into an invalid `Time`. So, for `Time`, this builder does have as much of
/// a benefit versus an API design with methods like `Time::with_hour` and
/// `Time::with_minute`. Nevertheless, this builder permits settings multiple
/// fields at the same time and performing only one validity check. Moreover,
/// this provides a consistent API with other date and time types in this
/// crate.
///
/// ```
/// use jiff::civil::time;
///
/// let t1 = time(0, 0, 24, 0);
/// let t2 = t1.with().hour(15).minute(30).millisecond(10).build()?;
/// assert_eq!(t2, time(15, 30, 24, 10_000_000));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Clone, Copy, Debug)]
pub struct TimeWith {
original: Time,
hour: Option<i8>,
minute: Option<i8>,
second: Option<i8>,
millisecond: Option<i16>,
microsecond: Option<i16>,
nanosecond: Option<i16>,
subsec_nanosecond: Option<i32>,
}
impl TimeWith {
#[inline]
fn new(original: Time) -> TimeWith {
TimeWith {
original,
hour: None,
minute: None,
second: None,
millisecond: None,
microsecond: None,
nanosecond: None,
subsec_nanosecond: None,
}
}
/// Create a new `Time` from the fields set on this configuration.
///
/// An error occurs when the fields combine to an invalid time. This only
/// occurs when at least one field has an invalid value, or if at least
/// one of `millisecond`, `microsecond` or `nanosecond` is set _and_
/// `subsec_nanosecond` is set. Otherwise, if all fields are valid, then
/// the entire `Time` is guaranteed to be valid.
///
/// For any fields not set on this configuration, the values are taken from
/// the [`Time`] that originally created this configuration. When no values
/// are set, this routine is guaranteed to succeed and will always return
/// the original time without modification.
///
/// # Example
///
/// This creates a time but with its fractional nanosecond component
/// stripped:
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(14, 27, 30, 123_456_789);
/// assert_eq!(t.with().subsec_nanosecond(0).build()?, time(14, 27, 30, 0));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: error for invalid time
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(14, 27, 30, 0);
/// assert!(t.with().hour(24).build().is_err());
/// ```
///
/// # Example: error for ambiguous sub-second value
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(14, 27, 30, 123_456_789);
/// // Setting both the individual sub-second fields and the entire
/// // fractional component could lead to a misleading configuration. So
/// // if it's done, it results in an error in all cases. Callers must
/// // choose one or the other.
/// assert!(t.with().microsecond(1).subsec_nanosecond(0).build().is_err());
/// ```
#[inline]
pub fn build(self) -> Result<Time, Error> {
let hour = match self.hour {
None => self.original.hour_ranged(),
Some(hour) => Hour::try_new("hour", hour)?,
};
let minute = match self.minute {
None => self.original.minute_ranged(),
Some(minute) => Minute::try_new("minute", minute)?,
};
let second = match self.second {
None => self.original.second_ranged(),
Some(second) => Second::try_new("second", second)?,
};
let millisecond = match self.millisecond {
None => self.original.millisecond_ranged(),
Some(millisecond) => {
Millisecond::try_new("millisecond", millisecond)?
}
};
let microsecond = match self.microsecond {
None => self.original.microsecond_ranged(),
Some(microsecond) => {
Microsecond::try_new("microsecond", microsecond)?
}
};
let nanosecond = match self.nanosecond {
None => self.original.nanosecond_ranged(),
Some(nanosecond) => Nanosecond::try_new("nanosecond", nanosecond)?,
};
let subsec_nanosecond = match self.subsec_nanosecond {
None => self.original.subsec_nanosecond_ranged(),
Some(subsec_nanosecond) => {
if self.millisecond.is_some() {
return Err(err!(
"cannot set both TimeWith::millisecond \
and TimeWith::subsec_nanosecond",
));
}
if self.microsecond.is_some() {
return Err(err!(
"cannot set both TimeWith::microsecond \
and TimeWith::subsec_nanosecond",
));
}
if self.nanosecond.is_some() {
return Err(err!(
"cannot set both TimeWith::nanosecond \
and TimeWith::subsec_nanosecond",
));
}
SubsecNanosecond::try_new(
"subsec_nanosecond",
subsec_nanosecond,
)?
}
};
if self.subsec_nanosecond.is_some() {
Ok(Time::new_ranged(hour, minute, second, subsec_nanosecond))
} else {
Ok(Time::new_ranged(hour, minute, second, C(0))
.with_subsec_parts_ranged(
millisecond,
microsecond,
nanosecond,
))
}
}
/// Set the hour field on a [`Time`].
///
/// One can access this value via [`Time::hour`].
///
/// This overrides any previous hour settings.
///
/// # Errors
///
/// This returns an error when [`TimeWith::build`] is called if the given
/// hour is outside the range `0..=23`.
///
/// # Example
///
/// ```
/// use jiff::civil::time;
///
/// let t1 = time(15, 21, 59, 0);
/// assert_eq!(t1.hour(), 15);
/// let t2 = t1.with().hour(3).build()?;
/// assert_eq!(t2.hour(), 3);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn hour(self, hour: i8) -> TimeWith {
TimeWith { hour: Some(hour), ..self }
}
/// Set the minute field on a [`Time`].
///
/// One can access this value via [`Time::minute`].
///
/// This overrides any previous minute settings.
///
/// # Errors
///
/// This returns an error when [`TimeWith::build`] is called if the given
/// minute is outside the range `0..=59`.
///
/// # Example
///
/// ```
/// use jiff::civil::time;
///
/// let t1 = time(15, 21, 59, 0);
/// assert_eq!(t1.minute(), 21);
/// let t2 = t1.with().minute(3).build()?;
/// assert_eq!(t2.minute(), 3);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn minute(self, minute: i8) -> TimeWith {
TimeWith { minute: Some(minute), ..self }
}
/// Set the second field on a [`Time`].
///
/// One can access this value via [`Time::second`].
///
/// This overrides any previous second settings.
///
/// # Errors
///
/// This returns an error when [`TimeWith::build`] is called if the given
/// second is outside the range `0..=59`.
///
/// # Example
///
/// ```
/// use jiff::civil::time;
///
/// let t1 = time(15, 21, 59, 0);
/// assert_eq!(t1.second(), 59);
/// let t2 = t1.with().second(3).build()?;
/// assert_eq!(t2.second(), 3);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn second(self, second: i8) -> TimeWith {
TimeWith { second: Some(second), ..self }
}
/// Set the millisecond field on a [`Time`].
///
/// One can access this value via [`Time::millisecond`].
///
/// This overrides any previous millisecond settings.
///
/// # Errors
///
/// This returns an error when [`TimeWith::build`] is called if the given
/// millisecond is outside the range `0..=999`, or if both this and
/// [`TimeWith::subsec_nanosecond`] are set.
///
/// # Example
///
/// This shows the relationship between [`Time::millisecond`] and
/// [`Time::subsec_nanosecond`]:
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(15, 21, 35, 0).with().millisecond(123).build()?;
/// assert_eq!(t.subsec_nanosecond(), 123_000_000);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn millisecond(self, millisecond: i16) -> TimeWith {
TimeWith { millisecond: Some(millisecond), ..self }
}
/// Set the microsecond field on a [`Time`].
///
/// One can access this value via [`Time::microsecond`].
///
/// This overrides any previous microsecond settings.
///
/// # Errors
///
/// This returns an error when [`TimeWith::build`] is called if the given
/// microsecond is outside the range `0..=999`, or if both this and
/// [`TimeWith::subsec_nanosecond`] are set.
///
/// # Example
///
/// This shows the relationship between [`Time::microsecond`] and
/// [`Time::subsec_nanosecond`]:
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(15, 21, 35, 0).with().microsecond(123).build()?;
/// assert_eq!(t.subsec_nanosecond(), 123_000);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn microsecond(self, microsecond: i16) -> TimeWith {
TimeWith { microsecond: Some(microsecond), ..self }
}
/// Set the nanosecond field on a [`Time`].
///
/// One can access this value via [`Time::nanosecond`].
///
/// This overrides any previous nanosecond settings.
///
/// # Errors
///
/// This returns an error when [`TimeWith::build`] is called if the given
/// nanosecond is outside the range `0..=999`, or if both this and
/// [`TimeWith::subsec_nanosecond`] are set.
///
/// # Example
///
/// This shows the relationship between [`Time::nanosecond`] and
/// [`Time::subsec_nanosecond`]:
///
/// ```
/// use jiff::civil::time;
///
/// let t = time(15, 21, 35, 0).with().nanosecond(123).build()?;
/// assert_eq!(t.subsec_nanosecond(), 123);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn nanosecond(self, nanosecond: i16) -> TimeWith {
TimeWith { nanosecond: Some(nanosecond), ..self }
}
/// Set the subsecond nanosecond field on a [`Time`].
///
/// If you want to access this value on `Time`, then use
/// [`Time::subsec_nanosecond`].
///
/// This overrides any previous subsecond nanosecond settings.
///
/// # Errors
///
/// This returns an error when [`TimeWith::build`] is called if the given
/// subsecond nanosecond is outside the range `0..=999,999,999`, or if both
/// this and one of [`TimeWith::millisecond`], [`TimeWith::microsecond`] or
/// [`TimeWith::nanosecond`] are set.
///
/// # Example
///
/// This shows the relationship between constructing a `Time` value with
/// subsecond nanoseconds and its individual subsecond fields:
///
/// ```
/// use jiff::civil::time;
///
/// let t1 = time(15, 21, 35, 0);
/// let t2 = t1.with().subsec_nanosecond(123_456_789).build()?;
/// assert_eq!(t2.millisecond(), 123);
/// assert_eq!(t2.microsecond(), 456);
/// assert_eq!(t2.nanosecond(), 789);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn subsec_nanosecond(self, subsec_nanosecond: i32) -> TimeWith {
TimeWith { subsec_nanosecond: Some(subsec_nanosecond), ..self }
}
}
#[cfg(test)]
mod tests {
use std::io::Cursor;
use crate::{civil::time, span::span_eq, ToSpan};
use super::*;
#[test]
fn min() {
let t = Time::MIN;
assert_eq!(t.hour(), 0);
assert_eq!(t.minute(), 0);
assert_eq!(t.second(), 0);
assert_eq!(t.subsec_nanosecond(), 0);
}
#[test]
fn max() {
let t = Time::MAX;
assert_eq!(t.hour(), 23);
assert_eq!(t.minute(), 59);
assert_eq!(t.second(), 59);
assert_eq!(t.subsec_nanosecond(), 999_999_999);
}
#[test]
fn invalid() {
assert!(Time::new(24, 0, 0, 0).is_err());
assert!(Time::new(23, 60, 0, 0).is_err());
assert!(Time::new(23, 59, 60, 0).is_err());
assert!(Time::new(23, 59, 61, 0).is_err());
assert!(Time::new(-1, 0, 0, 0).is_err());
assert!(Time::new(0, -1, 0, 0).is_err());
assert!(Time::new(0, 0, -1, 0).is_err());
assert!(Time::new(0, 0, 0, 1_000_000_000).is_err());
assert!(Time::new(0, 0, 0, -1).is_err());
assert!(Time::new(23, 59, 59, 1_000_000_000).is_err());
assert!(Time::new(23, 59, 59, -1).is_err());
}
#[test]
fn rounding_cross_midnight() {
let t1 = time(23, 59, 59, 999_999_999);
let t2 = t1.round(Unit::Nanosecond).unwrap();
assert_eq!(t2, t1);
let t2 = t1.round(Unit::Millisecond).unwrap();
assert_eq!(t2, time(0, 0, 0, 0));
let t2 = t1.round(Unit::Microsecond).unwrap();
assert_eq!(t2, time(0, 0, 0, 0));
let t2 = t1.round(Unit::Millisecond).unwrap();
assert_eq!(t2, time(0, 0, 0, 0));
let t2 = t1.round(Unit::Second).unwrap();
assert_eq!(t2, time(0, 0, 0, 0));
let t2 = t1.round(Unit::Minute).unwrap();
assert_eq!(t2, time(0, 0, 0, 0));
let t2 = t1.round(Unit::Hour).unwrap();
assert_eq!(t2, time(0, 0, 0, 0));
let t1 = time(22, 15, 0, 0);
assert_eq!(
time(22, 30, 0, 0),
t1.round(TimeRound::new().smallest(Unit::Minute).increment(30))
.unwrap()
);
}
quickcheck::quickcheck! {
fn prop_ordering_same_as_civil_nanosecond(
civil_nanosecond1: CivilDayNanosecond,
civil_nanosecond2: CivilDayNanosecond
) -> bool {
let t1 = Time::from_nanosecond(civil_nanosecond1);
let t2 = Time::from_nanosecond(civil_nanosecond2);
t1.cmp(&t2) == civil_nanosecond1.cmp(&civil_nanosecond2)
}
fn prop_checked_add_then_sub(
time: Time,
nano_span: CivilDayNanosecond
) -> quickcheck::TestResult {
let span = Span::new().nanoseconds(nano_span.get());
let Ok(sum) = time.checked_add(span) else {
return quickcheck::TestResult::discard()
};
let diff = sum.checked_sub(span).unwrap();
quickcheck::TestResult::from_bool(time == diff)
}
fn prop_wrapping_add_then_sub(
time: Time,
nano_span: CivilDayNanosecond
) -> bool {
let span = Span::new().nanoseconds(nano_span.get());
let sum = time.wrapping_add(span);
let diff = sum.wrapping_sub(span);
time == diff
}
fn prop_checked_add_equals_wrapping_add(
time: Time,
nano_span: CivilDayNanosecond
) -> quickcheck::TestResult {
let span = Span::new().nanoseconds(nano_span.get());
let Ok(sum_checked) = time.checked_add(span) else {
return quickcheck::TestResult::discard()
};
let sum_wrapped = time.wrapping_add(span);
quickcheck::TestResult::from_bool(sum_checked == sum_wrapped)
}
fn prop_checked_sub_equals_wrapping_sub(
time: Time,
nano_span: CivilDayNanosecond
) -> quickcheck::TestResult {
let span = Span::new().nanoseconds(nano_span.get());
let Ok(diff_checked) = time.checked_sub(span) else {
return quickcheck::TestResult::discard()
};
let diff_wrapped = time.wrapping_sub(span);
quickcheck::TestResult::from_bool(diff_checked == diff_wrapped)
}
fn prop_until_then_add(t1: Time, t2: Time) -> bool {
let span = t1.until(t2).unwrap();
t1.checked_add(span).unwrap() == t2
}
fn prop_until_then_sub(t1: Time, t2: Time) -> bool {
let span = t1.until(t2).unwrap();
t2.checked_sub(span).unwrap() == t1
}
fn prop_since_then_add(t1: Time, t2: Time) -> bool {
let span = t1.since(t2).unwrap();
t2.checked_add(span).unwrap() == t1
}
fn prop_since_then_sub(t1: Time, t2: Time) -> bool {
let span = t1.since(t2).unwrap();
t1.checked_sub(span).unwrap() == t2
}
fn prop_until_is_since_negated(t1: Time, t2: Time) -> bool {
t1.until(t2).unwrap().get_nanoseconds()
== t1.since(t2).unwrap().negate().get_nanoseconds()
}
}
#[test]
fn overflowing_add() {
let t1 = time(23, 30, 0, 0);
let (t2, span) = t1.overflowing_add(5.hours()).unwrap();
assert_eq!(t2, time(4, 30, 0, 0));
span_eq!(span, 1.days());
}
#[test]
fn overflowing_add_overflows() {
let t1 = time(23, 30, 0, 0);
let span = Span::new()
.hours(t::SpanHours::MAX_REPR)
.minutes(t::SpanMinutes::MAX_REPR)
.seconds(t::SpanSeconds::MAX_REPR)
.milliseconds(t::SpanMilliseconds::MAX_REPR)
.microseconds(t::SpanMicroseconds::MAX_REPR)
.nanoseconds(t::SpanNanoseconds::MAX_REPR);
assert!(t1.overflowing_add(span).is_err());
}
#[test]
fn time_size() {
#[cfg(debug_assertions)]
{
assert_eq!(24, core::mem::size_of::<Time>());
}
#[cfg(not(debug_assertions))]
{
assert_eq!(8, core::mem::size_of::<Time>());
}
}
// This test checks that a wrapping subtraction with the minimum signed
// duration is as expected.
#[test]
fn wrapping_sub_signed_duration_min() {
let max = -SignedDuration::MIN.as_nanos();
let got = time(15, 30, 8, 999_999_999).to_nanosecond();
let expected = max.rem_euclid(t::NANOS_PER_CIVIL_DAY.bound());
assert_eq!(got, expected);
}
// This test checks that a wrapping subtraction with the maximum signed
// duration is as expected.
#[test]
fn wrapping_sub_signed_duration_max() {
let max = -SignedDuration::MAX.as_nanos();
let got = time(8, 29, 52, 1).to_nanosecond();
let expected = max.rem_euclid(t::NANOS_PER_CIVIL_DAY.bound());
assert_eq!(got, expected);
}
// This test checks that a wrapping subtraction with the maximum unsigned
// duration is as expected.
#[test]
fn wrapping_sub_unsigned_duration_max() {
let max =
-i128::try_from(std::time::Duration::MAX.as_nanos()).unwrap();
let got = time(16, 59, 44, 1).to_nanosecond();
let expected = max.rem_euclid(t::NANOS_PER_CIVIL_DAY.bound());
assert_eq!(got, expected);
}
/// # `serde` deserializer compatibility test
///
/// Serde YAML used to be unable to deserialize `jiff` types,
/// as deserializing from bytes is not supported by the deserializer.
///
/// - <https://github.com/BurntSushi/jiff/issues/138>
/// - <https://github.com/BurntSushi/jiff/discussions/148>
#[test]
fn civil_time_deserialize_yaml() {
let expected = time(16, 35, 4, 987654321);
let deserialized: Time =
serde_yaml::from_str("16:35:04.987654321").unwrap();
assert_eq!(deserialized, expected);
let deserialized: Time =
serde_yaml::from_slice("16:35:04.987654321".as_bytes()).unwrap();
assert_eq!(deserialized, expected);
let cursor = Cursor::new(b"16:35:04.987654321");
let deserialized: Time = serde_yaml::from_reader(cursor).unwrap();
assert_eq!(deserialized, expected);
}
}