[med-svn] [r-cran-prettyunits] 03/05: New upstream version 1.0.2

Andreas Tille tille at debian.org
Fri Sep 29 20:47:43 UTC 2017


This is an automated email from the git hooks/post-receive script.

tille pushed a commit to branch master
in repository r-cran-prettyunits.

commit aca9929d9366f63e1d6189e65a70263f5b9d477a
Author: Andreas Tille <tille at debian.org>
Date:   Fri Sep 29 22:45:53 2017 +0200

    New upstream version 1.0.2
---
 DESCRIPTION                    |  19 ++++
 LICENSE                        |   2 +
 MD5                            |  22 ++++
 NAMESPACE                      |  12 ++
 R/pretty-package.r             |   9 ++
 R/sizes.r                      |  41 +++++++
 R/time-ago.r                   | 164 ++++++++++++++++++++++++++++
 R/time.r                       | 107 ++++++++++++++++++
 R/utils.r                      |  17 +++
 debian/README.test             |   8 --
 debian/changelog               |   5 -
 debian/compat                  |   1 -
 debian/control                 |  26 -----
 debian/copyright               |  33 ------
 debian/docs                    |   2 -
 debian/rules                   |   5 -
 debian/source/format           |   1 -
 debian/tests/control           |   5 -
 debian/tests/run-unit-test     |  17 ---
 debian/watch                   |   2 -
 inst/NEWS.md                   |  10 ++
 inst/README.Rmd                | 105 ++++++++++++++++++
 inst/README.md                 | 242 +++++++++++++++++++++++++++++++++++++++++
 man/pretty_bytes.Rd            |  25 +++++
 man/pretty_dt.Rd               |  30 +++++
 man/pretty_ms.Rd               |  31 ++++++
 man/pretty_sec.Rd              |  31 ++++++
 man/prettyunits.Rd             |  11 ++
 man/time_ago.Rd                |  56 ++++++++++
 man/vague_dt.Rd                |  51 +++++++++
 tests/testthat.R               |   4 +
 tests/testthat/test-bytes.r    |  51 +++++++++
 tests/testthat/test-ms.r       | 111 +++++++++++++++++++
 tests/testthat/test-vague-dt.r |  46 ++++++++
 34 files changed, 1197 insertions(+), 105 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
