diff --git a/.travis.yml b/.travis.yml index b9dbfe6..b183f30 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,13 @@ language: julia os: - - linux + - linux julia: - - 0.7 - - nightly + - 1.0 + - nightly notifications: - email: false -after_success: - - julia -e 'cd(Pkg.dir("Humanize")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())' + email: false +git: + depth: 99999999 +matrix: + allow_failures: + - julia: nightly \ No newline at end of file diff --git a/README.md b/README.md index 6e8f42e..e7854b9 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ Humanize.jl =========== [![Build Status](https://travis-ci.org/IainNZ/Humanize.jl.svg?branch=master)](https://travis-ci.org/IainNZ/Humanize.jl) -[![codecov](https://codecov.io/gh/IainNZ/Humanize.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/IainNZ/Humanize.jl) Humanize numbers, including * data sizes (`3e6 -> 3.0 MB or 2.9 MiB`). @@ -11,9 +10,6 @@ Humanize numbers, including This package is MIT licensed, and is based on [jmoiron's humanize Python library](https://github.com/jmoiron/humanize/). -## Documentation - -All functions are also documented using Julia's in-built help system, e.g. `?datasize`. ### Data sizes @@ -21,47 +17,48 @@ All functions are also documented using Julia's in-built help system, e.g. `?dat datasize(value::Number; style=:dec, format="%.1f") ``` -Style can be `:dec` (base 10^3), `:bin` (base 2^10), `:gnu` (base 2^10, like `ls -hs`). +Style can be + `:dec` (base 10^3), `:bin` (base 2^10), `:gnu` (base 2^10, like `ls -hs`). ```julia -julia> datasize(3000000) -"3.0 MB" -julia> datasize(3000000, style=:bin, format="%.3f") -"2.861 MiB" -julia> datasize(3000000, style=:gnu, format="%.3f") -"2.861M" +import Humanize: datasize +datasize(3000000) # "3.0 MB" +datasize(3000000, style=:bin, format="%.3f") # "2.861 MiB" +datasize(3000000, style=:gnu, format="%.3f") # "2.861M" ``` ### Date/datetime differences ```julia -timedelta(secs::Integer) -timedelta{T<:Integer}(years::T,months::T,days::T,hours::T,mins::T,secs::T) -timedelta(dt_diff::Dates.Millisecond) -timedelta(d_diff::Dates.Day) +timedelta(seconds::Integer) +timedelta(seconds::Dates.Second) +timedelta(Δdt::Dates.Millisecond) +timedelta(Δdate::Dates.Day) ``` Turns a date/datetime difference into a abbreviated human-friendly form. ```julia -julia> timedelta(70) -"a minute" -julia> timedelta(0,0,0,23,50,50) -"23 hours" -julia> timedelta(DateTime(2014,2,3,12,11,10) - - DateTime(2013,3,7,13,1,20)) -"11 months" -julia> timedelta(Date(2014,3,7) - Date(2013,2,4)) -"1 year, 1 month" +import Humanize: timedelta +timedelta(70) # "a minute" +import Dates: DateTime, Date +timedelta(DateTime(2014,2,3,12,11,10) - + DateTime(2013,3,7,13,1,20)) # "11 months" +timedelta(Date(2014,3,7) - Date(2013,2,4)) # "1 year, 1 month" ``` ### Digit separator ```julia -julia> digitsep(12345678) -"12,345,678" -julia> digitsep(12345678, sep = "'") -"12'345'678" -julia> digitsep(12345678, sep = "-", k = 4) -"1234-5678" +digitsep(value::Integer; separator=",", per_separator=3) ``` + +Convert an integer to a string, separating each `per_separator` digits by +`separator`. + +```julia +import Humanize: digitsep +digitsep(12345678) # "12,345,678" +digitsep(12345678, seperator= "'") # "12'345'678" +digitsep(12345678, seperator= "-", per_separator=4) # "1234-5678" +``` \ No newline at end of file diff --git a/REQUIRE b/REQUIRE index c5a584c..45954cb 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1 +1 @@ -julia 0.7-beta +julia 1.0 2.0 diff --git a/src/Humanize.jl b/src/Humanize.jl index cbb77e1..9bdc17f 100644 --- a/src/Humanize.jl +++ b/src/Humanize.jl @@ -34,7 +34,7 @@ function datasize(value::Number; style=:dec, format="%.1f")::String unit = base biggest_suffix = suffix[1] for power in 1:length(suffix) - unit = base ^ power + unit = base^power biggest_suffix = suffix[power] bytes < unit && break end @@ -45,26 +45,25 @@ end """ - timedelta(secs::Integer) + timedelta(seconds::Integer) timedelta(seconds::Dates.Second) timedelta(Δdt::Dates.Millisecond) timedelta(Δdate::Dates.Day) -Format a time length in a human friendly format. +Turns a date/datetime difference into a abbreviated human-friendly form. - timedelta(seconds::Integer) # 3699 -> "An hour". - timedelta(Δdt::Dates.Millisecond) - e.g. DateTime(2014,2,3) - DateTime(2013,3,7) -> "11 months". - timedelta(Δdate::Dates.Day) - e.g. Date(2014,3,7) - Date(2013,2,4) -> "1 year, 1 month". + timedelta(70) # "a minute" + timedelta(DateTime(2014,2,3,12,11,10) - + DateTime(2013,3,7,13,1,20)) # "11 months" + timedelta(Date(2014,3,7) - Date(2013,2,4)) # "1 year, 1 month" """ function timedelta(seconds::Integer) secs = seconds - mins = div( secs, 60); secs -= 60*mins - hours = div( mins, 60); mins -= 60*hours - days = div( hours, 24); hours -= 24*days - months = div( days, 30); days -= 30*months - years = div(months, 12); months -= 12*years + mins = div(secs, 60); secs -= 60 * mins + hours = div(mins, 60); mins -= 60 * hours + days = div(hours, 24); hours -= 24 * days + months = div(days, 30); days -= 30 * months + years = div(months, 12); months -= 12 * years if years == 0 if days == 0 && months == 0 @@ -89,16 +88,16 @@ timedelta(Δdate::Dates.Day) = timedelta(convert(Dates.Second, Δdate)) """ - digitsep(value::Integer, separator=",", per_separator=3) + digitsep(value::Integer; separator=",", per_separator=3) Convert an integer to a string, separating each `per_separator` digits by `separator`. - digitsep(value) # 12345678 -> "12,345,678". - digitsep(value, separator="'") # 12345678 -> "12'345'678". - digitsep(value, separator="/", per_separator=4) # 12345678 -> "1234/5678". + digitsep(12345678) # "12,345,678" + digitsep(12345678, seperator= "'") # "12'345'678" + digitsep(12345678, seperator= "-", per_separator=4) # "1234-5678" """ -function digitsep(value::Integer, seperator=",", per_separator=3) +function digitsep(value::Integer; seperator=",", per_separator=3) isnegative = value < zero(value) value = string(abs(value)) # Stringify, no seperators. # Figure out last character index of each group of digits. diff --git a/test/runtests.jl b/test/runtests.jl index 80a9dae..cb982cd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -31,7 +31,7 @@ end # testset datasize. ((0, 0, 0, 23, 50, 50), "23 hours"), ((0, 0, 1, 0, 0, 0), "a day"), ((0, 0, 500, 0, 0, 0), "1 year, 4 months"), - ((0, 0, 365*2 + 35, 0, 0, 0), "2 years"), + ((0, 0, 365 * 2 + 35, 0, 0, 0), "2 years"), ((0, 0, 10000, 0, 0, 0), "27 years"), ((0, 0, 365 + 30, 0, 0, 0), "1 year, 1 month"), ((0, 0, 365 + 4, 0, 0, 0), "a year"), @@ -41,7 +41,7 @@ end # testset datasize. ((0, 0, 365, 0, 0, 0), "a year")] @testset "datetime diff $output" for (inputs, output) in DATA - base_datetime = Dates.DateTime(2014,1,1,0,0,0) + base_datetime = Dates.DateTime(2014, 1, 1, 0, 0, 0) new_datetime = Dates.Year(inputs[1]) + base_datetime new_datetime += Dates.Month(inputs[2]) new_datetime += Dates.Day(inputs[3])