POSIXct to Date conversion could use timezone #49

Closed
ampu3ro opened this Issue Feb 7, 2017 · 13 comments

Projects

None yet

2 participants

@ampu3ro
ampu3ro commented Feb 7, 2017

anydate() seems to be missing the timezone argument to as.Date() internally ...

library(anytime)

tzone <- "America/New_York"
x <- as.POSIXct("2017-01-01 19:00",tzone)
anydate(x,tzone)
# "2017-01-02"

utcdate() should also be changed

Thanks for the awesome package!

@eddelbuettel
Owner

Dates do not have timezones.

R> str(Sys.Date())
 Date[1:1], format: "2017-02-07"
R> unlist(Sys.Date())
[1] "2017-02-07"
R> 

I intend to close this. If you do need a timezone with what really is a date, you can turn it back into POSIXct.

@ampu3ro
ampu3ro commented Feb 7, 2017

they don't carry the timezone attribute but converting to Date can require one

tzone <- "America/New_York"
x <- as.POSIXct("2017-01-01 19:00",tzone)
as.Date(x)
# "2017-01-02"
as.Date(x,tzone)
# "2017-01-01"

see ?as.Date

@eddelbuettel
Owner

We convert to Date at the C++ level now.

But yes, I see two stragglers on lines 161 and 184 -- and I see your point. I never used to use tz=... argument for as.Date().

@ampu3ro
ampu3ro commented Feb 7, 2017

yep, those are the lines. looks to me like POSIXct isn't converted to date in C++ like character is though

tzone <- "America/New_York"
x <- as.POSIXct("2017-01-01 19:00",tzone)
y <- anytime:::anytime_cpp(x,tzone,asDate=T)
str(y)
# POSIXct[1:1], format: "2017-01-01 19:00:00"
z <- anytime:::anytime_cpp(as.character(x),tzone,asDate=T)
str(z)
# Date[1:1], format: "2017-01-01"
@eddelbuettel
Owner

I don't follow. Look at class(), not the formatted output:

R> class(anytime("2017-02-07"))
[1] "POSIXct" "POSIXt" 
R> class(anydate("2017-02-07"))
[1] "Date"
R> 

Exactly what are you expecting if not Date for date variable?

@ampu3ro
ampu3ro commented Feb 7, 2017

Maybe I'm misunderstanding the function, but I guess I'm expecting

tzone <- "America/New_York"
anydate(as.POSIXct("2017-01-01 19:00",tzone),tzone)
# "2017-01-01" doesn't actually happen

just like I would expect

anydate("2017-01-01 19:00",tzone)
# "2017-01-01" does happen

If you agree, you could either change anytime_cpp() to return a date on POSIXct or just modify line 161 to as.Date(d,tz)

@eddelbuettel
Owner

This works, the rest appears to be your made up problem:

R> anytime("2017-01-01 19:00")
[1] "2017-01-01 19:00:00 CST"
R> anydate("2017-01-01 19:00")
[1] "2017-01-01"
R> 
@ampu3ro
ampu3ro commented Feb 7, 2017
> anydate(anytime("2017-01-01 19:00"))
[1] "2017-01-02"

does this seem right to you?

@eddelbuettel
Owner

Your call makes no sense to start with. Why envelop them? It's a usage error AFAICT.

R> anydate("2017-01-01 19:00")
[1] "2017-01-01"
R> 
@eddelbuettel eddelbuettel changed the title from POSIXct to Date conversion needs timezone to POSIXct to Date conversion could use timezone Feb 7, 2017
@ampu3ro
ampu3ro commented Feb 7, 2017

because I have a vector of class POSIXct and I want to convert it to Date. It seemed to me that that intention of your package was to simplify/speed up all the back and forth formatting when parsing/converting in R. yes I could obviously do

> x <- as.POSIXct("2017-01-01 19:00","America/New_York")
> as.Date(format(x,"%F"))
[1] "2017-01-01"

by why are you opposed to something like

> anydate(x)
[1] "2017-01-02" #what it currently returns

# vs:
> anydate(x)
[1] "2017-01-01" #what it should return

??

@eddelbuettel
Owner

Why format? Just do as.Date(myVectorOfPOSIXct).

I am going to lock this now. If you continue to have basic R questions, consider Stackoverflow or the help lists.

@eddelbuettel eddelbuettel locked and limited conversation to collaborators Feb 7, 2017
@eddelbuettel
Owner
eddelbuettel commented Feb 7, 2017 edited

Your whole confusion is due to the fact that as.POSIXct strips attributes. Consider this approach using as.POSIXlt.

R> as.Date(x <- as.POSIXlt("2017-01-01 19:00","America/New_York"), tzone="America/New_York")
[1] "2017-01-01"
R> 
R> as.Date(x <- as.POSIXlt("2017-01-01 19:00","America/New_York"))
[1] "2017-01-01"
R> 
R> as.Date(x <- as.POSIXlt("2017-01-01 19:00"))
[1] "2017-01-01"
R> 

But these are known quirks in Base R and, while esoteric, have nothing to do with my package.

The tzone argument is a side show here. It does not matter AFAICT.

@eddelbuettel
Owner

Change applied in #50 though

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.