Skip to content

Commit

Permalink
Add test documenting JuliaLang#10561, two-digit year parsing (JuliaLa…
Browse files Browse the repository at this point in the history
…ng#39654)

* Add test documenting JuliaLang#10561, two-digit year parsing

* Update stdlib/Dates/test/io.jl

Co-authored-by: Curtis Vogt <[email protected]>

* Update stdlib/Dates/test/io.jl

Co-authored-by: Curtis Vogt <[email protected]>

* Simplify test behavior

* Simplify test

* Tweak parse direction

* Clean up formatting (and trigger new build)

* Update stdlib/Dates/test/io.jl

Co-authored-by: Curtis Vogt <[email protected]>

* Update stdlib/Dates/test/io.jl

Co-authored-by: Curtis Vogt <[email protected]>

Co-authored-by: Curtis Vogt <[email protected]>
  • Loading branch information
2 people authored and ElOceanografo committed May 4, 2021
1 parent 7918c08 commit 4b30a8b
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions stdlib/Dates/test/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ end
@test sprint(show, DateFormat("ddxmm").tokens[2]) == "Delim(x)"
@test sprint(show, DateFormat("xxmmxx").tokens[2]) == "DatePart(mm)"
end

@testset "Common Parsing Patterns" begin
#'1996-January-15'
dt = Dates.DateTime(1996, 1, 15)
Expand Down Expand Up @@ -252,6 +253,7 @@ end
@test Dates.Date(string(Dates.Date(dt))) == Dates.Date(dt)
@test Dates.DateTime(string(dt)) == dt
end

@testset "prefix." begin
s = "/1996/1/15"
f = "/yyyy/m/d"
Expand All @@ -260,6 +262,7 @@ end
@test Dates.format(dt, f) == s
@test_throws ArgumentError Dates.DateTime("1996/1/15", f)
end

@testset "French and Chinese" begin
# from Jiahao
@test Dates.Date("2009年12月01日", "yyyy年mm月dd日") == Dates.Date(2009, 12, 1)
Expand Down Expand Up @@ -302,6 +305,7 @@ end
# doesn't parse month name greater than 4 chars
@test_throws ArgumentError Dates.Date("28avril2014", f; locale="french")
end

@testset "year digits parsing" begin
# From Tony Fong
f = "dduuuyy"
Expand Down Expand Up @@ -357,6 +361,7 @@ end

@test typeof(Dates.Date.(dr)) == Array{Date, 1}
end

@testset "Issue 13" begin
t = Dates.DateTime(1, 1, 1, 14, 51, 0, 118)
@test Dates.DateTime("[14:51:00.118]", "[HH:MM:SS.sss]") == t
Expand All @@ -366,6 +371,7 @@ end
@test Dates.DateTime("x14:51:00.118", "xHH:MM:SS.sss") == t
@test Dates.DateTime("14:51:00.118]", "HH:MM:SS.sss]") == t
end

@testset "RFC1123Format" begin
dt = Dates.DateTime(2014, 8, 23, 17, 22, 15)
@test Dates.format(dt, Dates.RFC1123Format) == "Sat, 23 Aug 2014 17:22:15"
Expand Down Expand Up @@ -394,6 +400,7 @@ end
@test parse(Dates.DateTime, "Mon, 12 Nov 2016 07:45:36", Dates.RFC1123Format) == dt # Wrong day of week
@test_throws ArgumentError parse(Date, "Foo, 12 Nov 2016 07:45:36", Dates.RFC1123Format)
end

@testset "Issue 15195" begin
f = "YY"
@test Dates.format(Dates.Date(1999), f) == "1999"
Expand Down Expand Up @@ -444,6 +451,7 @@ end
@test_throws ArgumentError Dates.Date("Apr 01 xx 2014", "uuu dd zz yyyy")
@test_throws ArgumentError Dates.Date("Apr 01 xx 2014", "uuu dd yyyy")
end

@testset "Issue 21001" begin
for (ms, str) in zip([0, 1, 20, 300, 450, 678], ["0", "001", "02", "3", "45", "678"])
local dt = DateTime(2000, 1, 1, 0, 0, 0, ms)
Expand All @@ -453,6 +461,7 @@ end
@test Dates.format(dt, "ssss") == rpad(str, 4, '0')
end
end

# Issue #21504
@test tryparse(Dates.Date, "0-1000") === nothing

Expand Down Expand Up @@ -510,21 +519,29 @@ end

@testset "midnight" begin
# issue #28203: 24:00 is a valid ISO 8601 time
@test DateTime("2018-01-01 24:00","yyyy-mm-dd HH:MM") == DateTime("2018-01-02T00:00:00") ==
DateTime(2018, 1, 1, 24) == DateTime(2018, 1, 2)
@test_throws ArgumentError DateTime("2018-01-01 24:01","yyyy-mm-dd HH:MM")
@test DateTime("2018-01-01 24:00", "yyyy-mm-dd HH:MM") ==
DateTime("2018-01-02T00:00:00") ==
DateTime(2018, 1, 1, 24) ==
DateTime(2018, 1, 2)
@test_throws ArgumentError DateTime("2018-01-01 24:01", "yyyy-mm-dd HH:MM")
@test_throws ArgumentError DateTime(2018, 1, 1, 24, 0, 1)
@test_throws ArgumentError DateTime(2018, 1, 1, 24, 0, 0, 1)
end

@testset "AM/PM" begin
for (t12,t24) in (("12:00am","00:00"), ("12:07am","00:07"), ("01:24AM","01:24"),
("12:00pm","12:00"), ("12:15pm","12:15"), ("11:59PM","23:59"))
for (t12, t24) in (
("12:00am", "00:00"),
("12:07am", "00:07"),
("01:24AM", "01:24"),
("12:00pm", "12:00"),
("12:15pm", "12:15"),
("11:59PM", "23:59"),
)
d = DateTime("2018-01-01T$t24:00")
t = Time("$t24:00")
for HH in ("HH","II")
@test DateTime("2018-01-01 $t12","yyyy-mm-dd $HH:MMp") == d
@test Time("$t12","$HH:MMp") == t
for HH in ("HH", "II")
@test DateTime("2018-01-01 $t12", "yyyy-mm-dd $HH:MMp") == d
@test Time("$t12", "$HH:MMp") == t
end
local tmstruct, strftime
withlocales(["C"]) do
Expand All @@ -534,9 +551,10 @@ end
nothing
end
@test Time(tmstruct) == t
@test uppercase(t12) == Dates.format(t, "II:MMp") ==
Dates.format(d, "II:MMp") ==
strftime
@test uppercase(t12) ==
Dates.format(t, "II:MMp") ==
Dates.format(d, "II:MMp") ==
strftime
end
for bad in ("00:24am", "00:24pm", "13:24pm", "2pm", "12:24p.m.", "12:24 pm", "12:24pµ")
@test_throws ArgumentError Time(bad, "II:MMp")
Expand All @@ -545,4 +563,13 @@ end
@test Time("13:24", "II:MMp") == Time("13:24", "HH:MM")
end

@testset "Issue #10561, two-digit year parsing ambiguities" begin
# All two-digit dates (whether full or resulting from truncation, e.g. 2010 -> 10)
# encoded in two digit year format YY are parsed as year 00YY

for test_year in ("00", "01", "99", "2021")
@test Date(test_year, dateformat"yy") == Date(parse(Int, test_year))
end
end

end

0 comments on commit 4b30a8b

Please sign in to comment.