Skip to content

Commit

Permalink
correct time formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavnatarajan committed Oct 15, 2022
1 parent 8f3b271 commit 16ed871
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RedClust"
uuid = "bf1adee6-87fe-4679-8d23-51fe99940a25"
authors = ["Abhinav Natarajan <[email protected]>"]
version = "0.2.1"
version = "0.2.2"

[deps]
Clustering = "aaaa29a8-35af-508c-8bc3-b662a17a0fe5"
Expand Down
5 changes: 5 additions & 0 deletions docs/src/changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Changelog
## [0.2.2]

### Fixed
- corrected time not printing in the correct format.
- moved `prettytime` and `prettynumber` to `utils.jl`.

## [0.2.1]

Expand Down
24 changes: 0 additions & 24 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,27 +286,3 @@ end
function Base.show(io::IO, result::MCMCResult)
print(io, "MCMC result with $(result.options.numsamples) samples")
end

function prettytime(t)
if t < 1e-6
x = Dates.canonicalize(Dates.Nanosecond(Integer(floor(t * 1e9))))
elseif t 1e-6 && t < 1e-3
x = Dates.canonicalize(Dates.Microsecond(Integer(floor(t * 1e6))))
elseif t 1e-3 && t < 1
x = Dates.canonicalize(Dates.Millisecond(Integer(floor(t * 1e3))))
else
x = Dates.canonicalize(Dates.Second(Integer(floor(t))))
end
out = @match x.periods[1] begin
_::Nanosecond => @sprintf("%.3f ns", t * 1e9)
_::Microsecond => @sprintf("%.3f μs", t * 1e6)
_::Millisecond => @sprintf("%.3f μs", t * 1e3)
_::Second => @sprintf("%.3f s", t)
_::Minute => @sprintf("%dm%ds", x.periods[1].value, x.periods[2].value)
_::Hour => @sprintf("%dh%dm%ds", x.periods[1].value, x.periods[2].value, x.periods[3].value)
_::Day => @sprintf("%dD%dh%dm", x.periods[1].value, x.periods[2].value, x.periods[3].value)
end
out
end

prettynumber(x) = x < 1 ? @sprintf("%.3e", x) : @sprintf("%.3f", x)
35 changes: 34 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,37 @@ Convert a vector of vectors into a matrix, where each vector becomes a column in
"""
function makematrix(x::AbstractVector{<:AbstractVector})::Matrix
[x[i][j] for j in 1:length(x[1]), i in 1:length(x)]
end
end

function prettytime(t)
if t < 1e-6 # nanoseconds
out = @sprintf "%.2f ns" t * 1e9
elseif t 1e-6 && t < 1e-3 # microseconds
out = @sprintf "%.2f μs" t * 1e6
elseif t 1e-3 && t < 1 # milliseconds
out = @sprintf "%.2f ms" t * 1e3
elseif t < 60 # seconds
out = @sprintf "%.2f s" t
else
x = Dates.canonicalize(Dates.Second(Integer(floor(t))))
out = ""
for i in 1:length(x.periods)
out *= string(x.periods[i].value) * " "
if typeof(x.periods[i]) == Second
out *= "s"
elseif typeof(x.periods[i]) == Minute
out *= "min" * (x.periods[i].value != 1 ? "s" : "")
elseif typeof(x.periods[i]) == Hour
out *= "hr" * (x.periods[i].value != 1 ? "s" : "")
elseif typeof(x.periods[i]) == Day
out *= "day" * (x.periods[i].value != 1 ? "s" : "")
end
if i != length(x.periods)
out *= " "
end
end
end
out
end

prettynumber(x) = x < 1 ? @sprintf("%.3e", x) : @sprintf("%.3f", x)
22 changes: 22 additions & 0 deletions test/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,26 @@ end
@test sum(x[inds1, inds2]) RedClust.matsum(x, inds1, inds2)
@test sum(v) RedClust.vecsum(v)
@test sum(v[inds1]) RedClust.vecsum(v, inds1)
end

@testset "printing time" begin
@test RedClust.prettytime(1e-9) == "1.00 ns"
@test RedClust.prettytime(999e-9) == "999.00 ns"
@test RedClust.prettytime(1e-6) == "1.00 μs"
@test RedClust.prettytime(999e-6) == "999.00 μs"
@test RedClust.prettytime(1e-3) == "1.00 ms"
@test RedClust.prettytime(999e-3) == "999.00 ms"
@test RedClust.prettytime(1) == "1.00 s"
@test RedClust.prettytime(5) == "5.00 s"
@test RedClust.prettytime(60) == "1 min"
@test RedClust.prettytime(120) == "2 mins"
@test RedClust.prettytime(65) == "1 min 5 s"
@test RedClust.prettytime(125) == "2 mins 5 s"
@test RedClust.prettytime(3600) == "1 hr"
@test RedClust.prettytime(7200) == "2 hrs"
@test RedClust.prettytime(7205) == "2 hrs 5 s"
@test RedClust.prettytime(7265) == "2 hrs 1 min 5 s"
@test RedClust.prettytime(7325) == "2 hrs 2 mins 5 s"
@test RedClust.prettytime(24 * 3600) == "1 day"
@test RedClust.prettytime(2 * 24 * 3600) == "2 days"
end

2 comments on commit 16ed871

@abhinavnatarajan
Copy link
Owner Author

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/70304

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 v0.2.2 -m "<description of version>" 16ed871937c47d65e6ff7f567094167a10048246
git push origin v0.2.2

Please sign in to comment.