Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add make_date() feature for converting date-times into dates #355

Closed
huftis opened this issue Nov 3, 2015 · 5 comments
Closed

Add make_date() feature for converting date-times into dates #355

huftis opened this issue Nov 3, 2015 · 5 comments
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@huftis
Copy link

huftis commented Nov 3, 2015

I propose adding a convenience function make_date() for converting date-times to dates. One might by tempted to use as.Date(), and this works in many cases, but with the default options it gives surprising results in a few cases. Example:

x1 = as.POSIXct("2010-08-03 00:59:59.23")
x2 = as.POSIXct("2010-08-03 00:59:59.23", tz="Europe/London")
x3 = as.POSIXct("2010-11-03 00:59:59.23")
x4 = as.POSIXct("2010-11-03 00:59:59.23", tz="Europe/London")

The naïve user might expect as.Date() to return the 3rd of the month (August/November) for all of x1x4. However, on my system (a Europe/Oslo timezone), I get:

> as.Date(x1)
[1] "2010-08-02"
> as.Date(x2)
[1] "2010-08-02"
> as.Date(x3)
[1] "2010-11-02"
> as.Date(x4)
[1] "2010-11-03"

It’s even more surprising if you have already used floor_date(x, "day") on the objects to remove the ‘time’ part.

The way to get the correct date (Date) corresponding to a date-time is either as.Date(format(x, "%Y-%m-%d")) or (preferably) as.Date(x, tz=attr(x, "tzone")):

> as.Date(x1, tz=attr(x1, "tzone"))
[1] "2010-08-03"
> as.Date(x2, tz=attr(x2, "tzone"))
[1] "2010-08-03"
> as.Date(x3, tz=attr(x3, "tzone"))
[1] "2010-11-03"
> as.Date(x4, tz=attr(x4, "tzone"))
[1] "2010-11-03"

So I propose adding such a convenience function make_date() to lubridate, that would run

as.Date(x, tz=attr(x, "tzone"))

for POSIXct and POSIXlt objects (and just return x for Date() objects).

I just based the name on the make_difftime() function; other names may better.

@hadley
Copy link
Member

hadley commented Dec 14, 2015

This seems more like a bug in base R, in my opinion.

@vspinu
Copy link
Member

vspinu commented Dec 15, 2015

bug in base R,

I also think this should be addressed in base R. No need to duplicate existent functions. Would you bother firing a bug report?

I think make_date and make_datetime could serve as better names for existent ISOdate and ISOdatetime functions.

@vspinu vspinu added the bug an unexpected problem or unintended behavior label Dec 15, 2015
@huftis
Copy link
Author

huftis commented Dec 15, 2015

I’m pretty sure this will not be seen as bug by R Core. It’s documented and intentional. While I (and we) see it as a bug/misfeature, I see no chance of the default behaviour being changed in R base.

So the only chance of having a widely-used sensible and consistent datetime-to-date function would be having such a function in the lubridate package. (Of course, this is not a required function, as it’s easy to work around, but it would be very helpful having it as a convenience function. I like convenience functions. ☺)

@hadley
Copy link
Member

hadley commented Dec 15, 2015

@huftis oh good point, I didn't see that it was documented

@vspinu I think it's better to keep creation and coercion functions separate, but we could have as_date() and make_date()

@imanuelcostigan
Copy link
Contributor

@huftis This annoys me greatly. I will submit a pull request.

This was referenced Mar 6, 2016
@vspinu vspinu added this to the v1.5.6 milestone Mar 13, 2016
@vspinu vspinu closed this as completed in a78509a Mar 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants