Skip to content

Commit

Permalink
Add days_ago and days_hence functions
Browse files Browse the repository at this point in the history
Add travis yaml
Add codecov yaml
Rebuild pkgdown site
Rebuild documentation files
  • Loading branch information
ellisvalentiner committed Sep 21, 2017
1 parent 373dfe9 commit 5afb8b7
Show file tree
Hide file tree
Showing 34 changed files with 2,371 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
README.Rmd
^docs$
^_pkgdown\.yml$
^codecov\.yml$
^\.travis\.yml$
11 changes: 2 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
# History files
.Rhistory
.Rapp.history

# Session Data files
.RData

# Example code in package build process
*-Ex.R

# Output files from R CMD build
/*.tar.gz

# Output files from R CMD check
/*.Rcheck/

# RStudio files
.Rproj.user/

# produced vignettes
vignettes/*.html
vignettes/*.pdf

# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3
.httr-oauth

# knitr and R markdown default cache directories
/*_cache/
/cache/

# Temporary files created by R markdown
*.utf8.md
*.knit.md
.Rproj.user
# macOS
.DS_Store
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r

language: r
sudo: false
cache: packages
latex: false

env:
global:
- _R_CHECK_FORCE_SUGGESTS_=false
- MAKEFLAGS="-j 2"

include:
- r: release
- r: oldrel
- r: devel

before_script:
- mkdir -p ~/.R; echo 'PKG_CXXFLAGS := ${PKG_CXXFLAGS} -Wall -Wextra -pedantic -Werror' > ~/.R/Makevars

after_success:
- Rscript -e 'covr::codecov()'
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Generated by roxygen2: do not edit by hand

export(days_ago)
export(days_hence)
export(is.weekday)
export(is.weekend)
export(last_month)
export(next_month)
export(this_month)
export(tomorrow)
export(yesterday)
import(lubridate)
80 changes: 79 additions & 1 deletion R/instants.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,71 @@ tomorrow <- function(tzone = "") {
as_date(now(tzone) + days(1))
}

#' The date x days ago
#'
#' @export days_ago
#' @param days an integer specifying the number of days to subtract from the current
#' @param tzone a character vector specifying which time zone you would like to
#' find the previous date of. tzone defaults to the system time zone set on your
#' computer.
#' @return the date, x days ago
#'
#' @examples
#' days_ago(3)
#' days_ago(1) == yesterday()
days_ago <- function(days = 0, tzone = "") {
as_date(today(tzone = tzone) - days(x = days))
}

#' The date x days hence
#'
#' @export days_hence
#' @param days an integer specifying the number of days to add from the current
#' @param tzone a character vector specifying which time zone you would like to
#' find the previous date of. tzone defaults to the system time zone set on your
#' computer.
#' @return the date, x days hence
#'
#' @examples
#' days_hence(3)
#' days_hence(1) == tomorrow()
days_hence <- function(days = 0, tzone = "") {
as_date(today(tzone = tzone) + days(x = days))
}

#' The current month
#'
#' @export this_month
#' @param tzone a character vector specifying which time zone you would like to
#' find the current month of. tzone defaults to the system time zone set on your
#' computer.
#' @return the current month as a Date object
this_month <- function(tzone = "") {
floor_date(x = today(tzone = tzone), unit = "month")
}

#' The previous month
#'
#' @export last_month
#' @param tzone a character vector specifying which time zone you would like to
#' find the previous month of. tzone defaults to the system time zone set on your
#' computer.
#' @return the previous month as a Date object
last_month <- function(tzone = "") {
floor_date(x = today(tzone = tzone), unit = "month") - months(1)
}

#' The next month
#'
#' @export next_month
#' @param tzone a character vector specifying which time zone you would like to
#' find the next month of. tzone defaults to the system time zone set on your
#' computer.
#' @return the next month as a Date object
next_month <- function(tzone = "") {
floor_date(x = today(tzone = tzone), unit = "month") + months(1)
}

#' Is x a weekend?
#'
#' @export is.weekend
Expand All @@ -39,6 +104,19 @@ tomorrow <- function(tzone = "") {
#' is.weekend("2017-08-29") # FALSE
#' is.weekend("2017-09-02") # TRUE
is.weekend <- function(x) {
wday(x = as_date(x), label = TRUE, abbr = FALSE) %in% c("Saturday", "Sunday")
wday(x = as_date(x), label = FALSE, abbr = FALSE) %in% 6:7
}

#' Is x a weekday?
#'
#' @export is.weekday
#' @param x a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, zooreg,
#' timeDate, xts, its, ti, jul, timeSeries, or fts object.
#' @return boolean indicating whether x is a weekday
#'
#' @examples
#' is.weekend("2017-08-29") # FALSE
#' is.weekend("2017-09-02") # TRUE
is.weekday <- function(x) {
wday(x = as_date(x), label = FALSE, abbr = FALSE) %in% 1:5
}
14 changes: 13 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Convenience functions for the lubridate package

## Overview

Lubridate makes it easier to work with date-time data in R and provides new capabilities. LubridateExtras builds on top of lubridate to provide
Lubridate makes it easier to work with date-time data in R and provides new capabilities. LubridateExtras builds on top of lubridate to provide a number of convenience functions, primarily focused on abstracting patterns in ways that improve code readability and reduce copying and pasting code.

## Installation

Expand All @@ -35,3 +35,15 @@ devtools::install_github("ellisvalentiner/lubridateExtras")
```

If you encounter a clear bug, please file a minimal reproducible example on [github](https://github.com/ellisvalentiner/lubridateExtras/issues).

## Usage

```{r}
library(lubridateExtras)
days_ago(7) # equivalent to lubridate::today() - lubridate::days(7)
days_hence(7) # equivalent to lubridate::today() + lubridate::days(7)
```


15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Convenience functions for the lubridate package
Overview
--------

Lubridate makes it easier to work with date-time data in R and provides new capabilities. LubridateExtras builds on top of lubridate to provide
Lubridate makes it easier to work with date-time data in R and provides new capabilities. LubridateExtras builds on top of lubridate to provide a number of convenience functions, primarily focused on abstracting patterns in ways that improve code readability and reduce copying and pasting code.

Installation
------------
Expand All @@ -22,3 +22,16 @@ devtools::install_github("ellisvalentiner/lubridateExtras")
```

If you encounter a clear bug, please file a minimal reproducible example on [github](https://github.com/ellisvalentiner/lubridateExtras/issues).

Usage
-----

``` r
library(lubridateExtras)

days_ago(7) # equivalent to lubridate::today() - lubridate::days(7)
#> [1] "2017-09-14"

days_hence(7) # equivalent to lubridate::today() + lubridate::days(7)
#> [1] "2017-09-28"
```
16 changes: 16 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,19 @@ url: https://ellisvalentiner.github.com/lubridateExtras
template:
params:
bootswatch: lumen

home:
strip_header: true

navbar:
type: default
left:
- text: Intro
href: lubridateExtras.html
- text: Reference
href: reference/index.html
- text: News
href: news/index.html
right:
- icon: fa-github fa-lg
href: https://github.com/ellisvalentiner/lubridateExtras
12 changes: 12 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
comment: false

coverage:
status:
patch:
default:
target: 0
threshold: 100
project:
default:
target: 70
threshold: 100
127 changes: 127 additions & 0 deletions docs/LICENSE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5afb8b7

Please sign in to comment.