|
747 | 747 | @test_throws Exception Dates.Date(nat3) |
748 | 748 | end |
749 | 749 | end |
| 750 | + |
| 751 | +@testitem "defaultunit" begin |
| 752 | + using Dates |
| 753 | + using PythonCall: NumpyDates |
| 754 | + |
| 755 | + # Helper: normalize any defaultunit result to a unitpair tuple |
| 756 | + norm(u) = NumpyDates.unitpair(u) |
| 757 | + |
| 758 | + # 1) Primitives and Dates.* types |
| 759 | + cases = [ |
| 760 | + # fallback Any (e.g. AbstractString) |
| 761 | + ("hello", :ns), |
| 762 | + |
| 763 | + # Dates.Date / Dates.DateTime |
| 764 | + (Date(2000, 1, 2), :D), |
| 765 | + (DateTime(2000, 1, 2, 3, 4, 5), :ms), |
| 766 | + |
| 767 | + # Dates.Periods => exact unit mapping |
| 768 | + (Year(1), :Y), |
| 769 | + (Month(1), :M), |
| 770 | + (Week(1), :W), |
| 771 | + (Day(1), :D), |
| 772 | + (Hour(1), :h), |
| 773 | + (Minute(1), :m), |
| 774 | + (Second(1), :s), |
| 775 | + (Millisecond(1), :ms), |
| 776 | + (Microsecond(1), :us), |
| 777 | + (Nanosecond(1), :ns), |
| 778 | + ] |
| 779 | + |
| 780 | + @testset "$x -> $u" for (x, u) in cases |
| 781 | + @test norm(NumpyDates.defaultunit(x)) == norm(u) |
| 782 | + end |
| 783 | + |
| 784 | + # 2) (Inline)DateTime64 should return their own unitpair |
| 785 | + @testset "AbstractDateTime64 instances" begin |
| 786 | + # DateTime64 |
| 787 | + dt_s = NumpyDates.DateTime64(0, :s) |
| 788 | + dt_ms = NumpyDates.DateTime64(0, :ms) |
| 789 | + @test norm(NumpyDates.defaultunit(dt_s)) == NumpyDates.unitpair(dt_s) |
| 790 | + @test norm(NumpyDates.defaultunit(dt_ms)) == NumpyDates.unitpair(dt_ms) |
| 791 | + |
| 792 | + # InlineDateTime64 typed |
| 793 | + idt_typed = NumpyDates.InlineDateTime64{NumpyDates.SECONDS}(0) |
| 794 | + @test norm(NumpyDates.defaultunit(idt_typed)) == NumpyDates.unitpair(idt_typed) |
| 795 | + |
| 796 | + # InlineDateTime64 dynamic |
| 797 | + idt_dyn = NumpyDates.InlineDateTime64(0, :us) |
| 798 | + @test norm(NumpyDates.defaultunit(idt_dyn)) == NumpyDates.unitpair(idt_dyn) |
| 799 | + end |
| 800 | + |
| 801 | + # 3) (Inline)TimeDelta64 should return their own unitpair |
| 802 | + @testset "AbstractTimeDelta64 instances" begin |
| 803 | + # TimeDelta64 |
| 804 | + td_ns = NumpyDates.TimeDelta64(0, :ns) |
| 805 | + td_ps = NumpyDates.TimeDelta64(0, :ps) |
| 806 | + @test norm(NumpyDates.defaultunit(td_ns)) == NumpyDates.unitpair(td_ns) |
| 807 | + @test norm(NumpyDates.defaultunit(td_ps)) == NumpyDates.unitpair(td_ps) |
| 808 | + |
| 809 | + # InlineTimeDelta64 typed |
| 810 | + itd_typed = NumpyDates.InlineTimeDelta64{NumpyDates.MINUTES}(0) |
| 811 | + @test norm(NumpyDates.defaultunit(itd_typed)) == NumpyDates.unitpair(itd_typed) |
| 812 | + |
| 813 | + # InlineTimeDelta64 dynamic |
| 814 | + itd_dyn = NumpyDates.InlineTimeDelta64(0, :as) |
| 815 | + @test norm(NumpyDates.defaultunit(itd_dyn)) == NumpyDates.unitpair(itd_dyn) |
| 816 | + end |
| 817 | +end |
0 commit comments