README.md

gregorian

CRAN
status Build
Status Coverage
status

Provides a robust date object. It also provides date calculation functions that work on top of the new object. Many of the functions seamlessly convert Date and character objects into the new gregorian_date object.

Problem

as.Date("-99-7-12")
Error in charToDate(x) : 
  character string is not in a standard unambiguous format

Installation

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("edgararuiz/gregorian")

Gregorian object

library(gregorian)
as_gregorian("-99-7-12")
#> Friday July 12, 100 BCE
born <- as_gregorian("-99-7-12")
str(born)
#> List of 7
#>  $ year        : num 100
#>  $ month       : int 7
#>  $ day         : int 12
#>  $ bce         : logi TRUE
#>  $ day_name    :List of 2
#>   ..$ name  : chr "Friday"
#>   ..$ number: num 5
#>  $ astronomical: chr "-99-07-12"
#>  $ full_date   : chr "Friday July 12, 100 BCE"
#>  - attr(*, "class")= chr "gregorian_date"
get_date()
#> Monday May 13, 2019 CE

Date calculations

diff_days(born, get_date())
#> [1] 773524
diff_dates(born, get_date())
#> $years
#> [1] 2117
#> 
#> $months
#> [1] 10
#> 
#> $days
#> [1] 1
diff_calendar(born, get_date())
#> $years
#> [1] 2117
#> 
#> $months
#> [1] 9
#> 
#> $days
#> [1] 32
add_days(get_date(), 365)
#> Tuesday May 12, 2020 CE

Leap Year

is_leap_year(2012)
#> [1] TRUE
is_leap_year(2019)
#> [1] FALSE

Day Rotation

# Sunday plus one day (1 == Monday)
day_rotation(1, 7, 6) + 1
#> [1] 1
# Sunday plus 100 days (2 == Tuesday)
day_rotation(100, 7, 6) + 1
#> [1] 2

In the tidyverse

library(dplyr)

dt <- tribble(~dates,
        as.Date("12/12/2014"),
        as.Date("1/1/1") - 1000,
        as.Date("1/1/1") - 365
        )

dt
#> # A tibble: 3 x 1
#>   dates     
#>   <date>    
#> 1 12-12-20  
#> 2 -2-04-07  
#> 3 0-01-02
library(purrr)

dt %>%
  mutate(new_date = map(dates, as_gregorian))
#> # A tibble: 3 x 2
#>   dates      new_date            
#>   <date>     <list>              
#> 1 12-12-20   <S3: gregorian_date>
#> 2 -2-04-07   <S3: gregorian_date>
#> 3 0-01-02    <S3: gregorian_date>
dt %>%
  mutate(new_date = map_chr(dates, ~as_gregorian(.x)$full_date))
#> # A tibble: 3 x 2
#>   dates      new_date                   
#>   <date>     <chr>                      
#> 1 12-12-20   Thursday December 20, 12 CE
#> 2 -2-04-07   Tuesday April 7, 3 BCE     
#> 3 0-01-02    Sunday January 2, 1 BCE
dt %>%
  mutate(
    today_diff = map2_dbl(dates, Sys.Date(), diff_days)
    ) 
#> # A tibble: 3 x 2
#>   dates      today_diff
#>   <date>          <dbl>
#> 1 12-12-20       732820
#> 2 -2-04-07       738191
#> 3 0-01-02        737556


edgararuiz/gregorian documentation built on June 4, 2019, 11:48 a.m.