new file mode 100644
index 0000000..4abf038
--- /dev/null
+++ b/DESCRIPTION
@@ -0,0 +1,19 @@
+Package: prettyunits
+Title: Pretty, Human Readable Formatting of Quantities
+Version: 1.0.2
+Author: Gabor Csardi
+Maintainer: Gabor Csardi <csardi.gabor at gmail.com>
+Description: Pretty, human readable formatting of quantities.
+    Time intervals: 1337000 -> 15d 11h 23m 20s.
+    Vague time intervals: 2674000 -> about a month ago.
+    Bytes: 1337 -> 1.34 kB.
+License: MIT + file LICENSE
+LazyData: true
+URL: https://github.com/gaborcsardi/prettyunits
+BugReports: https://github.com/gaborcsardi/prettyunits/issues
+Imports: magrittr, assertthat, methods
+Suggests: testthat
+NeedsCompilation: no
+Packaged: 2015-07-13 02:03:57 UTC; gaborcsardi
+Repository: CRAN
+Date/Publication: 2015-07-13 04:09:56
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..00834d3
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,2 @@
+YEAR: 2014
+COPYRIGHT HOLDER: Gabor Csardi
diff --git a/MD5 b/MD5
new file mode 100644
index 0000000..477b864
--- /dev/null
+++ b/MD5
@@ -0,0 +1,22 @@
+a38a241a307ccea87b37d73ee5a638d1 *DESCRIPTION
+3d55ef9a10c04c95a867277c02d32bca *LICENSE
+a721a0a6adc44cbe1ea3e164bb1fed5c *NAMESPACE
+a4bc058206feaddf8edcd3d2dc21ccfd *R/pretty-package.r
+98451cf71d340e5d5ede32637c0bcecd *R/sizes.r
+c68dedcc1c36d9de65d8f9b43c57e010 *R/time-ago.r
+e67460d79bcf4c37ca1c185b4051bbf0 *R/time.r
+58e328fd294f4ade5025fb393bd1ca56 *R/utils.r
+60bc0664f12e7e4a8c6ee49618db5672 *inst/NEWS.md
+48619686b38568ba65f28c288304aab9 *inst/README.Rmd
+c157c0891b7911a7865b61b8ce18df83 *inst/README.md
+7ce684cd37b50f2584569b0986629e21 *man/pretty_bytes.Rd
+4d8ce90ab48c9780255edb907745df62 *man/pretty_dt.Rd
+f73d3b114199084105f6cc16774218b8 *man/pretty_ms.Rd
+4fca9fc697f5ddb99f7ad0b3b4cb06b3 *man/pretty_sec.Rd
+c2fe310a01a0e92bcd4c9fbb05764fb9 *man/prettyunits.Rd
+8b48a38660d5171d5ffb3433893c3bd2 *man/time_ago.Rd
+3e98c1ddb8e7480ff2c4e9008eb64924 *man/vague_dt.Rd
+d4df5b35b4ce6e070b4980c38847cad3 *tests/testthat.R
+e59a4dc5477c4387d8cb97eb914c9b8c *tests/testthat/test-bytes.r
+472ebe383f5377d40c6687d64374d49c *tests/testthat/test-ms.r
+50c54d52ed56e60a64709770a7d38412 *tests/testthat/test-vague-dt.r
diff --git a/NAMESPACE b/NAMESPACE
new file mode 100644
index 0000000..b6bd37b
--- /dev/null
+++ b/NAMESPACE
@@ -0,0 +1,12 @@
+# Generated by roxygen2 (4.1.1): do not edit by hand
+
+export(pretty_bytes)
+export(pretty_dt)
+export(pretty_ms)
+export(pretty_sec)
+export(time_ago)
+export(vague_dt)
+importFrom(assertthat,assert_that)
+importFrom(assertthat,is.string)
+importFrom(magrittr,"%>%")
+importFrom(methods,is)
diff --git a/R/pretty-package.r b/R/pretty-package.r
new file mode 100644
index 0000000..27fc757
--- /dev/null
+++ b/R/pretty-package.r
@@ -0,0 +1,9 @@
+
+#' Prettier formatting of quantities
+#'
+#' @docType package
+#' @name prettyunits
+#' @importFrom magrittr %>%
+NULL
+
+. <- "STFU"
diff --git a/R/sizes.r b/R/sizes.r
new file mode 100644
index 0000000..ebf0179
--- /dev/null
+++ b/R/sizes.r
@@ -0,0 +1,41 @@
+
+#' Bytes in a human readable string
+#'
+#' @param bytes Numeric vector, number of bytes.
+#' @return Character vector, the formatted sizes.
+#'
+#' @export
+#' @examples
+#' pretty_bytes(1337)
+#' pretty_bytes(133337)
+#' pretty_bytes(13333337)
+#' pretty_bytes(1333333337)
+#' pretty_bytes(133333333337)
+
+pretty_bytes <- function(bytes) {
+
+  stopifnot(is.numeric(bytes))
+
+  units <- c('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
+
+  neg <- bytes < 0 & !is.na(bytes)
+  bytes <- abs(bytes)
+
+  exponent <- pmin(floor(log(bytes, 1000)), length(units) - 1)
+  res <- round(bytes / 1000 ^ exponent, 2)
+  unit <- units[exponent + 1]
+
+  ## Zero bytes
+  res[bytes == 0] <- 0
+  unit[bytes == 0] <- units[1]
+
+  ## NA and NaN bytes
+  res[is.na(bytes)] <- NA_real_
+  res[is.nan(bytes)] <- NaN
+  unit[is.na(bytes)] <- "B"            # Includes NaN as well
+
+  ## String
+  res <- format(ifelse(neg, -1, 1) * res, scientific = FALSE)
+
+  "%s %s" %s% list(res, unit)
+}
diff --git a/R/time-ago.r b/R/time-ago.r
new file mode 100644
index 0000000..3cdfaf4
--- /dev/null
+++ b/R/time-ago.r
@@ -0,0 +1,164 @@
+
+e <- expression
+
+vague_dt_default <- list(
+  list(c = e(seconds < 10), s = "moments ago"),
+  list(c = e(seconds < 45), s = "less than a minute ago"),
+  list(c = e(seconds < 90), s = "about a minute ago"),
+  list(c = e(minutes < 45), s = e("%d minutes ago" %s% round(minutes))),
+  list(c = e(minutes < 90), s = "about an hour ago"),
+  list(c = e(hours < 24),   s = e("%d hours ago" %s% round(hours))),
+  list(c = e(hours < 42),   s = "a day ago"),
+  list(c = e(days < 30),    s = e("%d days ago" %s% round(days))),
+  list(c = e(days < 45),    s = "about a month ago"),
+  list(c = e(days < 335),   s = e("%d months ago" %s% round(days / 30))),
+  list(c = e(years < 1.5),  s = "about a year ago"),
+  list(c = TRUE,            s = e("%d years ago" %s% round(years)))
+)
+
+vague_dt_short <- list(
+  list(c = e(seconds < 50), s = "<1 min"),
+  list(c = e(minutes < 50), s = e("%d min" %s% round(minutes))),
+  list(c = e(hours < 1.5),  s = "1 hour"),
+  list(c = e(hours < 18),   s = e("%d hours" %s% round(hours))),
+  list(c = e(hours < 42),   s = "1 day"),
+  list(c = e(days < 30),    s = e("%d day" %s% round(days))),
+  list(c = e(days < 45),    s = "1 mon"),
+  list(c = e(days < 335),   s = e("%d mon" %s% round(days / 30))),
+  list(c = e(years < 1.5),  s = "1 year"),
+  list(c = TRUE,            s = e("%d years" %s% round(years)))
+)
+
+vague_dt_terse <- list(
+  list(c = e(seconds < 50), s = e("%2ds" %s% round(seconds))),
+  list(c = e(minutes < 50), s = e("%2dm" %s% round(minutes))),
+  list(c = e(hours < 18),   s = e("%2dh" %s% round(hours))),
+  list(c = e(days < 30),    s = e("%2dd" %s% round(days))),
+  list(c = e(days < 335),   s = e("%2dM" %s% round(days / 30))),
+  list(c = TRUE,            s = e("%2dy" %s% round(years)))
+)
+
+vague_dt_formats <- list(
+  "default" = vague_dt_default,
+  "short" = vague_dt_short,
+  "terse" = vague_dt_terse
+)
+
+#' Human readable format of the time interval since a time point
+#'
+#' It calls \code{\link{vague_dt}} to do the actual formatting.
+#'
+#' @param date Date(s), \code{as.POSIXct} will be called on them.
+#' @param format Format, currently available formats are:
+#'   \sQuote{default}, \sQuote{short}, \sQuote{terse}. See examples below.
+#' @return Character vector of the formatted time intervals.
+#'
+#' @export
+#' @examples
+#' now <- Sys.time()
+#'
+#' time_ago(now)
+#' time_ago(now - as.difftime(30, units = "secs"))
+#' time_ago(now - as.difftime(14, units = "mins"))
+#' time_ago(now - as.difftime(5, units = "hours"))
+#' time_ago(now - as.difftime(25, units = "hours"))
+#' time_ago(now - as.difftime(5, units = "days"))
+#' time_ago(now - as.difftime(30, units = "days"))
+#' time_ago(now - as.difftime(365, units = "days"))
+#' time_ago(now - as.difftime(365 * 10, units = "days"))
+#'
+#' ## Short format
+#' time_ago(format = "short", now)
+#' time_ago(format = "short", now - as.difftime(30, units = "secs"))
+#' time_ago(format = "short", now - as.difftime(14, units = "mins"))
+#' time_ago(format = "short", now - as.difftime(5, units = "hours"))
+#' time_ago(format = "short", now - as.difftime(25, units = "hours"))
+#' time_ago(format = "short", now - as.difftime(5, units = "days"))
+#' time_ago(format = "short", now - as.difftime(30, units = "days"))
+#' time_ago(format = "short", now - as.difftime(365, units = "days"))
+#' time_ago(format = "short", now - as.difftime(365 * 10, units = "days"))
+#'
+#' ## Even shorter, terse format, (almost always) exactly 3 characters wide
+#' time_ago(format = "terse", now)
+#' time_ago(format = "terse", now - as.difftime(30, units = "secs"))
+#' time_ago(format = "terse", now - as.difftime(14, units = "mins"))
+#' time_ago(format = "terse", now - as.difftime(5, units = "hours"))
+#' time_ago(format = "terse", now - as.difftime(25, units = "hours"))
+#' time_ago(format = "terse", now - as.difftime(5, units = "days"))
+#' time_ago(format = "terse", now - as.difftime(30, units = "days"))
+#' time_ago(format = "terse", now - as.difftime(365, units = "days"))
+#' time_ago(format = "terse", now - as.difftime(365 * 10, units = "days"))
+
+time_ago <- function(date, format = c("default", "short", "terse")) {
+
+  date <- as.POSIXct(date)
+
+  if (length(date) > 1) return(sapply(date, time_ago, format = format))
+
+  seconds <- Sys.time() %>%
+    difftime(date, units = "secs")
+
+  vague_dt(seconds, format = format)
+}
+
+#' Human readable format of a time interval
+#'
+#' @param dt A \code{difftime} object, the time interval(s).
+#' @param format Format, currently available formats are:
+#'   \sQuote{default}, \sQuote{short}, \sQuote{terse}. See examples below.
+#' @return Character vector of the formatted time intervals.
+#'
+#' @export
+#' @examples
+#' vague_dt(as.difftime(30, units = "secs"))
+#' vague_dt(as.difftime(14, units = "mins"))
+#' vague_dt(as.difftime(5, units = "hours"))
+#' vague_dt(as.difftime(25, units = "hours"))
+#' vague_dt(as.difftime(5, units = "days"))
+#' vague_dt(as.difftime(30, units = "days"))
+#' vague_dt(as.difftime(365, units = "days"))
+#' vague_dt(as.difftime(365 * 10, units = "days"))
+#'
+#' ## Short format
+#' vague_dt(format = "short", as.difftime(30, units = "secs"))
+#' vague_dt(format = "short", as.difftime(14, units = "mins"))
+#' vague_dt(format = "short", as.difftime(5, units = "hours"))
+#' vague_dt(format = "short", as.difftime(25, units = "hours"))
+#' vague_dt(format = "short", as.difftime(5, units = "days"))
+#' vague_dt(format = "short", as.difftime(30, units = "days"))
+#' vague_dt(format = "short", as.difftime(365, units = "days"))
+#' vague_dt(format = "short", as.difftime(365 * 10, units = "days"))
+#'
+#' ## Even shorter, terse format, (almost always) exactly 3 characters wide
+#' vague_dt(format = "terse", as.difftime(30, units = "secs"))
+#' vague_dt(format = "terse", as.difftime(14, units = "mins"))
+#' vague_dt(format = "terse", as.difftime(5, units = "hours"))
+#' vague_dt(format = "terse", as.difftime(25, units = "hours"))
+#' vague_dt(format = "terse", as.difftime(5, units = "days"))
+#' vague_dt(format = "terse", as.difftime(30, units = "days"))
+#' vague_dt(format = "terse", as.difftime(365, units = "days"))
+#' vague_dt(format = "terse", as.difftime(365 * 10, units = "days"))
+
+vague_dt <- function(dt, format = c("default", "short", "terse")) {
+
+  assert_diff_time(dt)
+
+  units(dt) <- "secs"
+  seconds <- as.vector(dt)
+
+  ## Simplest to quit here for empty input
+  if (!length(seconds)) return(character())
+
+  pieces <- list(
+    minutes = seconds / 60,
+    hours = seconds / 60 / 60,
+    days = seconds / 60 / 60 / 24,
+    years = seconds / 60 / 60 / 24 / 365.25
+  )
+
+  format <- match.arg(format)
+
+  for (p in vague_dt_formats[[format]]) {
+    if (eval(p$c, pieces)) return(eval(p$s, pieces))
+  }
+}
diff --git a/R/time.r b/R/time.r
new file mode 100644
index 0000000..7d4e77b
--- /dev/null
+++ b/R/time.r
@@ -0,0 +1,107 @@
+
+parse_ms <- function(ms) {
+  stopifnot(is.numeric(ms))
+
+  data.frame(
+    days = floor(ms / 86400000),
+    hours = floor((ms / 3600000) %% 24),
+    minutes = floor((ms / 60000) %% 60),
+    seconds = round((ms / 1000) %% 60, 1)
+  )
+}
+
+first_positive <- function(x) which(x > 0)[1]
+
+trim <- function (x) gsub("^\\s+|\\s+$", "", x)
+
+#' Pretty formatting of milliseconds
+#'
+#' @param ms Numeric vector of milliseconds
+#' @param compact If true, then only the first non-zero
+#'   unit is used. See examples below.
+#' @return Character vector of formatted time intervals.
+#'
+#' @family time
+#' @export
+#' @examples
+#' pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000))
+#'
+#' pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000),
+#'           compact = TRUE)
+
+pretty_ms <- function(ms, compact = FALSE) {
+
+  stopifnot(is.numeric(ms))
+
+  parsed <- parse_ms(ms) %>% t()
+
+  if (compact) {
+    units <- c("d", "h", "m", "s")
+    parsed2 <- parsed
+    parsed2[] <- paste0(parsed, units)
+    idx <- apply(parsed, 2, first_positive) %>%
+      cbind(seq_len(length(ms)))
+    parsed2[idx] %>%
+      paste0("~", .)
+
+  } else {
+
+    ## Exact for small ones
+    exact <- paste0(ceiling(ms), "ms")
+
+    ## Approximate for others, in seconds
+    merge_pieces <- function(pieces) {
+      (
+        (if (pieces[1]) pieces[1] %+% "d " else "") %+%
+        (if (pieces[2]) pieces[2] %+% "h " else "") %+%
+        (if (pieces[3]) pieces[3] %+% "m " else "") %+%
+        (if (pieces[4]) pieces[4] %+% "s " else "")
+      )
+    }
+    approx <- trim(apply(parsed, 2, merge_pieces))
+
+    ifelse(ms < 1000, exact, approx)
+  }
+}
+
+#' Pretty formatting of seconds
+#'
+#' @param sec Numeric vector of seconds.
+#' @return Character vector of formatted time intervals.
+#'
+#' @inheritParams pretty_ms
+#' @family time
+#' @export
+#' @examples
+#' pretty_sec(c(1337, 13370, 133700, 1337000, 13370000))
+#'
+#' pretty_sec(c(1337, 13370, 133700, 1337000, 13370000),
+#'            compact = TRUE)
+
+pretty_sec <- function(sec, compact = FALSE) {
+  pretty_ms(sec * 1000, compact = compact)
+}
+
+#' Pretty formatting of time intervals (difftime objects)
+#'
+#' @param dt A \code{difftime} object, a vector of time
+#'   differences.
+#' @return Character vector of formatted time intervals.
+#'
+#' @inheritParams pretty_ms
+#' @family time
+#' @export
+#' @importFrom methods is
+#' @examples
+#' pretty_dt(as.difftime(1000, units = "secs"))
+#' pretty_dt(as.difftime(0, units = "secs"))
+
+pretty_dt <- function(dt, compact = FALSE) {
+
+  stopifnot(is(dt, "difftime"))
+
+  units(dt) <- "secs"
+
+  as.vector(dt) %>%
+    pretty_sec(compact = compact)
+}
diff --git a/R/utils.r b/R/utils.r
new file mode 100644
index 0000000..f7a5122
--- /dev/null
+++ b/R/utils.r
@@ -0,0 +1,17 @@
+
+#' @importFrom assertthat assert_that is.string
+
+`%s%` <- function(lhs, rhs) {
+  assert_that(is.string(lhs))
+  list(lhs) %>%
+    c(as.list(rhs)) %>%
+    do.call(what = sprintf)
+}
+
+`%+%` <- function(lhs, rhs) {
+  paste0(lhs, rhs)
+}
+
+assert_diff_time <- function(x) {
+  stopifnot(inherits(x, "difftime"))
+}
diff --git a/debian/README.test b/debian/README.test
deleted file mode 100644
index 53fb4d7..0000000
--- a/debian/README.test
+++ /dev/null
@@ -1,8 +0,0 @@
-Notes on how this package can be tested.
-────────────────────────────────────────
-
-This package can be tested by running the provided test:
-
-   sh ./run-unit-test
-
-in order to confirm its integrity.
diff --git a/debian/changelog b/debian/changelog
deleted file mode 100644
index 0de9172..0000000
--- a/debian/changelog
+++ /dev/null
@@ -1,5 +0,0 @@
-r-cran-prettyunits (1.0.2-1) unstable; urgency=medium
-
-  * Initial release (closes: #848658)
-
- -- Andreas Tille <tille at debian.org>  Mon, 19 Dec 2016 11:28:57 +0100
diff --git a/debian/compat b/debian/compat
deleted file mode 100644
index f599e28..0000000
--- a/debian/compat
+++ /dev/null
@@ -1 +0,0 @@
-10
diff --git a/debian/control b/debian/control
deleted file mode 100644
index 232a2bb..0000000
--- a/debian/control
+++ /dev/null
@@ -1,26 +0,0 @@
-Source: r-cran-prettyunits
-Maintainer: Debian Med Packaging Team <debian-med-packaging at lists.alioth.debian.org>
-Uploaders: Andreas Tille <tille at debian.org>
-Section: gnu-r
-Priority: optional
-Build-Depends: debhelper (>= 10),
-               dh-r,
-               r-base-dev,
-               r-cran-magrittr,
-               r-cran-assertthat
-Standards-Version: 3.9.8
-Vcs-Browser: https://anonscm.debian.org/viewvc/debian-med/trunk/packages/R/r-cran-prettyunits/trunk/
-Vcs-Svn: svn://anonscm.debian.org/debian-med/trunk/packages/R/r-cran-prettyunits/trunk/
-Homepage: https://cran.r-project.org/package=prettyunits
-
-Package: r-cran-prettyunits
-Architecture: all
-Depends: ${R:Depends},
-         ${misc:Depends}
-Recommends: ${R:Recommends}
-Suggests: ${R:Suggests}
-Description: GNU R pretty, human readable formatting of quantities
- Pretty, human readable formatting of quantities.
- Time intervals: 1337000 -> 15d 11h 23m 20s.
- Vague time intervals: 2674000 -> about a month ago.
- Bytes: 1337 -> 1.34 kB.
diff --git a/debian/copyright b/debian/copyright
deleted file mode 100644
index 67ced9a..0000000
--- a/debian/copyright
+++ /dev/null
@@ -1,33 +0,0 @@
-Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
-Upstream-Name: prettyunits
-Upstream-Contact: Gabor Csardi <csardi.gabor at gmail.com>
-Source: https://cran.r-project.org/package=prettyunits
-
-Files: *
-Copyright: 2014-2015 Gabor Csardi
-License: MIT
-Comment: 
-
-Files: debian/*
-Copyright: 2016 Andreas Tille <tille at debian.org>
-License: MIT
-
-License: MIT
- Permission is hereby granted, free of charge, to any person obtaining
- a copy of this software and associated documentation files (the
- "Software"), to deal in the Software without restriction, including
- without limitation the rights to use, copy, modify, merge, publish,
- distribute, sublicense, and/or sell copies of the Software, and to
- permit persons to whom the Software is furnished to do so, subject to
- the following conditions:
- .
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- .
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/debian/docs b/debian/docs
deleted file mode 100644
index 67ce40b..0000000
--- a/debian/docs
+++ /dev/null
@@ -1,2 +0,0 @@
-debian/tests/run-unit-test
-tests
diff --git a/debian/rules b/debian/rules
deleted file mode 100755
index 529c38a..0000000
--- a/debian/rules
+++ /dev/null
@@ -1,5 +0,0 @@
-#!/usr/bin/make -f
-
-%:
-	dh $@ --buildsystem R
-
diff --git a/debian/source/format b/debian/source/format
deleted file mode 100644
index 163aaf8..0000000
--- a/debian/source/format
+++ /dev/null
@@ -1 +0,0 @@
-3.0 (quilt)
diff --git a/debian/tests/control b/debian/tests/control
deleted file mode 100644
index a62fb6e..0000000
--- a/debian/tests/control
+++ /dev/null
@@ -1,5 +0,0 @@
-Tests: run-unit-test
-Depends: @, r-cran-testthat,
-Restrictions: allow-stderr
-
-
diff --git a/debian/tests/run-unit-test b/debian/tests/run-unit-test
deleted file mode 100644
index 9d0ca1f..0000000
--- a/debian/tests/run-unit-test
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/sh -e
-
-pkgname=prettyunits
-debname=r-cran-prettyunits
-
-if [ "$ADTTMP" = "" ] ; then
-    ADTTMP=`mktemp -d /tmp/${debname}-test.XXXXXX`
-    trap "rm -rf $ADTTMP" 0 INT QUIT ABRT PIPE TERM
-fi
-cd $ADTTMP
-cp -a /usr/share/doc/$debname/tests/* $ADTTMP
-gunzip -r *
-for testfile in *.R; do
-    echo "BEGIN TEST $testfile"
-    LC_ALL=C R --no-save < $testfile
-done
-
diff --git a/debian/watch b/debian/watch
deleted file mode 100644
index 80a2864..0000000
--- a/debian/watch
+++ /dev/null
@@ -1,2 +0,0 @@
-version=4
-https://cran.r-project.org/src/contrib/prettyunits_([-\d.]*)\.tar\.gz
diff --git a/inst/NEWS.md b/inst/NEWS.md
new file mode 100644
index 0000000..e28015f
--- /dev/null
+++ b/inst/NEWS.md
@@ -0,0 +1,10 @@
+
+# 1.0.2
+
+First version with a NEWS file.
+
+* Get rid of `R CMD check` notes.
+
+# 1.0.0
+
+Last version without a NEWS file.
diff --git a/inst/README.Rmd b/inst/README.Rmd
new file mode 100644
index 0000000..a5d5d01
--- /dev/null
+++ b/inst/README.Rmd
@@ -0,0 +1,105 @@
+
+```{r, setup, echo = FALSE, message = FALSE}
+knitr::opts_chunk$set(
+  comment = "##>",
+  tidy = FALSE,
+  error = FALSE)
+```
+
+[![Linux Build Status](https://travis-ci.org/gaborcsardi/prettyunits.svg?branch=master)](https://travis-ci.org/gaborcsardi/prettyunits)
+[![Windows Build status](https://ci.appveyor.com/api/projects/status/github/gaborcsardi/prettyunits?svg=true)](https://ci.appveyor.com/project/gaborcsardi/prettyunits)
+[![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/prettyunits)](http://cran.r-project.org/web/packages/prettyunits/index.html)
+
+
+# prettyunits
+
+The `prettyunits` package formats quantities in human readable form. Currently
+time units and information (i.e. bytes) are supported.
+
+## Installation
+
+You can install the package from github:
+
+```{r eval = FALSE}
+library(devtools)
+install_github("gaborcsardi/prettyunits")
+```
+
+```{r}
+library(prettyunits)
+library(magrittr)
+```
+
+## Bytes
+
+`pretty_bytes` formats number of bytes in a human readable way:
+
+```{r}
+pretty_bytes(1337)
+pretty_bytes(133337)
+pretty_bytes(13333337)
+pretty_bytes(1333333337)
+pretty_bytes(133333333337)
+```
+
+Here is a simple function that emulates the Unix `ls` command, with
+nicely formatted file sizes:
+
+```{r}
+uls <- function(path = ".") {
+  files <- dir(path)
+  info <- files %>%
+    lapply(file.info) %>%
+    do.call(what = rbind)
+  info$size <- pretty_bytes(info$size)
+  df <- data.frame(d = ifelse(info$isdir, "d", " "),
+	mode = as.character(info$mode), user = info$uname, group = info$grname,
+    size = ifelse(info$isdir, "", info$size), modified = info$mtime, name = files)
+  print(df, row.names = FALSE)
+}
+uls()
+```
+
+## Time intervals
+
+`pretty_ms` formats a time interval given in milliseconds. `pretty_sec` does
+the same for seconds, and `pretty_dt` for `difftime` objects. The optional
+`compact` argument turns on a compact, approximate format.
+
+```{r}
+pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000))
+pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000),
+  compact = TRUE)
+pretty_sec(c(1337, 13370, 133700, 1337000, 13370000))
+pretty_sec(c(1337, 13370, 133700, 1337000, 13370000),
+  compact = TRUE)
+
+```
+
+## Vague time intervals
+
+`vague_dt` and `time_ago` formats time intervals using a vague format,
+omitting smaller units. They both have three formats: `default`, `short` and `terse`.
+`vague_dt` takes a `difftime` object, and `time_ago` works relatively to the
+specified date.
+
+```{r}
+vague_dt(format = "short", as.difftime(30, units = "secs"))
+vague_dt(format = "short", as.difftime(14, units = "mins"))
+vague_dt(format = "short", as.difftime(5, units = "hours"))
+vague_dt(format = "short", as.difftime(25, units = "hours"))
+vague_dt(format = "short", as.difftime(5, units = "days"))
+```
+
+```{r}
+now <- Sys.time()
+time_ago(now)
+time_ago(now - as.difftime(30, units = "secs"))
+time_ago(now - as.difftime(14, units = "mins"))
+time_ago(now - as.difftime(5, units = "hours"))
+time_ago(now - as.difftime(25, units = "hours"))
+```
+
+```{r}
+
+```
diff --git a/inst/README.md b/inst/README.md
new file mode 100644
index 0000000..d5becf1
--- /dev/null
+++ b/inst/README.md
@@ -0,0 +1,242 @@
+
+
+
+[![Linux Build Status](https://travis-ci.org/gaborcsardi/prettyunits.svg?branch=master)](https://travis-ci.org/gaborcsardi/prettyunits)
+[![Windows Build status](https://ci.appveyor.com/api/projects/status/github/gaborcsardi/prettyunits?svg=true)](https://ci.appveyor.com/project/gaborcsardi/prettyunits)
+[![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/prettyunits)](http://cran.r-project.org/web/packages/prettyunits/index.html)
+
+
+# prettyunits
+
+The `prettyunits` package formats quantities in human readable form. Currently
+time units and information (i.e. bytes) are supported.
+
+## Installation
+
+You can install the package from github:
+
+
+```r
+library(devtools)
+install_github("gaborcsardi/prettyunits")
+```
+
+
+```r
+library(prettyunits)
+library(magrittr)
+```
+
+## Bytes
+
+`pretty_bytes` formats number of bytes in a human readable way:
+
+
+```r
+pretty_bytes(1337)
+```
+
+```
+##> [1] "1.34 kB"
+```
+
+```r
+pretty_bytes(133337)
+```
+
+```
+##> [1] "133.34 kB"
+```
+
+```r
+pretty_bytes(13333337)
+```
+
+```
+##> [1] "13.33 MB"
+```
+
+```r
+pretty_bytes(1333333337)
+```
+
+```
+##> [1] "1.33 GB"
+```
+
+```r
+pretty_bytes(133333333337)
+```
+
+```
+##> [1] "133.33 GB"
+```
+
+Here is a simple function that emulates the Unix `ls` command, with
+nicely formatted file sizes:
+
+
+```r
+uls <- function(path = ".") {
+  files <- dir(path)
+  info <- files %>%
+    lapply(file.info) %>%
+    do.call(what = rbind)
+  info$size <- pretty_bytes(info$size)
+  df <- data.frame(d = ifelse(info$isdir, "d", " "),
+	mode = as.character(info$mode), user = info$uname, group = info$grname,
+    size = ifelse(info$isdir, "", info$size), modified = info$mtime, name = files)
+  print(df, row.names = FALSE)
+}
+uls()
+```
+
+```
+##>  d mode        user group      size            modified         name
+##>     644 gaborcsardi staff  795.00 B 2014-10-13 09:00:43 appveyor.yml
+##>     644 gaborcsardi staff  561.00 B 2014-10-13 09:19:59  DESCRIPTION
+##>     644 gaborcsardi staff   42.00 B 2014-10-03 15:44:54      LICENSE
+##>     644 gaborcsardi staff  111.00 B 2014-10-12 23:07:32     Makefile
+##>  d  755 gaborcsardi staff           2014-10-12 16:51:06          man
+##>     644 gaborcsardi staff  259.00 B 2014-10-12 16:51:39    NAMESPACE
+##>  d  755 gaborcsardi staff           2014-10-12 16:47:25            R
+##>     644 gaborcsardi staff   4.46 kB 2014-10-13 09:04:42    README.md
+##>     644 gaborcsardi staff   2.98 kB 2015-04-23 15:18:17   README.Rmd
+##>     644 gaborcsardi staff   4.01 kB 2015-03-14 14:42:48         tags
+##>  d  755 gaborcsardi staff           2014-10-12 15:15:48        tests
+```
+
+## Time intervals
+
+`pretty_ms` formats a time interval given in milliseconds. `pretty_sec` does
+the same for seconds, and `pretty_dt` for `difftime` objects. The optional
+`compact` argument turns on a compact, approximate format.
+
+
+```r
+pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000))
+```
+
+```
+##> [1] "1.3s"            "13.4s"           "2m 13.7s"        "22m 17s"        
+##> [5] "15d 11h 23m 20s"
+```
+
+```r
+pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000),
+  compact = TRUE)
+```
+
+```
+##> [1] "~1.3s"  "~13.4s" "~2m"    "~22m"   "~15d"
+```
+
+```r
+pretty_sec(c(1337, 13370, 133700, 1337000, 13370000))
+```
+
+```
+##> [1] "22m 17s"          "3h 42m 50s"       "1d 13h 8m 20s"   
+##> [4] "15d 11h 23m 20s"  "154d 17h 53m 20s"
+```
+
+```r
+pretty_sec(c(1337, 13370, 133700, 1337000, 13370000),
+  compact = TRUE)
+```
+
+```
+##> [1] "~22m"  "~3h"   "~1d"   "~15d"  "~154d"
+```
+
+## Vague time intervals
+
+`vague_dt` and `time_ago` formats time intervals using a vague format,
+omitting smaller units. They both have three formats: `default`, `short` and `terse`.
+`vague_dt` takes a `difftime` object, and `time_ago` works relatively to the
+specified date.
+
+
+```r
+vague_dt(format = "short", as.difftime(30, units = "secs"))
+```
+
+```
+##> [1] "<1 min"
+```
+
+```r
+vague_dt(format = "short", as.difftime(14, units = "mins"))
+```
+
+```
+##> [1] "14 min"
+```
+
+```r
+vague_dt(format = "short", as.difftime(5, units = "hours"))
+```
+
+```
+##> [1] "5 hours"
+```
+
+```r
+vague_dt(format = "short", as.difftime(25, units = "hours"))
+```
+
+```
+##> [1] "1 day"
+```
+
+```r
+vague_dt(format = "short", as.difftime(5, units = "days"))
+```
+
+```
+##> [1] "5 day"
+```
+
+
+```r
+now <- Sys.time()
+time_ago(now)
+```
+
+```
+##> [1] "moments ago"
+```
+
+```r
+time_ago(now - as.difftime(30, units = "secs"))
+```
+
+```
+##> [1] "less than a minute ago"
+```
+
+```r
+time_ago(now - as.difftime(14, units = "mins"))
+```
+
+```
+##> [1] "14 minutes ago"
+```
+
+```r
+time_ago(now - as.difftime(5, units = "hours"))
+```
+
+```
+##> [1] "5 hours ago"
+```
+
+```r
+time_ago(now - as.difftime(25, units = "hours"))
+```
+
+```
+##> [1] "a day ago"
+```
+
+
diff --git a/man/pretty_bytes.Rd b/man/pretty_bytes.Rd
new file mode 100644
index 0000000..f4306c4
--- /dev/null
+++ b/man/pretty_bytes.Rd
@@ -0,0 +1,25 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/sizes.r
+\name{pretty_bytes}
+\alias{pretty_bytes}
+\title{Bytes in a human readable string}
+\usage{
+pretty_bytes(bytes)
+}
+\arguments{
+\item{bytes}{Numeric vector, number of bytes.}
+}
+\value{
+Character vector, the formatted sizes.
+}
+\description{
+Bytes in a human readable string
+}
+\examples{
+pretty_bytes(1337)
+pretty_bytes(133337)
+pretty_bytes(13333337)
+pretty_bytes(1333333337)
+pretty_bytes(133333333337)
+}
+
diff --git a/man/pretty_dt.Rd b/man/pretty_dt.Rd
new file mode 100644
index 0000000..70ea5d5
--- /dev/null
+++ b/man/pretty_dt.Rd
@@ -0,0 +1,30 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/time.r
+\name{pretty_dt}
+\alias{pretty_dt}
+\title{Pretty formatting of time intervals (difftime objects)}
+\usage{
+pretty_dt(dt, compact = FALSE)
+}
+\arguments{
+\item{dt}{A \code{difftime} object, a vector of time
+differences.}
+
+\item{compact}{If true, then only the first non-zero
+unit is used. See examples below.}
+}
+\value{
+Character vector of formatted time intervals.
+}
+\description{
+Pretty formatting of time intervals (difftime objects)
+}
+\examples{
+pretty_dt(as.difftime(1000, units = "secs"))
+pretty_dt(as.difftime(0, units = "secs"))
+}
+\seealso{
+Other time: \code{\link{pretty_ms}};
+  \code{\link{pretty_sec}}
+}
+
diff --git a/man/pretty_ms.Rd b/man/pretty_ms.Rd
new file mode 100644
index 0000000..7fadfa7
--- /dev/null
+++ b/man/pretty_ms.Rd
@@ -0,0 +1,31 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/time.r
+\name{pretty_ms}
+\alias{pretty_ms}
+\title{Pretty formatting of milliseconds}
+\usage{
+pretty_ms(ms, compact = FALSE)
+}
+\arguments{
+\item{ms}{Numeric vector of milliseconds}
+
+\item{compact}{If true, then only the first non-zero
+unit is used. See examples below.}
+}
+\value{
+Character vector of formatted time intervals.
+}
+\description{
+Pretty formatting of milliseconds
+}
+\examples{
+pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000))
+
+pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000),
+          compact = TRUE)
+}
+\seealso{
+Other time: \code{\link{pretty_dt}};
+  \code{\link{pretty_sec}}
+}
+
diff --git a/man/pretty_sec.Rd b/man/pretty_sec.Rd
new file mode 100644
index 0000000..001595a
--- /dev/null
+++ b/man/pretty_sec.Rd
@@ -0,0 +1,31 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/time.r
+\name{pretty_sec}
+\alias{pretty_sec}
+\title{Pretty formatting of seconds}
+\usage{
+pretty_sec(sec, compact = FALSE)
+}
+\arguments{
+\item{sec}{Numeric vector of seconds.}
+
+\item{compact}{If true, then only the first non-zero
+unit is used. See examples below.}
+}
+\value{
+Character vector of formatted time intervals.
+}
+\description{
+Pretty formatting of seconds
+}
+\examples{
+pretty_sec(c(1337, 13370, 133700, 1337000, 13370000))
+
+pretty_sec(c(1337, 13370, 133700, 1337000, 13370000),
+           compact = TRUE)
+}
+\seealso{
+Other time: \code{\link{pretty_dt}};
+  \code{\link{pretty_ms}}
+}
+
diff --git a/man/prettyunits.Rd b/man/prettyunits.Rd
new file mode 100644
index 0000000..60bd624
--- /dev/null
+++ b/man/prettyunits.Rd
@@ -0,0 +1,11 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/pretty-package.r
+\docType{package}
+\name{prettyunits}
+\alias{prettyunits}
+\alias{prettyunits-package}
+\title{Prettier formatting of quantities}
+\description{
+Prettier formatting of quantities
+}
+
diff --git a/man/time_ago.Rd b/man/time_ago.Rd
new file mode 100644
index 0000000..808d74f
--- /dev/null
+++ b/man/time_ago.Rd
@@ -0,0 +1,56 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/time-ago.r
+\name{time_ago}
+\alias{time_ago}
+\title{Human readable format of the time interval since a time point}
+\usage{
+time_ago(date, format = c("default", "short", "terse"))
+}
+\arguments{
+\item{date}{Date(s), \code{as.POSIXct} will be called on them.}
+
+\item{format}{Format, currently available formats are:
+\sQuote{default}, \sQuote{short}, \sQuote{terse}. See examples below.}
+}
+\value{
+Character vector of the formatted time intervals.
+}
+\description{
+It calls \code{\link{vague_dt}} to do the actual formatting.
+}
+\examples{
+now <- Sys.time()
+
+time_ago(now)
+time_ago(now - as.difftime(30, units = "secs"))
+time_ago(now - as.difftime(14, units = "mins"))
+time_ago(now - as.difftime(5, units = "hours"))
+time_ago(now - as.difftime(25, units = "hours"))
+time_ago(now - as.difftime(5, units = "days"))
+time_ago(now - as.difftime(30, units = "days"))
+time_ago(now - as.difftime(365, units = "days"))
+time_ago(now - as.difftime(365 * 10, units = "days"))
+
+## Short format
+time_ago(format = "short", now)
+time_ago(format = "short", now - as.difftime(30, units = "secs"))
+time_ago(format = "short", now - as.difftime(14, units = "mins"))
+time_ago(format = "short", now - as.difftime(5, units = "hours"))
+time_ago(format = "short", now - as.difftime(25, units = "hours"))
+time_ago(format = "short", now - as.difftime(5, units = "days"))
+time_ago(format = "short", now - as.difftime(30, units = "days"))
+time_ago(format = "short", now - as.difftime(365, units = "days"))
+time_ago(format = "short", now - as.difftime(365 * 10, units = "days"))
+
+## Even shorter, terse format, (almost always) exactly 3 characters wide
+time_ago(format = "terse", now)
+time_ago(format = "terse", now - as.difftime(30, units = "secs"))
+time_ago(format = "terse", now - as.difftime(14, units = "mins"))
+time_ago(format = "terse", now - as.difftime(5, units = "hours"))
+time_ago(format = "terse", now - as.difftime(25, units = "hours"))
+time_ago(format = "terse", now - as.difftime(5, units = "days"))
+time_ago(format = "terse", now - as.difftime(30, units = "days"))
+time_ago(format = "terse", now - as.difftime(365, units = "days"))
+time_ago(format = "terse", now - as.difftime(365 * 10, units = "days"))
+}
+
diff --git a/man/vague_dt.Rd b/man/vague_dt.Rd
new file mode 100644
index 0000000..793fb1a
--- /dev/null
+++ b/man/vague_dt.Rd
@@ -0,0 +1,51 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/time-ago.r
+\name{vague_dt}
+\alias{vague_dt}
+\title{Human readable format of a time interval}
+\usage{
+vague_dt(dt, format = c("default", "short", "terse"))
+}
+\arguments{
+\item{dt}{A \code{difftime} object, the time interval(s).}
+
+\item{format}{Format, currently available formats are:
+\sQuote{default}, \sQuote{short}, \sQuote{terse}. See examples below.}
+}
+\value{
+Character vector of the formatted time intervals.
+}
+\description{
+Human readable format of a time interval
+}
+\examples{
+vague_dt(as.difftime(30, units = "secs"))
+vague_dt(as.difftime(14, units = "mins"))
+vague_dt(as.difftime(5, units = "hours"))
+vague_dt(as.difftime(25, units = "hours"))
+vague_dt(as.difftime(5, units = "days"))
+vague_dt(as.difftime(30, units = "days"))
+vague_dt(as.difftime(365, units = "days"))
+vague_dt(as.difftime(365 * 10, units = "days"))
+
+## Short format
+vague_dt(format = "short", as.difftime(30, units = "secs"))
+vague_dt(format = "short", as.difftime(14, units = "mins"))
+vague_dt(format = "short", as.difftime(5, units = "hours"))
+vague_dt(format = "short", as.difftime(25, units = "hours"))
+vague_dt(format = "short", as.difftime(5, units = "days"))
+vague_dt(format = "short", as.difftime(30, units = "days"))
+vague_dt(format = "short", as.difftime(365, units = "days"))
+vague_dt(format = "short", as.difftime(365 * 10, units = "days"))
+
+## Even shorter, terse format, (almost always) exactly 3 characters wide
+vague_dt(format = "terse", as.difftime(30, units = "secs"))
+vague_dt(format = "terse", as.difftime(14, units = "mins"))
+vague_dt(format = "terse", as.difftime(5, units = "hours"))
+vague_dt(format = "terse", as.difftime(25, units = "hours"))
+vague_dt(format = "terse", as.difftime(5, units = "days"))
+vague_dt(format = "terse", as.difftime(30, units = "days"))
+vague_dt(format = "terse", as.difftime(365, units = "days"))
+vague_dt(format = "terse", as.difftime(365 * 10, units = "days"))
+}
+
diff --git a/tests/testthat.R b/tests/testthat.R
new file mode 100644
index 0000000..a6bbded
--- /dev/null
+++ b/tests/testthat.R
@@ -0,0 +1,4 @@
+library(testthat)
+library(prettyunits)
+
+test_check("prettyunits")
diff --git a/tests/testthat/test-bytes.r b/tests/testthat/test-bytes.r
new file mode 100644
index 0000000..af20e08
--- /dev/null
+++ b/tests/testthat/test-bytes.r
@@ -0,0 +1,51 @@
+
+context("Pretty bytes")
+
+test_that("pretty_bytes gives errors on invalid input", {
+
+  expect_error(pretty_bytes(''), 'is.numeric.*is not TRUE')
+  expect_error(pretty_bytes('1'), 'is.numeric.*is not TRUE')
+  expect_error(pretty_bytes(TRUE), 'is.numeric.*is not TRUE')
+  expect_error(pretty_bytes(list(1,2,3)), 'is.numeric.*is not TRUE')
+
+})
+
+test_that("pretty_bytes converts properly", {
+
+  expect_equal(pretty_bytes(0), '0 B')
+  expect_equal(pretty_bytes(10), '10 B')
+  expect_equal(pretty_bytes(999), '999 B')
+  expect_equal(pretty_bytes(1001), '1 kB')
+  expect_equal(pretty_bytes(1e16), '10 PB')
+  expect_equal(pretty_bytes(1e30), '1000000 YB')
+
+})
+
+test_that("pretty_bytes handles NA and NaN", {
+
+  expect_equal(pretty_bytes(NA_real_), "NA B")
+  expect_equal(pretty_bytes(NA_integer_), "NA B")
+  expect_error(pretty_bytes(NA_character_), 'is.numeric.*is not TRUE')
+  expect_error(pretty_bytes(NA), 'is.numeric.*is not TRUE')
+
+  expect_equal(pretty_bytes(NaN), "NaN B")
+
+})
+
+test_that("pretty_bytes handles vectors", {
+
+  expect_equal(pretty_bytes(1:10), paste(format(1:10), "B"))
+  v <- c(NA, 1, 1e4, 1e6, NaN, 1e5)
+
+  expect_equal(pretty_bytes(v),
+    c(" NA B", "  1 B", " 10 kB", "  1 MB", "NaN B", "100 kB"))
+
+  expect_equal(pretty_bytes(numeric()), character())
+})
+
+test_that("pretty_bytes handles negative values", {
+  v <- c(NA, -1, 1e4, 1e6, NaN, -1e5)
+  expect_equal(pretty_bytes(v),
+    c("  NA B", "  -1 B", "  10 kB", "   1 MB", " NaN B", "-100 kB"))
+
+})
diff --git a/tests/testthat/test-ms.r b/tests/testthat/test-ms.r
new file mode 100644
index 0000000..f8673b4
--- /dev/null
+++ b/tests/testthat/test-ms.r
@@ -0,0 +1,111 @@
+
+context("Pretty milliseconds")
+
+test_that("pretty_ms works", {
+
+  expect_equal(pretty_ms(0), '0ms')
+  expect_equal(pretty_ms(0.1), '1ms')
+  expect_equal(pretty_ms(1), '1ms')
+  expect_equal(pretty_ms(1000 + 400), '1.4s')
+  expect_equal(pretty_ms(1000 * 2 + 400), '2.4s')
+  expect_equal(pretty_ms(1000 * 55), '55s')
+  expect_equal(pretty_ms(1000 * 67), '1m 7s')
+  expect_equal(pretty_ms(1000 * 60 * 5), '5m')
+  expect_equal(pretty_ms(1000 * 60 * 67), '1h 7m')
+  expect_equal(pretty_ms(1000 * 60 * 60 * 12), '12h')
+  expect_equal(pretty_ms(1000 * 60 * 60 * 40), '1d 16h')
+  expect_equal(pretty_ms(1000 * 60 * 60 * 999), '41d 15h')
+
+})
+
+test_that("compact pretty_ms works", {
+
+  expect_equal(pretty_ms(1000 + 4, compact = TRUE), '~1s')
+  expect_equal(pretty_ms(1000 * 60 * 60 * 999, compact =  TRUE), '~41d')
+
+})
+
+test_that("pretty_ms handles vectors", {
+
+  v <- c(0, 0.1, 1, 1400, 2400, 1000 * 55, 1000 * 67,
+         1000 * 60 * 5, 1000 * 60 * 67, 1000 * 60 * 60 * 12,
+         1000 * 60 * 60 * 40, 1000 * 60 * 60 * 999)
+  v2 <- c("0ms", "1ms", "1ms", "1.4s", "2.4s", "55s", "1m 7s",
+          "5m", "1h 7m", "12h", "1d 16h", "41d 15h")
+
+  expect_equal(pretty_ms(v), v2)
+})
+
+context("Pretty seconds")
+
+test_that("pretty_sec works", {
+
+  expect_equal(pretty_sec(0 / 1000), '0ms')
+  expect_equal(pretty_sec(0.1 / 1000), '1ms')
+  expect_equal(pretty_sec(1 / 1000), '1ms')
+  expect_equal(pretty_sec((1000 + 400) / 1000), '1.4s')
+  expect_equal(pretty_sec((1000 * 2 + 400) / 1000), '2.4s')
+  expect_equal(pretty_sec(1000 * 55 / 1000), '55s')
+  expect_equal(pretty_sec(1000 * 67 / 1000), '1m 7s')
+  expect_equal(pretty_sec(1000 * 60 * 5 / 1000), '5m')
+  expect_equal(pretty_sec(1000 * 60 * 67 / 1000), '1h 7m')
+  expect_equal(pretty_sec(1000 * 60 * 60 * 12 / 1000), '12h')
+  expect_equal(pretty_sec(1000 * 60 * 60 * 40 / 1000), '1d 16h')
+  expect_equal(pretty_sec(1000 * 60 * 60 * 999 / 1000), '41d 15h')
+
+})
+
+test_that("compact pretty_sec works", {
+
+  expect_equal(pretty_sec((1000 + 4) / 1000, compact = TRUE), '~1s')
+  expect_equal(pretty_sec(1000 * 60 * 60 * 999 / 1000, compact =  TRUE), '~41d')
+
+})
+
+test_that("pretty_sec handles vectors", {
+
+  v <- c(0, 0.1, 1, 1400, 2400, 1000 * 55, 1000 * 67,
+         1000 * 60 * 5, 1000 * 60 * 67, 1000 * 60 * 60 * 12,
+         1000 * 60 * 60 * 40, 1000 * 60 * 60 * 999) / 1000
+  v2 <- c("0ms", "1ms", "1ms", "1.4s", "2.4s", "55s", "1m 7s",
+          "5m", "1h 7m", "12h", "1d 16h", "41d 15h")
+
+  expect_equal(pretty_sec(v), v2)
+})
+
+context("Pretty dt")
+
+test_that("pretty_dt works", {
+
+  expect_equal(pretty_dt(as.difftime(units = "secs", 0 / 1000)), '0ms')
+  expect_equal(pretty_dt(as.difftime(units = "secs", 0.1 / 1000)), '1ms')
+  expect_equal(pretty_dt(as.difftime(units = "secs", 1 / 1000)), '1ms')
+  expect_equal(pretty_dt(as.difftime(units = "secs", (1000 + 400) / 1000)), '1.4s')
+  expect_equal(pretty_dt(as.difftime(units = "secs", (1000 * 2 + 400) / 1000)), '2.4s')
+  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 55 / 1000)), '55s')
+  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 67 / 1000)), '1m 7s')
+  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 5 / 1000)), '5m')
+  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 67 / 1000)), '1h 7m')
+  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 60 * 12 / 1000)), '12h')
+  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 60 * 40 / 1000)), '1d 16h')
+  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 60 * 999 / 1000)), '41d 15h')
+
+})
+
+test_that("compact pretty_dt works", {
+
+  expect_equal(pretty_dt(as.difftime(units = "secs", (1000 + 4) / 1000), compact = TRUE), '~1s')
+  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 60 * 999 / 1000), compact =  TRUE), '~41d')
+
+})
+
+test_that("pretty_dt handles vectors", {
+
+  v <- c(0, 0.1, 1, 1400, 2400, 1000 * 55, 1000 * 67,
+         1000 * 60 * 5, 1000 * 60 * 67, 1000 * 60 * 60 * 12,
+         1000 * 60 * 60 * 40, 1000 * 60 * 60 * 999) / 1000
+  v2 <- c("0ms", "1ms", "1ms", "1.4s", "2.4s", "55s", "1m 7s",
+          "5m", "1h 7m", "12h", "1d 16h", "41d 15h")
+
+  expect_equal(pretty_dt(as.difftime(units = "secs", v)), v2)
+})
diff --git a/tests/testthat/test-vague-dt.r b/tests/testthat/test-vague-dt.r
new file mode 100644
index 0000000..df3d9d1
--- /dev/null
+++ b/tests/testthat/test-vague-dt.r
@@ -0,0 +1,46 @@
+
+context("Vague time intervals")
+
+## Amount, unit, default result, short, terse
+all_tests <- list(
+  list(  1,      "secs",  "moments ago",            "<1 min",   " 1s"),
+  list( 30,      "secs",  "less than a minute ago", "<1 min",   "30s"),
+  list( 50,      "secs",  "about a minute ago",     "1 min",    " 1m"),
+  list( 14,      "mins",  "14 minutes ago",         "14 min",   "14m"),
+  list( 70,      "mins",  "about an hour ago",      "1 hour",   " 1h"),
+  list(  5,      "hours", "5 hours ago",            "5 hours",  " 5h"),
+  list( 25,      "hours", "a day ago",              "1 day",    " 1d"),
+  list(  5,      "days",  "5 days ago",             "5 day",    " 5d"),
+  list( 30,      "days",  "about a month ago",      "1 mon",    " 1M"),
+  list( 30 * 3,  "days",  "3 months ago",           "3 mon",    " 3M"),
+  list(365,      "days",  "about a year ago",       "1 year",   " 1y"),
+  list(365 * 10, "days",  "10 years ago",           "10 years", "10y")
+)
+
+test_that("vague_dt works", {
+
+  sapply(all_tests, function(case) {
+    dt <- as.difftime(case[[1]], units = case[[2]])
+    default <- vague_dt(dt)
+    short   <- vague_dt(dt, format = "short")
+    terse   <- vague_dt(dt, format = "terse")
+    expect_equal(default, case[[3]], info = paste(case[[1]], case[[2]], "default"))
+    expect_equal(short,   case[[4]], info = paste(case[[1]], case[[2]], "short"))
+    expect_equal(terse,   case[[5]], info = paste(case[[1]], case[[2]], "terse"))
+  })
+  
+})
+
+test_that("time_ago works", {
+
+  sapply(all_tests, function(case) {
+    t <- Sys.time() - as.difftime(case[[1]], units = case[[2]])
+    default <- time_ago(t)
+    short   <- time_ago(t, format = "short")
+    terse   <- time_ago(t, format = "terse")
+    expect_equal(default, case[[3]], info = paste(case[[1]], case[[2]], "default"))
+    expect_equal(short,   case[[4]], info = paste(case[[1]], case[[2]], "short"))
+    expect_equal(terse,   case[[5]], info = paste(case[[1]], case[[2]], "terse"))
+  })
+  
+})

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/r-cran-prettyunits.git



More information about the debian-med-commit mailing list