Skip to content

Commit aa8f0b5

Browse files
committed
Check length
1 parent da96506 commit aa8f0b5

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/UnixTimes.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,14 @@ UnixTime(x::Date, y::Time) =
6666
const DATEFORMAT = dateformat"yyyy-mm-ddTHH:MM:SS.sss"
6767

6868
function UnixTime(s::AbstractString)
69-
dt = DateTime(chop(s; tail=6), DATEFORMAT)
70-
ns = Nanosecond(parse(Int, last(s, 6)))
71-
UnixTime(dt) + ns
69+
try
70+
@assert length(s) == 29
71+
dt = DateTime(chop(s; tail=6), DATEFORMAT)
72+
ns = Nanosecond(parse(Int, last(s, 6)))
73+
UnixTime(dt) + ns
74+
catch
75+
throw(ArgumentError("Invalid UnixTime string"))
76+
end
7277
end
7378

7479
Base.convert(::Type{UnixTime}, x::DateTime) = UnixTime(x)

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ end
4848
x = UnixTime(2020, 1, 2, 3, 4, 5, 6, 7, 8)
4949
@test string(x) == "2020-01-02T03:04:05.006007008"
5050
@test UnixTime(string(x)) == x
51+
@test_throws ArgumentError UnixTime("2025-01-28T15:49:36.5901397023")
52+
@test_throws ArgumentError UnixTime("2025-01-28T15:49:36.59013970")
53+
@test_throws ArgumentError UnixTime(repeat("a", 29))
5154
end
5255

5356
@testset "arithmetic" begin

0 commit comments

Comments
 (0)