Skip to content

Commit

Permalink
Merge pull request #176 from tpgillam/date_format
Browse files Browse the repository at this point in the history
Allow date format on DefaultFormatter to be configured
  • Loading branch information
rofinn committed Jun 16, 2021
2 parents aea16b6 + adfe69f commit 00c6686
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Memento"
uuid = "f28f55f0-a522-5efc-85c2-fe41dfb9b2d9"
license = "MIT"
authors = ["Invenia Technical Computing"]
version = "1.1.2"
version = "1.2.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
12 changes: 7 additions & 5 deletions src/formatters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ log `Record`.
abstract type Formatter end

const DEFAULT_FMT_STRING = "[{level} | {name}]: {msg}"
const DATE_FMT_STRING = "yyyy-mm-dd HH:MM:SS"
const DEFAULT_DATE_FMT = "yyyy-mm-dd HH:MM:SS"

"""
DefaultFormatter
Expand All @@ -26,10 +26,12 @@ struct DefaultFormatter <: Formatter
fmt_str::AbstractString
tokens::Vector{Pair{Symbol, Bool}}
output_tz::Union{Dates.TimeZone, Nothing}
date_fmt_string::AbstractString

function DefaultFormatter(
fmt_str::AbstractString=DEFAULT_FMT_STRING,
output_tz=nothing,
output_tz=nothing;
date_fmt_string::AbstractString=DEFAULT_DATE_FMT,
)
#r"(?<={).+?(?=})
tokens = map(eachmatch(r"({.+?})|(.+?)", fmt_str)) do m
Expand All @@ -41,7 +43,7 @@ struct DefaultFormatter <: Formatter
end
end

new(fmt_str, tokens, output_tz)
new(fmt_str, tokens, output_tz, date_fmt_string)
end
end

Expand Down Expand Up @@ -80,9 +82,9 @@ function format(fmt::DefaultFormatter, rec::Record)
value = if tmp_val isa ZonedDateTime
# `localzone` is expensive, so we don't call it until it is required.
tzout = fmt.output_tz === nothing ? localzone() : fmt.output_tz
Dates.format(astimezone(tmp_val, tzout), DATE_FMT_STRING)
Dates.format(astimezone(tmp_val, tzout), fmt.date_fmt_string)
elseif tmp_val isa DateTime
Dates.format(tmp_val, DATE_FMT_STRING)
Dates.format(tmp_val, fmt.date_fmt_string)
else
tmp_val
end
Expand Down
11 changes: 11 additions & 0 deletions test/formatters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@
@test Memento.format(fmt, ts_rec) == string(ts)
end
end

@testset "Date format string" begin
date = ZonedDateTime(2012, 1, 1, 3, 1, 1, 123, localzone())
formats = ["yyyy", "yyyy-mm-dd HH:MM:SS.s"]
date_strings = ["2012", "2012-01-01 03:01:01.123"]
for (format, date_string) in zip(formats, date_strings)
formatter = DefaultFormatter("{date}"; date_fmt_string=format)
record = SimpleRecord("info", "moo", date)
@test Memento.format(formatter, record) == date_string
end
end
end

@testset "DictFormatter" begin
Expand Down

2 comments on commit 00c6686

@rofinn
Copy link
Member Author

@rofinn rofinn commented on 00c6686 Jun 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/38968

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.2.0 -m "<description of version>" 00c6686922e368426d2c21d2ad9cccae1ba6e9f5
git push origin v1.2.0

Please sign in to comment.