Skip to content

Commit edac6f7

Browse files
maleadtKeno
authored andcommitted
Execute AM/PM Dates tests in the C locale. (#34371)
1 parent 0403fd5 commit edac6f7

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

stdlib/Dates/test/io.jl

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -515,25 +515,36 @@ end
515515
end
516516

517517
@testset "AM/PM" begin
518-
for (t12,t24) in (("12:00am","00:00"), ("12:07am","00:07"), ("01:24AM","01:24"),
519-
("12:00pm","12:00"), ("12:15pm","12:15"), ("11:59PM","23:59"))
520-
d = DateTime("2018-01-01T$t24:00")
521-
t = Time("$t24:00")
522-
for HH in ("HH","II")
523-
@test DateTime("2018-01-01 $t12","yyyy-mm-dd $HH:MMp") == d
524-
@test Time("$t12","$HH:MMp") == t
518+
# get the current locale
519+
LC_TIME = 2
520+
time_locale = ccall(:setlocale, Cstring, (Cint, Cstring), LC_TIME, C_NULL)
521+
try
522+
# set the locale
523+
ccall(:setlocale, Cstring, (Cint, Cstring), LC_TIME, "C")
524+
525+
for (t12,t24) in (("12:00am","00:00"), ("12:07am","00:07"), ("01:24AM","01:24"),
526+
("12:00pm","12:00"), ("12:15pm","12:15"), ("11:59PM","23:59"))
527+
d = DateTime("2018-01-01T$t24:00")
528+
t = Time("$t24:00")
529+
for HH in ("HH","II")
530+
@test DateTime("2018-01-01 $t12","yyyy-mm-dd $HH:MMp") == d
531+
@test Time("$t12","$HH:MMp") == t
532+
end
533+
tmstruct = Libc.strptime("%I:%M%p", t12)
534+
@test Time(tmstruct) == t
535+
@test uppercase(t12) == Dates.format(t, "II:MMp") ==
536+
Dates.format(d, "II:MMp") ==
537+
Libc.strftime("%I:%M%p", tmstruct)
525538
end
526-
tmstruct = Libc.strptime("%I:%M%p", t12)
527-
@test Time(tmstruct) == t
528-
@test uppercase(t12) == Dates.format(t, "II:MMp") ==
529-
Dates.format(d, "II:MMp") ==
530-
Libc.strftime("%I:%M%p", tmstruct)
531-
end
532-
for bad in ("00:24am", "00:24pm", "13:24pm", "2pm", "12:24p.m.", "12:24 pm", "12:24pµ")
533-
@eval @test_throws ArgumentError Time($bad, "II:MMp")
539+
for bad in ("00:24am", "00:24pm", "13:24pm", "2pm", "12:24p.m.", "12:24 pm", "12:24pµ")
540+
@eval @test_throws ArgumentError Time($bad, "II:MMp")
541+
end
542+
# if am/pm is missing, defaults to 24-hour clock
543+
@eval Time("13:24", "II:MMp") == Time("13:24", "HH:MM")
544+
finally
545+
# recover the locale
546+
ccall(:setlocale, Cstring, (Cint, Cstring), LC_TIME, time_locale)
534547
end
535-
# if am/pm is missing, defaults to 24-hour clock
536-
@eval Time("13:24", "II:MMp") == Time("13:24", "HH:MM")
537548
end
538549

539550
end

0 commit comments

Comments
 (0)