The R Project SVN R

Rev

Rev 90029 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 90029 Rev 90178
Line 4... Line 4...
4
.pt <- proc.time()
4
.pt <- proc.time()
5
 
5
 
6
tryCmsg <- function(expr) tryCatch(expr, error = conditionMessage) # typically == *$message
6
tryCmsg <- function(expr) tryCatch(expr, error = conditionMessage) # typically == *$message
7
assertErrV <- function(...) tools::assertError(..., verbose=TRUE)
7
assertErrV <- function(...) tools::assertError(..., verbose=TRUE)
8
options(warn = max(1, getOption("warn")))
8
options(warn = max(1, getOption("warn")))
9
 
-
 
-
 
9
myInteractive <- function() interactive() ||
-
 
10
                                as.logical( Sys.getenv("_R_CHECK_INTERACTIVE_datetime3_", FALSE) )
10
if(!nzchar(Sys.getenv("_R_CHECK_DATETIME3_NO_TZ_"))) withAutoprint({
11
if(!nzchar(Sys.getenv("_R_CHECK_DATETIME3_NO_TZ_"))) withAutoprint({
11
  ## For some inter-platform reproducibility, try to set timezone
12
  ## For some inter-platform reproducibility, try to set timezone
12
  ## even though  Sys.setenv(..) does *NOT* always work
13
  ## even though  Sys.setenv(..) does *NOT* always work
13
 
14
 
14
  ## do this early: PR#19005
15
  ## do this early: PR#19005
Line 879... Line 880...
879
}) ##   delta  was   8.742e-8
880
}) ##   delta  was   8.742e-8
880
## in R <= 4.5.2 when c(<POSIXlt>) worked via POSIXct
881
## in R <= 4.5.2 when c(<POSIXlt>) worked via POSIXct
881
 
882
 
882
 
883
 
883
## c.POSIXlt, [<-.POSIXlt , [[, etc, for far (and very far) future -- PR#18989
884
## c.POSIXlt, [<-.POSIXlt , [[, etc, for far (and very far) future -- PR#18989
-
 
885
show_nonTRUE <- function(v) {
-
 
886
    if(R <- all(v)) return(R)
-
 
887
    ## else
-
 
888
    cat("some not TRUE in ", deparse(substitute(v)),":  ",
-
 
889
        paste(symnum(v), collapse = " "), "\n", sep="")
-
 
890
    TRUE
-
 
891
}
-
 
892
## "data" to be used below
-
 
893
ep <- 4^-(1:25)
-
 
894
x_s25 <- 60 - ep # `$<-.POSIXlt`() effectively *removing* "balanced" :
-
 
895
(il2d.sec <- round(log2(60 - x_s25))) # -2 -4 .. -46 -Inf -Inf
-
 
896
stopifnot(identical(il2d.sec, -2*c(1:23, Inf,Inf)))
-
 
