Is as.vector() for POSIXct objects buggy?

Viewed 69

The funny part

Let's start with the set-up (R console):

> z <- Sys.time()
> z
[1] "2022-03-15 00:21:42 CET"
> class(z)
[1] "POSIXct" "POSIXt"

Now, the plot (as in joke, not graphics):

> as.numeric(z)
[1] 1647300102
> as.character(z)
[1] "2022-03-15 00:21:42"

Now, the punch line (also as in joke, not graphics):

> as.vector(z, "numeric")
[1] 1647300102
> as.vector(z, "character")
[1] "1647300102.23863"

Intuitively, as.vector(z, "character") should return the same as as.character(z), but it doesn't. Funny, isn't it?

The serious part

I think I understand why as.vector() with mode="character" gives the number-of-seconds-since-epoch instead of the human-readable date-time returned by as.character(): the POSIXct class has an implementation for the as.character() generic, but the guys in charge didn't bother to provide a custom as.vector() implementation for this class; hence, as.vector() defaults to the base class of POSIXct (which is numeric) to print the date-time as a character vector.

The question is: should be this reported as a bug?

I apologize for asking this question here before going head-on to the R core development team; here's why:

The question part

Should this be a bug? I would rather ask the community at large first, before asking the R core guys, because POSIXct is -- indeed -- one of the base R types, but I don't know if this bothers any actual R users (excepting me). My problem with the behaviour is very specific (i.e. using a generic as.vector(x, mode) for conversion instead of specific as.character(x), as.numeric(x), as.MaryPoppins(x) etc.)

The reason-for-question part

The reasons for my asking the question are here:

  • The CRAN Bug reporting in R essentially says "if it seems unexpected to you, probably is in your head, unless something crashes". That is not my idea of "bug". But maybe I misunderstood.

  • I wonder if other R users would find the behaviour described above surprising. If yes, then maybe this can be reported as an "enhancement" instead as a bug, but with similar outcome (i.e. fixing the behaviour of as.vector to be... sane? about the POSIXct).

  • I wonder also how much legacy code relies on the existing behaviour of as.vector() when the main argument is of class POSIXct. If there are a lot of users counting on the current behaviour, then maybe I should write my own as.vector.POSIXct() methods.

I appreciate all the comments, including the disparaging ones.

Update: The meta-question

Just in case you've thought of a workaround using conversion to/from character via format()... is not exactly the point. The point is: should we expect a consistent interface from the classes in the base namespace or not? Ok, now I sound like a revolutionary confined in a Pokemon ball...

0 Answers
Related