lubridate icon indicating copy to clipboard operation
lubridate copied to clipboard

Ceiling date failing

Open bart1 opened this issue 6 years ago • 7 comments

I just encountered this issue (both with devel and current cran, I cant reproduce it on linux with R 3.4.3). The ceiling date of 1970-01-01 00:00:00 UTC becomes 1969-12-02 UTC. I will tonight try on a different machine.

> require(lubridate)
> t<-structure(c(0, 86.4864864864865, 172.972972972973, 259.459459459459, 345.945945945946, 432.432432432432), class = 
+ c("POSIXct", "POSIXt"), tzone = "UTC")   
> floor_date(t, unit='day')
[1] "1970-01-01 UTC" "1970-01-01 UTC" "1970-01-01 UTC" "1970-01-01 UTC"
[5] "1970-01-01 UTC" "1970-01-01 UTC"
> ceiling_date(t, unit='day')
[1] "1969-12-02 UTC" "1970-01-02 UTC" "1970-01-02 UTC" "1970-01-02 UTC"
[5] "1970-01-02 UTC" "1970-01-02 UTC"
> sessionInfo()
R version 3.5.1 (2018-07-02)
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.4.9000

loaded via a namespace (and not attached):
[1] compiler_3.5.1 Rcpp_1.0.0

bart1 avatar Jan 09 '19 14:01 bart1

For the record the bug reproduces on a indepentent osx system

> require(lubridate)
Loading required package: lubridate

Attaching package: ‘lubridate’

The following object is masked from ‘package:base’:

    date

> t<-structure(c(0, 86.4864864864865, 172.972972972973, 259.459459459459, 345.945945945946, 432.432432432432), class = c("POSIXct", "POSIXt"), tzone = "UTC") 
> ceiling_date(t, unit='day')
[1] "1969-12-02 UTC" "1970-01-02 UTC" "1970-01-02 UTC" "1970-01-02 UTC"
[5] "1970-01-02 UTC" "1970-01-02 UTC"
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.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.4

loaded via a namespace (and not attached):
[1] compiler_3.5.1 magrittr_1.5   tools_3.5.1    yaml_2.2.0     Rcpp_1.0.0    
[6] stringi_1.2.4  stringr_1.3.1 

bart1 avatar Jan 09 '19 17:01 bart1

An ubuntu system works correctly

> require(lubridate)
Loading required package: lubridate

Attaching package: ‘lubridate’

The following object is masked from ‘package:base’:

    date

> t<-structure(c(0, 86.4864864864865, 172.972972972973, 259.459459459459, 345.945945945946, 432.432432432432), class = c("POSIXct", "POSIXt"), tzone = "UTC") 
> ceiling_date(t, unit='day')
[1] "1970-01-01 UTC" "1970-01-02 UTC" "1970-01-02 UTC" "1970-01-02 UTC"
[5] "1970-01-02 UTC" "1970-01-02 UTC"
> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=de_CH.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=de_CH.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=de_CH.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=de_CH.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] lubridate_1.7.4

loaded via a namespace (and not attached):
[1] compiler_3.5.2 magrittr_1.5   tools_3.5.2    Rcpp_1.0.0     stringi_1.2.4 
[6] stringr_1.3.1 

bart1 avatar Jan 09 '19 18:01 bart1

Looks like OS dependent issue. Would you mind installing timechange package and trying

timechange::time_ceiling(ymd("1970-01-01", tz = "UTC"), "day")

I had in plan to move lubridate on top of timechange for a while but just couldn't find time. If it works for you I will make it a higher priority. Thanks.

vspinu avatar Jan 11 '19 19:01 vspinu

That seems to work

> timechange::time_ceiling(lubridate::ymd("1970-01-01", tz = "UTC"), "day")
[1] "1970-01-01 UTC"
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: OS X El Capitan 10.11.6
> t<-structure(c(0, 86.4864864864865, 172.972972972973, 259.459459459459, 345.945945945946, 432.432432432432), class = c("POSIXct", "POSIXt"), tzone = "UTC") 
> timechange::time_ceiling(t, "day")
[1] "1970-01-01 UTC" "1970-01-02 UTC" "1970-01-02 UTC" "1970-01-02 UTC"
[5] "1970-01-02 UTC" "1970-01-02 UTC"

bart1 avatar Jan 15 '19 11:01 bart1

FYI I also have this problem. Problem doesn't only occur right at 1970, but seems like the first of any month (I've tried about 10 dates) before 1970 encounters this problem. Changing to timechange::time_ceiling also fixes the problem for me.

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

#works as expected
testdatePost1970 <- as.POSIXct("1971-09-01 00:00", format = "%Y-%m-%d %H:%M", tz = "UTC")

ceiling_date(testdatePost1970, unit = "day", change_on_boundary = F)
#> [1] "1971-09-01 UTC"
ceiling_date(testdatePost1970, unit = "day", change_on_boundary = T)
#> [1] "1971-09-02 UTC"

#gives completely unexpected date if change_on_boundary = F

testdatePre1970 <- as.POSIXct("1959-09-01 00:00", format = "%Y-%m-%d %H:%M", tz = "UTC")

ceiling_date(testdatePre1970, unit = "day", change_on_boundary = F)
#> [1] "1959-08-02 UTC"
ceiling_date(testdatePre1970, unit = "day", change_on_boundary = T)
#> [1] "1959-09-02 UTC"


timechange::time_ceiling(testdatePre1970, "day",change_on_boundary = F)
#> [1] "1959-09-01 UTC"
timechange::time_ceiling(testdatePre1970, "day",change_on_boundary = T)
#> [1] "1959-09-02 UTC"


sessionInfo()
#> R version 3.5.3 (2019-03-11)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 17763)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
#> [3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
#> [5] LC_TIME=English_Australia.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] lubridate_1.7.4
#> 
#> loaded via a namespace (and not attached):
#>  [1] compiler_3.5.3   magrittr_1.5     tools_3.5.3      htmltools_0.3.6 
#>  [5] yaml_2.2.0       Rcpp_1.0.1       stringi_1.4.3    rmarkdown_1.13  
#>  [9] highr_0.8        knitr_1.23       stringr_1.4.0    xfun_0.7        
#> [13] digest_0.6.19    timechange_0.0.1 evaluate_0.13

Created on 2019-11-11 by the reprex package (v0.3.0)

Blundys avatar Nov 11 '19 04:11 Blundys

Minimal reprex:

library(lubridate, warn.conflicts = FALSE)

ceiling_date(ymd("1970-01-01", tz = "UTC"), "day")
#> [1] "1969-12-02 UTC"
timechange::time_ceiling(ymd("1970-01-01", tz = "UTC"), "day")
#> [1] "1970-01-01 UTC"

Created on 2019-11-19 by the reprex package (v0.3.0)

hadley avatar Nov 19 '19 23:11 hadley

Also works with clock

library(clock)

date_ceiling(date_time_build(1970, 1, 1, zone = "UTC"), precision = "day")
#> [1] "1970-01-01 UTC"

DavisVaughan avatar May 25 '21 13:05 DavisVaughan

Moved on top of timechange. Issue should be fixed.

vspinu avatar Nov 05 '22 12:11 vspinu