897
ok.s25 <- is.finite(il2d.sec)
884
for(thtz in c("UTC", "Europe/Kyiv", "Asia/Kolkata", "Pacific/Auckland"
898
for(thtz in c("UTC", "Europe/Kyiv", "Asia/Kolkata", "Pacific/Auckland"
885
            , "Asia/Hebron", "Canada/Mountain", "Navajo"
899
            , "Asia/Hebron", "Canada/Mountain", "Navajo"
886
              )) { # dbg: withAutoprint({
900
              )) { # dbg: withAutoprint({
887
  x0 <- as.POSIXlt("9999-12-31 23:59:59.99999", tz=thtz)
901
  x0 <- as.POSIXlt("9999-12-31 23:59:59.99999", tz=thtz)
888
  print(x0)
902
  cat("\n>> TZ ", thtz,": ", sep=""); print(x0)
889
  xx <- x0; xx$year <- x0$year + c(0L, as.integer(10^(0:9)))
903
  xx <- x0; xx$year <- x0$year + c(0L, as.integer(10^(0:9)))
-
 
904
  xx # length 11 , but xx$sec has still length 1
-
 
905
  if(FALSE) { ## for now -- This kills   attr(lcx, "tzone")[1L] == thtz   {below}
-
 
906
      xx <- sort(c(xx, .POSIXct(c(.25, .5, .8, .9, .95, .99)*as.numeric(xx[11]))))
890
  xx # length 11
907
      xx$sec[] <- xx$sec[1]
-
 
908
  }
891
  cxx <- c(xx) # differs when "large":
909
  cxx <- c(xx) # differs when "large":
892
  lrgDT <- function(x) unclass(as.POSIXct(x)) >= 2^53
910
  lrgDT <- function(x) unclass(as.POSIXct(x)) >= 2^53
893
  lrg <- lrgDT(xx)
911
  lrg <- lrgDT(xx)
894
  ok <- !lrg ## NB: exact [small | large] boundary is between the two years  285426850 + 0:1
912
  ok <- !lrg ## NB: exact [small | large] boundary is between the two years  1970+285426850 + 0:1
895
  stopifnot(cxx == xx,
913
  stopifnot(cxx == xx,
896
            cxx - xx == 0,
914
            cxx - xx == 0,
897
            cxx$sec[ok] == xx$sec)
915
            cxx$sec[ok] == xx$sec)
898
  ## checking `[` , `[[` , `$<-`
916
  ## checking `[` , `[[` , `$<-`
899
  for(i in seq_along(xx)) { # dbg: withAutoprint({
917
  for(i in seq_along(xx)) { # dbg: withAutoprint({
900
    x <- xx[i]
918
    x <- xx[i]
901
    (ff <- c(format(x), format(x, digits = 6, usetz=TRUE)))
-
 
902
    ep <- 4^-(1:25)
-
 
903
    x$sec <- 60 - ep # `$<-.POSIXlt`() effectively *removing* "balanced" :
919
    x$sec <- x_s25 # `$<-.POSIXlt`() effectively *removing* "balanced" :
904
    (il2d.sec <- round(log2(60 - x$sec))) # -2 -4 .. -46 -Inf -Inf
-
 
905
    lcx <- as.POSIXlt(as.POSIXct(x)) # lossy
920
    lcx <- as.POSIXlt(as.POSIXct(x)) # lossy
-
 
921
    if(myInteractive()) {
-
 
922
        cat("x: "); ff <- c(format(x), format(x, digits = 6, usetz=TRUE)); print(ff); cat(":\n")
906
    if(interactive()) utils:::str.default(lcx) # else print(ff)
923
        utils:::str.default(lcx) # else print(ff)
-
 
924
    }
907
    stopifnot(exprs = {
925
    stopifnot(exprs = {
908
        is.null(attr(x, "balanced"))
926
        is.null(attr(x, "balanced"))
909
        identical(il2d.sec, -2*c(1:23, Inf,Inf))
-
 
910
        lengths(unclass(lcx)) == length(ep)
927
        lengths(unclass(lcx)) == length(ep)
911
        attr(lcx, "balanced")
928
        attr(lcx, "balanced")
912
        attr(lcx, "tzone")[1L] == thtz
929
        attr(lcx, "tzone")[1L] == thtz
913
        all.equal(x, (bx <- balancePOSIXlt(x)), tolerance = 0)# FIXME? all.equal.POSIXt() currently via as.POSIXct()
930
        all.equal(x, (bx <- balancePOSIXlt(x)), tolerance = 0)# FIXME? all.equal.POSIXt() currently via as.POSIXct()
914
        attr(bx, "balanced")
931
        attr(bx, "balanced")
Line 917... Line 934...
917
        lcx == x ; x - lcx == 0 # as Ops currently work via as.POSIXct(), losing ..
934
        lcx == x ; x - lcx == 0 # as Ops currently work via as.POSIXct(), losing ..
918
        bx == lcx               # (ditto)
935
        bx == lcx               # (ditto)
919
        (cx <- c(x)) == x  # was all TRUE in R 4.5.3
936
        (cx <- c(x)) == x  # was all TRUE in R 4.5.3
920
    })
937
    })
921
    ##
938
    ##
922
    if(ok[i]) stopifnot(cx$sec[1:23] == x$sec[1:23])
939
    if(ok[i]) stopifnot(cx$sec[ok.s25] == x$sec[ok.s25])
923
    x25 <- x. <- x
940
    x25 <- x. <- x
924
    x[1:25] <- x25[1:25]
941
    x[1:25] <- x25[1:25]
925
    for(i in c(7L, 12L, 20:25)) x.[[i]] <- x25[[i]]
942
    for(j in c(7L, 12L, 20:25)) x.[[j]] <- x25[[j]]
926
    stopifnot(exprs = {
943
    stopifnot(exprs = {
927
        x[25]$sec == 60
944
        x[25]$sec == 60
928
        identical(x, x.)
945
        identical(x, x.)
929
        is.na(attr(x, "balanced"))# NA: x[25]$sec == 60
946
        is.na(attr(x, "balanced"))# NA: x[25]$sec == 60
930
        all.equal(il2d.sec, log2(60 - x$sec), tolerance = 4*.Machine$double.eps)
947
        all.equal(il2d.sec, log2(60 - x$sec), tolerance = 4*.Machine$double.eps)
931
      ! identical(x, x25)
948
      ! identical(x, x25)
932
        attr(x25, "tzone") == thtz
949
        attr(x25, "tzone") == thtz
933
        attr(x  , "tzone") == thtz
950
        attr(x  , "tzone") == thtz
934
    })
951
    })
-
 
952
    ## new (June 2026) -- TODO: more checks, less show_nonTRUE()
-
 
953
    xG. <- xG <- as.POSIXlt(.POSIXct(numeric(25), tz = "GMT"))
-
 
954
    xG[1:25]  <- x25[1:25]
-
 
955
    xG.[[7]]  <- x25[[7]]
-
 
956
    xG.[[25]] <- x25[[25]]
-
 
957
    cat("all.eq(x25, xG):  "); print(all.equal(x25, xG ))
-
 
958
    stopifnot(exprs = {
-
 
959
        inherits(xG , "POSIXlt")
-
 
960
        inherits(xG., "POSIXlt"); inherits(xG.[[7]], "POSIXlt")
-
 
961
        xG.[[7]]  - x25[[7]]  == 0 # not ok in MM's R-dev./bugzilla May 1, 2026
-
 
962
        xG.[[25]] - x25[[25]] == 0
-
 
963
        xG[1:25]  - x25[1:25] == 0 # (even when '==' is not accurate for POSIXlt)
-
 
964
        if(ok[i]) xG$sec[ok.s25] == x25$sec[ok.s25] else TRUE
-
 
965
        show_nonTRUE(
-
 
966
            xG.$sec[c(7, 25)] == x25$sec[c(7, 25)] )
-
 
967
        show_nonTRUE(
-
 
968
            xG $wday == x25$wday          ) # ok in R 4.5.3
-
 
969
        show_nonTRUE(
-
 
970
            xG.$wday[c(7, 25)] == x25$wday) # ditto
-
 
971
    })
935
  }#) ##  for(i in .....)
972
  }#) ##  for(i in .....)
936
}#) ## for(thtz in ..)
973
}#) ## for(thtz in ..)
-
 
