This is expected behavior. What is printed is not what the object is. To be printed, the object needs to be converted to character. as.character.Date
calls format.Date
, which calls format.POSIXlt
. The Value section of ?format.POSIXlt
(or ?strptime
) says:
The format
methods and strftime
return character vectors
representing the time. NA
times are returned as NA_character_
.
So that's why NA
is printed, because printing structure(NA_real_, class = "Date")
returns NA_character_
. For example:
R> is.na(format(structure(Inf, class = "Date")))
[1] TRUE
R> is.na(format(structure(NaN, class = "Date")))
[1] TRUE
If you somehow encounter these wonky dates in your code, I recommend you test for them using is.finite
instead of is.na
.
R> is.finite(structure(Inf, class = "Date"))
[1] FALSE
R> is.finite(structure(NaN, class = "Date"))
[1] FALSE
print.Date
display the "end of times". – Conventionerunclass(structure(Inf, class = "Date"))
is notNA
Inf is not reallyNA
, but it is as far as the Date class goes...or something like that – Arandis.na.Date
. Not sure if that matters. Very good question! – ArandInf
? – ChemistryInf
basically saying that time is infinite, whileNA
is basically saying "I don't know and I don't have a way to find out". – Shotunclass
it:as.numeric(structure(Inf, class = "Date"))
returnsInf
. – StunkNA
s.Sys.Date() < structure(Inf, class = "Date")
returnsTRUE
very appropriately. – Chemistryprint.date
->format.date
->as.POSIXlt
– Hynda