From 16ed871937c47d65e6ff7f567094167a10048246 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Sat, 15 Oct 2022 20:35:34 +0530 Subject: [PATCH] correct time formatting --- Project.toml | 2 +- docs/src/changelog.md | 5 +++++ src/types.jl | 24 ------------------------ src/utils.jl | 35 ++++++++++++++++++++++++++++++++++- test/test_utils.jl | 22 ++++++++++++++++++++++ 5 files changed, 62 insertions(+), 26 deletions(-) diff --git a/Project.toml b/Project.toml index 6ee7ecc..064bc89 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "RedClust" uuid = "bf1adee6-87fe-4679-8d23-51fe99940a25" authors = ["Abhinav Natarajan "] -version = "0.2.1" +version = "0.2.2" [deps] Clustering = "aaaa29a8-35af-508c-8bc3-b662a17a0fe5" diff --git a/docs/src/changelog.md b/docs/src/changelog.md index b7e313e..1d8224b 100644 --- a/docs/src/changelog.md +++ b/docs/src/changelog.md @@ -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] diff --git a/src/types.jl b/src/types.jl index c83c7ae..f18e44a 100644 --- a/src/types.jl +++ b/src/types.jl @@ -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) diff --git a/src/utils.jl b/src/utils.jl index 7d141df..85ead9b 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -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 \ No newline at end of file +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) \ No newline at end of file diff --git a/test/test_utils.jl b/test/test_utils.jl index 4d76866..7199b29 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -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 \ No newline at end of file