Commercial enterprise offerings Open source packages - Quarto, Shiny, and more

lubridate 1.6.0

Hadley Wickham Headshot
Written by Hadley Wickham
2016-09-15

Please note that the information presented in this post reflects the package as it stood when initially released, and may now be outdated. For the most up-to-date information, kindly refer to https://lubridate.tidyverse.org/.

I am pleased to announced lubridate 1.6.0. Lubridate is designed to make working with dates and times as pleasant as possible, and is maintained by Vitalie Spinu. You can install the latest version with:

install.packages("lubridate")

This release includes a range of bug fixes and minor improvements. Some highlights from this release include:

  • period() and duration() constructors now accept character strings and allow a very flexible specification of timespans:
period("3H 2M 1S")
#> [1] "3H 2M 1S"

duration("3 hours, 2 mins, 1 secs")
#> [1] "10921s (~3.03 hours)"

# Missing numerals default to 1.
# Repeated units are summed
period("hour minute minute")
#> [1] "1H 2M 0S"

Period and duration parsing allows for arbitrary abbreviations of time units as long as the specification is unambiguous. For single letter specs, strptime() rules are followed, so m stands for months and M for minutes.

These same rules allows you to compare strings and durations/periods:

"2mins 1 sec" > period("2mins")
#> [1] TRUE
  • Date time rounding (with round_date(), floor_date() and ceiling_date()) now supports unit multipliers, like “3 days” or “2 months”:
ceiling_date(ymd_hms("2016-09-12 17:10:00"), unit = "5 minutes")
#> [1] "2016-09-12 17:10:00 UTC"
  • The behavior of ceiling_date for Date objects is now more intuitive. In short, dates are now interpreted as time intervals that are physically part of longer unit intervals:

    |day1| … |day31|day1| … |day28| … | January | February | …

That means that rounding up 2000-01-01 by a month is done to the boundary between January and February which, i.e. 2000-02-01:

ceiling_date(ymd("2000-01-01"), unit = "month")
#> [1] "2000-02-01"

This behavior is controlled by the change_on_boundary argument.

  • It is now possible to compare POSIXct and Date objects:
ymd_hms("2000-01-01 00:00:01") > ymd("2000-01-01")
#> [1] TRUE
  • C-level parsing now handles English months and AM/PM indicator regardless of your locale. This means that English date-times are now always handled by lubridate C-level parsing and you don’t need to explicitly switch the locale.

  • New parsing function yq() allows you to parse a year + quarter:

yq("2016-02")
#> [1] "2016-04-01"

The new q format is available in all lubridate parsing functions.

See the release notes for the full list of changes. A big thanks goes to everyone who contributed: @arneschillert, @cderv, @ijlyttle, @jasonelaw, @jonboiser, and @krlmlr.

Hadley Wickham Headshot

Hadley Wickham

Chief Scientist, Posit
Hadley is Chief Scientist at Posit PBC, winner of the 2019 COPSS award, and a member of the R Foundation. He builds tools (both computational and cognitive) to make data science easier, faster, and more fun. His work includes packages for data science (like the tidyverse, which includes ggplot2, dplyr, and tidyr)and principled software development (e.g. roxygen2, testthat, and pkgdown). He is also a writer, educator, and speaker promoting the use of R for data science.