tidyverse / lubridate

Make working with dates in R just that little bit easier
https://lubridate.tidyverse.org
GNU General Public License v3.0
733 stars 207 forks source link

issue with flooring and ceiling dates #645

Closed bart1 closed 6 years ago

bart1 commented 6 years ago

I noticed some strange results where ceiling date sometimes does not produce a rounded date (see the example where the time becomes 1 am)

> a<-structure(631166450, class = c("POSIXct", "POSIXt"), tzone = "")
> a
[1] "1990-01-01 05:00:50 CET"
> floor_date(a,'days');ceiling_date(a,'days')
[1] "1990-01-01 CET"
[1] "1990-01-02 01:00:00 CET"
> 
> a<-structure(631166450, class = c("POSIXct", "POSIXt"), tzone = "CET")
> a
[1] "1990-01-01 05:00:50 CET"
> floor_date(a,'days');ceiling_date(a,'days')
[1] "1990-01-01 CET"
[1] "1990-01-02 CET"
> 
> Sys.timezone()
[1] "Europe/Zurich"
> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: OS X El Capitan 10.11.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] lubridate_1.7.2 browMod_0.1     Rcpp_0.12.15    move_3.0.3      rgdal_1.2-16    raster_2.6-7    sp_1.2-7        geosphere_1.5-7 mgcv_1.8-23    
[10] nlme_3.1-131   

loaded via a namespace (and not attached):
 [1] compiler_3.4.2     pillar_1.1.0       plyr_1.8.4         tools_3.4.2        digest_0.6.15      memoise_1.1.0      tibble_1.4.2       gtable_0.2.0      
 [9] lattice_0.20-35    rlang_0.1.6        Matrix_1.2-12      yaml_2.1.16        parallel_3.4.2     gridExtra_2.3      httr_1.3.1         stringr_1.2.0     
[17] xml2_1.2.0         stats4_3.4.2       grid_3.4.2         inline_0.3.14      R6_2.2.2           rstan_2.17.3       ggplot2_2.2.1      magrittr_1.5      
[25] splines_3.4.2      scales_0.5.0       codetools_0.2-15   StanHeaders_2.17.2 colorspace_1.3-2   stringi_1.1.6      lazyeval_0.2.1     munsell_0.4.3     
bart1 commented 6 years ago

Not sure if it relates to #642 but here is the test code anyway

> Sys.timezone()
[1] "Europe/Zurich"
> Sys.getenv("TZ")
[1] ""
> lubridate:::C_local_tz()
[1] "raster465"
> as.POSIXct("2017-08-13", tz = "")
[1] "2017-08-13 CEST"
> ymd("2017-08-13", tz = "")
[1] "2017-08-13 02:00:00 CEST"
> force_tz(as.POSIXct("2017-08-13", tz = ""), "UTC")
[1] "2017-08-12 22:00:00 UTC"
> 
> t1 <- as.POSIXct("2018-02-28 20:10:00")
> lubridate::minute(t1) <- 0
> t1
[1] "2018-02-28 21:00:00 CET"
> as.POSIXct("2017-07-01", format = "%Y-%m-%d") + days(1)
[1] "2017-06-02 02:00:00 CEST"
vspinu commented 6 years ago

lubridate:::C_local_tz() [1] "raster465"

Ok. This is messed up :( I am surprised you are not seeing explicit TZ errors with this.

For now setting Sys.setenv(TZ="Europe/Zurich") should fix this.

vspinu commented 6 years ago

Would you mind checking the github version?

bart1 commented 6 years ago

I just installed the devel version from github and it seems to work at least for the ceiling example

> Sys.timezone()
[1] "Europe/Zurich"
> Sys.getenv("TZ")
[1] ""
> lubridate:::C_local_tz()
[1] "Europe/Zurich"
> as.POSIXct("2017-08-13", tz = "")
[1] "2017-08-13 CEST"
> ymd("2017-08-13", tz = "")
[1] "2017-08-13 CEST"
> force_tz(as.POSIXct("2017-08-13", tz = ""), "UTC")
[1] "2017-08-13 UTC"
> 
> t1 <- as.POSIXct("2018-02-28 20:10:00")
> lubridate::minute(t1) <- 0
> t1
[1] "2018-02-28 20:00:00 CET"
> as.POSIXct("2017-07-01", format = "%Y-%m-%d") + days(1)
[1] "2017-07-02 CEST"
> 
> a<-structure(631166450, class = c("POSIXct", "POSIXt"), tzone = "")
> a
[1] "1990-01-01 05:00:50 CET"
> floor_date(a,'days');ceiling_date(a,'days')
[1] "1990-01-01 CET"
[1] "1990-01-02 CET"
> 
> a<-structure(631166450, class = c("POSIXct", "POSIXt"), tzone = "CET")
> a
[1] "1990-01-01 05:00:50 CET"
> floor_date(a,'days');ceiling_date(a,'days')
[1] "1990-01-01 CET"
[1] "1990-01-02 CET"
> 
> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: OS X El Capitan 10.11.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lubridate_1.7.2

loaded via a namespace (and not attached):
 [1] httr_1.3.1      compiler_3.4.2  magrittr_1.5    R6_2.2.2        tools_3.4.2     withr_2.1.1     curl_3.1        yaml_2.1.16    
 [9] Rcpp_0.12.15    memoise_1.1.0   stringi_1.1.6   knitr_1.20      git2r_0.21.0    stringr_1.3.0   digest_0.6.15   devtools_1.13.5
vspinu commented 6 years ago

Ok. Perfect. Will release asap. Thanks for the report!