Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Getting the number of days in the month #118

Closed
richierocks opened this Issue · 2 comments

3 participants

@richierocks

Occasionally it is useful to know how many days there are in each month. For example, if you have monthly readings of something (sales totals or electricity consumption or whatever), you might expect there to be less in February since the month is shorter. In this case it can be useful to normalise the monthly readings by dividing by the number of days in that month.

Here's a suggested implementation

#' Get the number of days in the month of a date-time.
#' 
#' Date-time must be a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, 
#' zoo, zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects.
#'
#' @param x a date-time object
#' @return An integer of the number of days in the month component of the date-time object.
days_in_month <- function(x)
{
  month_x <- month(x, label = TRUE)
  N_DAYS <- c(
    Jan = 31L, Feb = 28L, Mar = 31L, 
    Apr = 30L, May = 31L, Jun = 30L, 
    Jul = 31L, Aug = 31L, Sep = 30L, 
    Oct = 31L, Nov = 30L, Dec = 31L
  )
  n_days <- N_DAYS[month_x]
  n_days[month_x == "Feb" & leap_year(x)] <- 29L
  n_days
}

And some testthat-style tests.

context("test days_in_month")

test_that(
  "days in month works for non leap years",
  {
    x <- seq(ymd("2011-01-01"), ymd("2011-12-01"), "1 month")
    expected <- c(
      Jan = 31L, Feb = 28L, Mar = 31L, 
      Apr = 30L, May = 31L, Jun = 30L, 
      Jul = 31L, Aug = 31L, Sep = 30L, 
      Oct = 31L, Nov = 30L, Dec = 31L
    )
    expect_that(days_in_month(x), equals(expected))
  }
)

test_that(
  "days in month works for leap years",
  {
    x <- seq(ymd("2012-01-01"), ymd("2012-12-01"), "1 month")
    expected <- c(
      Jan = 31L, Feb = 29L, Mar = 31L, 
      Apr = 30L, May = 31L, Jun = 30L, 
      Jul = 31L, Aug = 31L, Sep = 30L, 
      Oct = 31L, Nov = 30L, Dec = 31L
    )
    expect_that(days_in_month(x), equals(expected))
  }
)
@garrettgman
Collaborator
@eshilts

This would be great to have. The StackOverflow thread for how to do this provides a bunch of messy options:
http://stackoverflow.com/questions/6243088/find-out-the-number-of-days-of-a-month-in-r

@kenahoo kenahoo referenced this issue from a commit in kenahoo/lubridate
@garrettgman garrettgman Added richierocks' days_in_month function. Great idea and thanks for …
…the tests! Fixes #118.
0728b22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.