Skip to content

Add a ym() function for year-month "dates" in spirit of yq()? #630

@gavinsimpson

Description

@gavinsimpson

As lubridate already has a handy yq() function for parsing year-quarter "dates", would you consider adding ym(), the equivalent for numeric year-month "dates"?

Date such as this crop up all the time in applied sciences, where the exact date of sampling is not recorded or neccessary or the data have been aggregated to a monthly time step, eg monthly climate anomolies, and provided in that format (i.e. without any day component).

parse_date_time knows how to handle such inputs

> parse_date_time("10/2001", "mY")
[1] "2001-10-01 UTC"

I'm thinking of something along the lines of (I'm new to lubridate so I'm not sure I follow the meaning of the orders argument just yet):

ym <- function (..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME")) {
  lubridate:::.parse_xxx(..., orders = "ym", quiet = quiet, tz = tz, locale = locale, 
                    truncated = 0)
}

my <- function (..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME")) {
  lubridate:::.parse_xxx(..., orders = "my", quiet = quiet, tz = tz, locale = locale, 
                    truncated = 0)
}

with expected output

> my("10-2001")
[1] "2001-10-01"
> ym("2010-10")
[1] "2010-10-01"

Optionally, accepting an argument fraction (or equivalent) would allow user to control the day part that is added: would return date + days(fraction * days_in_month)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions