Skip to content

Commit 47166b3

Browse files
authored
Merge pull request #4041 from CliMA/feat-add-weeks-as-supported-yml
feat: add 'weeks' as supported yml file time unit
2 parents 30b4892 + a14f7c6 commit 47166b3

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/utils/utilities.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,22 +229,25 @@ time_to_seconds(t::Number) =
229229
"""
230230
time_to_seconds(s::String)
231231
232-
Convert a string representing a time to seconds. Supported units: seconds, minutes, hours, days as
233-
`s`, `secs`, `m`, `mins`, `h`, `hours`, `d`, `days`.
232+
Convert a string representing a time to seconds. Supported units: seconds, minutes, hours, days, weeks as
233+
`s`, `secs`, `m`, `mins`, `h`, `hours`, `d`, `days`, `weeks`.
234234
"""
235235
function time_to_seconds(s::String)
236236
s == "Inf" && return Inf
237237
# match a number followed by one of the supported units of time
238-
m = match(r"^(\d+(?:\.\d+)?)(s|secs|m|mins|h|hours|d|days)$", s)
238+
m = match(r"^(\d+(?:\.\d+)?)(s|secs|m|mins|h|hours|d|days|weeks)$", s)
239239
isnothing(m) &&
240-
error("Bad format for flag $s. Examples: `10secs`, `20mins`, `30hours`, `40days`")
240+
error(
241+
"Bad format for flag $s. Examples: `10secs`, `20mins`, `30hours`, `40days`, `50weeks`",
242+
)
241243
value = parse(Float64, m.captures[1])
242244
unit = m.captures[2]
243245
factor_groups = Dict(
244246
["s", "secs"] => 1,
245247
["m", "mins"] => 60,
246248
["h", "hours"] => 3600,
247249
["d", "days"] => 86400,
250+
["weeks"] => 604800,
248251
)
249252
factors = Dict(unit => val for (units, val) in factor_groups for unit in units)
250253
return value * factors[unit]

test/utilities.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ end
5050
@test CA.time_to_seconds("3d") == 3 * 86400
5151
@test CA.time_to_seconds("3days") == 3 * 86400
5252

53+
# Weeks
54+
@test CA.time_to_seconds("50weeks") == 50 * 604800
55+
5356
# Float input
5457
@test CA.time_to_seconds("1.5h") == 1.5 * 3600
5558

0 commit comments

Comments
 (0)