Skip to content

Commit 840aa33

Browse files
author
Pietro Vertechi
authored
Merge pull request #145 from piever/pv/time
validate time format before attempting to parse
2 parents 98dfa1b + 18960e3 commit 840aa33

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/input.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,19 @@ end
127127

128128
_parse(::Type{S}, x) where{S} = parse(S, x)
129129
function _parse(::Type{Dates.Time}, x)
130-
h, m = parse.(Int, split(x, ':'))
130+
segments = split(x, ':')
131+
length(segments) >= 2 && all(!isempty, segments) || return nothing
132+
h, m = parse.(Int, segments)
131133
Dates.Time(h, m)
132134
end
133135

136+
function _string(x::Dates.Time)
137+
h = Dates.hour(x)
138+
m = Dates.minute(x)
139+
string(lpad(h, 2, "0"), ":", lpad(m, 2, "0"))
140+
end
141+
_string(x::Dates.Date) = string(x)
142+
134143
"""
135144
`datepicker(value::Union{Dates.Date, Observable, Nothing}=nothing)`
136145
@@ -149,17 +158,18 @@ for (func, typ, str, unit) in [(:timepicker, :(Dates.Time), "time", Dates.Second
149158
@eval begin
150159
function $func(::WidgetTheme, val=nothing; value=val, kwargs...)
151160
(value isa AbstractObservable) || (value = Observable{Union{$typ, Nothing}}(value))
152-
f = x -> x === nothing ? "" : split(string(x), '.')[1]
161+
f = x -> x === nothing ? "" : _string(x)
153162
g = t -> _parse($typ, t)
154163
pair = ObservablePair(value, f=f, g=g)
155164
ui = input(pair.second; typ=$str, kwargs...)
156165
Widget{$(Expr(:quote, func))}(ui, output = value)
157166
end
158167

159168
function $func(T::WidgetTheme, vals::AbstractRange, val=medianelement(vals); value=val, kwargs...)
160-
f = x -> x === nothing ? "" : split(string(x), '.')[1]
169+
f = x -> x === nothing ? "" : _string(x)
161170
fs = x -> x === nothing ? "" : split(string(convert($unit, x)), ' ')[1]
162-
$func(T; value=value, min=f(minimum(vals)), max=f(maximum(vals)), step=fs(step(vals)), kwargs...)
171+
min, max = extrema(vals)
172+
$func(T; value=value, min=f(min), max=f(max), step=fs(step(vals)), kwargs...)
163173
end
164174
end
165175
end

0 commit comments

Comments
 (0)