Skip to content

Commit

Permalink
0.4 only update
Browse files Browse the repository at this point in the history
  • Loading branch information
IainNZ committed Aug 30, 2015
1 parent c007ea3 commit cc9e937
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 47 deletions.
16 changes: 8 additions & 8 deletions .travis.yml
@@ -1,14 +1,14 @@
language: julia
os:
- linux
- osx
- linux
- osx
julia:
- release
- nightly
- nightly
notifications:
email: false
email: false
sudo: false
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia --check-bounds=yes -e 'Pkg.clone(pwd()); Pkg.build("Humanize"); Pkg.test("Humanize"; coverage=true)'
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia -e 'Pkg.clone(pwd()); Pkg.test("Humanize"; coverage=true)'
after_success:
- julia -e 'cd(Pkg.dir("Humanize")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
- julia -e 'cd(Pkg.dir("Humanize")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
13 changes: 8 additions & 5 deletions README.md
@@ -1,9 +1,11 @@
Humanize.jl
===========

[![Build Status](https://travis-ci.org/IainNZ/Humanize.jl.svg)](https://travis-ci.org/IainNZ/Humanize.jl)
[![Coverage Status](https://img.shields.io/coveralls/IainNZ/Humanize.jl.svg)](https://coveralls.io/r/IainNZ/Humanize.jl)
[![Humanize](http://pkg.julialang.org/badges/Humanize_release.svg)](http://pkg.julialang.org/?pkg=Humanize&ver=release)
[![Build Status](https://travis-ci.org/IainNZ/Humanize.jl.svg?branch=master)](https://travis-ci.org/IainNZ/Humanize.jl)
[![codecov.io](http://codecov.io/github/IainNZ/Humanize.jl/coverage.svg?branch=master)](http://codecov.io/github/IainNZ/Humanize.jl?branch=master)

[![Humanize](http://pkg.julialang.org/badges/Humanize_0.3.svg)](http://pkg.julialang.org/?pkg=Humanize&ver=release)
[![Humanize](http://pkg.julialang.org/badges/Humanize_0.4.svg)](http://pkg.julialang.org/?pkg=Humanize&ver=nightly)

Humanize numbers, including
* data sizes (`3e6 -> 3.0 MB or 2.9 MiB`).
Expand All @@ -16,6 +18,8 @@ This package is MIT licensed, and is based on [jmoiron's humanize Python library

## Documentation

All functions are also documented using Julia's in-built help system, e.g. `?datasize`.

### Data sizes

```julia
Expand Down Expand Up @@ -43,7 +47,6 @@ timedelta(d_diff::Dates.Day)
```

Turns a date/datetime difference into a abbreviated human-friendly form.
Supports the types defined by `Dates.jl` on Julia 0.3, `Base.Dates` on Julia 0.4.

```julia
julia> timedelta(70)
Expand All @@ -66,4 +69,4 @@ julia> digitsep(12345678, sep = "'")
"12'345'678"
julia> digitsep(12345678, sep = "-", k = 4)
"1234-5678"
```
```
3 changes: 1 addition & 2 deletions REQUIRE
@@ -1,2 +1 @@
julia 0.3
Dates
julia 0.4-
52 changes: 29 additions & 23 deletions src/Humanize.jl
Expand Up @@ -5,27 +5,25 @@
# All original code is (c) Iain Dunning and MIT licensed.
#----------------------------------------------------------------------

module Humanize
isdefined(Base, :__precompile__) && __precompile__()

if VERSION < v"0.4-"
import Dates
else
import Base.Dates
end
module Humanize

export datasize, timedelta, digitsep

#=---------------------------------------------------------------------
Format a number of bytes in a human-friendly format (eg. 10 kB).
style=:dec - default, decimal suffixes (kB, MB), base 10^3
style=:bin - binary suffixes (KiB, MiB), base 2^10
style=:gnu - GNU-style (ls -sh style, K, M), base 2^10
=#

#---------------------------------------------------------------------
const dec_suf = [" B", " kB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"]
const bin_suf = [" B", " KiB", " MiB", " GiB", " TiB", " PiB", " EiB", " ZiB", " YiB"]
const gnu_suf = ["", "K", "M", "G", "T", "P", "E", "Z", "Y"]
"""
datasize(value::Number; style=:dec, format="%.1f")
Format a number of bytes in a human-friendly format (eg. 10 kB).
style=:dec - default, decimal suffixes (kB, MB), base 10^3
style=:bin - binary suffixes (KiB, MiB), base 2^10
style=:gnu - GNU-style (ls -sh style, K, M), base 2^10
"""
function datasize(value::Number; style=:dec, format="%.1f")
suffix = style == :gnu ? gnu_suf : (style == :bin ? bin_suf : dec_suf)
base = style == :dec ? 1000.0 : 1024.0
Expand All @@ -41,8 +39,15 @@ function datasize(value::Number; style=:dec, format="%.1f")
return fmt_str(base * bytes / unit, s)
end

#=---------------------------------------------------------------------
#---------------------------------------------------------------------
"""
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)
Format a time length in a human friendly format.
timedelta(secs::Integer)
e.g. 3699 -> 'An hour'
timedelta{T<:Integer}(years::T,months::T,days::T,hours::T,mins::T,secs::T)
Expand All @@ -53,8 +58,7 @@ timedelta(dt_diff::Dates.Millisecond)
e.g. DateTime(2014,2,3) - DateTime(2013,3,7) -> '11 months'
timedelta(d_diff::Dates.Day)
e.g. Date(2014,3,7) - Date(2013,2,4) -> '1 year, 1 month'
=#

"""
function timedelta(secs::Integer)
mins = div( secs, 60); secs -= 60*mins
hours = div( mins, 60); mins -= 60*hours
Expand Down Expand Up @@ -82,11 +86,14 @@ end
# Assume nothing about magnitudes of inputs, so cast to seconds first
timedelta{T<:Integer}(years::T,months::T,days::T,hours::T,mins::T,secs::T) =
timedelta(((((years*12+months)*30+days)*24+hours)*60+mins)*60+secs)
timedelta(dt_diff::Dates.Millisecond) = timedelta(div(int(dt_diff),1000))
timedelta(d_diff::Dates.Day) = timedelta(int(d_diff)*24*3600)
timedelta(dt_diff::Dates.Millisecond) = timedelta(div(Int(dt_diff),1000))
timedelta(d_diff::Dates.Day) = timedelta(Int(d_diff)*24*3600)


#---------------------------------------------------------------------
"""
digitsep(value::Integer, sep = ",", k = 3)
#=---------------------------------------------------------------------
Convert an integer to a string, separating each 'k' digits by 'sep'. 'k'
defaults to 3, separating by thousands. The default "," for 'sep' matches the
commonly used digit separator in the US.
Expand All @@ -97,14 +104,13 @@ digitsep(value::Integer, sep = "'")
e.g. 12345678 -> "12'345'678"
digitsep(value::Integer, sep = "'", k = 4)
e.g. 12345678 -> "1234'5678"
=#

"""
function digitsep(value::Integer, sep = ",", k = 3)
value = string(value)
n = length(value)
starts = reverse([n:-k:1])
starts = reverse(collect(n:-k:1))
groups = [value[max(x-k+1, 1):x] for x in starts]
return join(groups, sep)
end

end
end
12 changes: 3 additions & 9 deletions test/runtests.jl
@@ -1,19 +1,13 @@
using Humanize
using Base.Test
if VERSION < v"0.4-"
import Dates
else
import Base.Dates
end


function test_datasize()
println("test_datasize")

tests = [300, 3000, 3000000, 3000000000, 3000000000000, 1e26, 3141592]
results = [:dec => ["450.000 B", "4.500 kB", "4.500 MB", "4.500 GB", "4.500 TB", "150.000 YB", "4.712 MB"],
:bin => ["630.000 B", "6.152 KiB", "6.008 MiB", "5.867 GiB", "5.730 TiB", "173.708 YiB", "6.292 MiB"],
:gnu => ["480.000", "4.688K", "4.578M", "4.470G", "4.366T", "132.349Y", "4.794M"]]
results = Dict( :dec => ["450.000 B", "4.500 kB", "4.500 MB", "4.500 GB", "4.500 TB", "150.000 YB", "4.712 MB"],
:bin => ["630.000 B", "6.152 KiB", "6.008 MiB", "5.867 GiB", "5.730 TiB", "173.708 YiB", "6.292 MiB"],
:gnu => ["480.000", "4.688K", "4.578M", "4.470G", "4.366T", "132.349Y", "4.794M"])
for (style,mult) in [(:dec,1.5), (:bin,2.1), (:gnu, 1.6)]
println(" $style")
for i in 1:length(tests)
Expand Down

0 comments on commit cc9e937

Please sign in to comment.