is_leap_year: Is some year a so-called leap year?

View source: R/time_util_fun.R

is_leap_yearR Documentation

Is some year a so-called leap year?

Description

is_leap_year checks whether a given year (provided as a date or time dt, or number/string denoting a 4-digit year) lies in a so-called leap year (i.e., a year containing a date of Feb-29).

Usage

is_leap_year(dt)

Arguments

dt

Date or time (scalar or vector). Numbers or strings with dates are parsed into 4-digit numbers denoting the year.

Details

When dt is not recognized as "Date" or "POSIXt" object(s), is_leap_year aims to parse a string dt as describing year(s) in a "dddd" (4-digit year) format, as a valid "Date" string (to retrieve the 4-digit year "%Y"), or a numeric dt as 4-digit integer(s).

is_leap_year then solves the task by verifying the numeric definition of a "leap year" (see https://en.wikipedia.org/wiki/Leap_year).

An alternative solution that tried using as.Date() for defining a "Date" of Feb-29 in the corresponding year(s) was removed, as it evaluated NA values as FALSE.

Value

Boolean vector.

Source

See https://en.wikipedia.org/wiki/Leap_year for definition.

See Also

days_in_month for the number of days in given months; diff_tz for time zone-based time differences; leap_year function of the lubridate package.

Other date and time functions: change_time(), change_tz(), cur_date(), cur_time(), days_in_month(), diff_dates(), diff_times(), diff_tz(), what_date(), what_month(), what_time(), what_wday(), what_week(), what_year(), zodiac()

Examples

is_leap_year(2020)
(days_this_year <- 365 + is_leap_year(Sys.Date()))

# from dates:
is_leap_year(Sys.Date())
is_leap_year(as.Date("2022-02-28"))

# from times:
is_leap_year(Sys.time())
is_leap_year(as.POSIXct("2022-10-11 10:11:12"))
is_leap_year(as.POSIXlt("2022-10-11 10:11:12"))

# from non-integers:
is_leap_year(2019.5)

# For vectors:
is_leap_year(2020:2028)

# with dt as strings:
is_leap_year(c("2020", "2021"))
is_leap_year(c("2020-02-29 01:02:03", "2021-02-28 01:02"))

# Note: Invalid date string yields error: 
# is_leap_year("2021-02-29")


ds4psy documentation built on Sept. 15, 2023, 9:08 a.m.