Skip to content

Set year component of an IDate object #1061

@Henrik-P

Description

@Henrik-P

data.table has the IDate class:

IDate is a date class derived from Date. It has the same internal representation as the Date class, except the storage mode is integer.

From ?lubridate::year:

Date-time must be a [...] Date [...] or any other object convertible to POSIXlt.

Create IDate object, check class, and coerce to POSIXlt:

d = data.table::as.IDate("2022-08-28")

class(d)
# [1] "IDate" "Date" 

class(as.POSIXlt(d))
# [1] "POSIXlt" "POSIXt"

Thus, an IDate object, having also class Date and being coercible to POSIXlt, seems to fulfil the requirements of the input object of lubridate::year. Indeed, it is possible to use lubridate::year to get the year component of an IDate object. However setting the year fails. Wouldn't we expect year<- to work as well?

# get year works
lubridate::year(d)
# [1] 2022 

# set year errors
lubridate::year(d) <- 2021
# Error: Incompatible classes: <IDate> + <Period>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions