Bug 16923

Summary: pretty fails on some sets of dates
Product: R Reporter: Dan Keshet <dan.keshet>
Component: Low-levelAssignee: R-core <R-core>
Status: REOPENED ---    
Severity: normal CC: maechler
Priority: P5    
Version: R 3.3.*   
Hardware: All   
OS: Linux   
Attachments: RDS file with dates that trigger bug
Against R devel r70763
Against R devel r70763

Description Dan Keshet 2016-05-25 19:25:15 UTC
Created attachment 2094 [details]
RDS file with dates that trigger bug

On version 3.3.0:

x <- readRDS( "bugdates.RDS")
pretty( x )
# Errors:
#
# Error in seqDtime(at[length(at)], by = s$spec, length = 2) : 
#  'mday' must be 1 or 15 for "halfmonth"

# The timezone seems to affect the results

pretty( lubridate::with_tz(x, "UTC" ))
# Returns:
#
# [1] "2016-03-01 UTC" "2016-03-15 UTC" "2016-04-01 UTC" "2016-04-15 UTC"
# [5] "2016-05-01 UTC"

This worked on version 3.2.5:

x <- readRDS( "bugdates.RDS" )
pretty( x )
# Returns:
#
# [1] "2016-03-07 00:00:00 AEDT" "2016-03-14 00:00:00 AEDT"
# [3] "2016-03-21 00:00:00 AEDT" "2016-03-28 00:00:00 AEDT"
# [5] "2016-04-03 23:00:00 AEST" "2016-04-10 23:00:00 AEST"
Comment 1 Martin Maechler 2016-05-26 08:39:34 UTC
Confirmed.  Easier reproducible code (no *.rds; no lubridate()):

x <- structure(c(1456837200, 1460728800), class = c("POSIXct", "POSIXt"),
               tzone = "Australia/Sydney")
x # "2016-03-02 AEDT" "2016-04-16 AEST"
pretty( x )
## Error in seqDtime(at[length(at)], by = s$spec, length = 2) :
##  'mday' must be 1 or 15 for "halfmonth"

# The timezone seems to affect the results
x. <- x; attr(x., "tzone") <- "UTC"
pretty(x.) # returns
## [1] "2016-03-01 UTC" "2016-03-15 UTC" "2016-04-01 UTC" "2016-04-15 UTC"
## [5] "2016-05-01 UTC"
Comment 2 Martin Maechler 2016-05-26 17:56:09 UTC
I have a fix which is "nice" in the sense that it is easy to see that it is backwards (to 3.3.0) compatible in cases it previously worked.

I don't think it is nice more broadly, but that is impossible with that whole code.  Pretty dates in a world where months have beetween 28 and 31 days is not easily possible .. ;-)

I have commited the fix now, both for R-devel (svn rev 70668) and R (3.3.0) patched (svn rev 70669-71).
Comment 3 Suharto Anggono 2016-05-27 15:34:52 UTC
What is supposed to be 2016-04-15 00:00:00 with DST is 2016-04-14 23:00:00 without DST.

However, the example succeeds on my system, Windows, using 32-bit R 3.3.0.

A technique like in function 'seq.POSIXt' seems to avoid the "shift": adding
    at2$isdst <- -1L
before conversion to POSIXct in grDevices:::seqDtime.
Comment 4 Suharto Anggono 2016-06-10 23:31:42 UTC
(In reply to Suharto Anggono from comment #3)
> What is supposed to be 2016-04-15 00:00:00 with DST is 2016-04-14 23:00:00
> without DST.
> 
> However, the example succeeds on my system, Windows, using 32-bit R 3.3.0.
> 
> A technique like in function 'seq.POSIXt' seems to avoid the "shift": adding
>     at2$isdst <- -1L
> before conversion to POSIXct in grDevices:::seqDtime.

Work in http://www.tutorialspoint.com/r_terminal_online.php :

> lim <- .POSIXct(c(1456837200, 1460728800), tz = "Australia/Sydney")                                           
> lim                                                                                                           
[1] "2016-03-02 AEDT" "2016-04-16 AEST"                                                                         
> startTime <- trunc.POSIXt(lim[1], "days")                                                                     
> startTime$mday <- 1                                                                                           
> at <- seq(startTime, lim[2], by = "months")                                                                   
> at                                                                                                            
[1] "2016-03-01 AEDT" "2016-04-01 AEDT"                                                                         
> at2 <- as.POSIXlt(at)                                                                                         
> at2$isdst                                                                                                     
[1] 1 1                                                                                                         
> at2$mday <- 15L                                                                                               
> as.POSIXct(at2)                                                                                               
[1] "2016-03-15 00:00:00 AEDT" "2016-04-14 23:00:00 AEST"                                                       
> at2$isdst <- -1L                                                                                              
> as.POSIXct(at2)                                                                                               
[1] "2016-03-15 AEDT" "2016-04-15 AEST"                                                                         
> sessionInfo()                                                                                                 
R version 3.2.3 (2015-12-10)                                                                                    
Platform: x86_64-redhat-linux-gnu (64-bit)                                                                      
Running under: Fedora 23 (Twenty Three)                                                                         
                                                                                                                
locale:                                                                                                         
[1] C                                                                                                           
                                                                                                                
attached base packages:                                                                                         
[1] stats     graphics  grDevices utils     datasets  methods   base
Comment 5 Martin Maechler 2016-06-11 07:30:32 UTC
Thank you, Suharto Anggono,

this will not make 3.3.1  I am sorry, but I agree it should be fixed.

I also believe that temporary setting of  isdst to  -1   may be the best approach.  When I looked it did not seem obvious to me *where* to change the R code (without making it considerably more complicated).

If you see the solution, we'd be grateful for a patch proposal.
Thank you in advance!
Comment 6 Suharto Anggono 2016-06-11 11:32:05 UTC
Created attachment 2103 [details]
Against R devel r70763
Comment 7 Suharto Anggono 2016-06-11 11:45:36 UTC
Created attachment 2104 [details]
Against R devel r70763