974
## --- tzone behaviour for Arith "-"  {non-large times}:
-
 
975
yU <- .POSIXct(0, tz = "UTC")             # midnight, i.e., 00:00
-
 
976
y  <- .POSIXct(0, tz = "Pacific/Auckland")# "
-
 
977
stopifnot(y - yU == 0) # (always)
-
 
978
x2 <- x1 <- as.POSIXlt(yU)
-
 
979
x2[[1]] <- x1[1] <- y
-
 
980
stopifnot(exprs = {
-
 
981
    x1[1] - y == 0
-
 
982
    x2[1] - y == 0
-
 
983
    x2[1] == yU
-
 
984
    y == x1[1] # + Warning in .check_tzones(e1, e2) : 'tzone' attributes are inconsistent
-
 
985
})
-
 
986
## these  were all FALSE in R 4.5.3
937
 
987
 
938
 
988
 
939
## PR#19038:- wrong as.POSIXct(x) for POSIXlt object x  w/ x$year close to integer.max
989
## PR#19038:- wrong as.POSIXct(x) for POSIXlt object x  w/ x$year close to integer.max
940
x <- as.POSIXlt(c("1900-01-01", "1900-01-01"), tz="UTC")
990
x <- as.POSIXlt(c("1900-01-01", "1900-01-01"), tz="UTC")
941
x$year <- .Machine$integer.max - (1901:1899)
991
x$year <- .Machine$integer.max - (1901:1899)