Skip to content

Commit

Permalink
Humanize 1.0 pr
Browse files Browse the repository at this point in the history
  • Loading branch information
IainNZ committed Aug 12, 2018
1 parent 17178af commit a4a0ba5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 57 deletions.
15 changes: 9 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
57 changes: 27 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand All @@ -11,57 +10,55 @@ 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

```julia
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"
```
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
julia 0.7-beta
julia 1.0 2.0
35 changes: 17 additions & 18 deletions src/Humanize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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])
Expand Down

0 comments on commit a4a0ba5

Please sign in to comment.