1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
R Under development (unstable) (2024-03-14 r86129) -- "Unsuffered Consequences"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> ### more date-time tests where the output is to be checked.
>
> ## Expect differences, especially for platforms without tm_zone/tm_gmtoff.
>
> options(warn = 1L)
>
> ## tests of "POSIXlt" objects
> xU <- strptime("2022-01-01", "%Y-%m-%d", tz = "UTC")
> xU
[1] "2022-01-01 UTC"
> str(unclass(xU))
List of 11
$ sec : num 0
$ min : int 0
$ hour : int 0
$ mday : int 1
$ mon : int 0
$ year : int 122
$ wday : int 6
$ yday : int 0
$ isdst : int 0
$ zone : chr "UTC"
$ gmtoff: int 0
- attr(*, "tzone")= chr "UTC"
- attr(*, "balanced")= logi TRUE
>
> x0 <- strptime("2022-01-01", "%Y-%m-%d") # current time zone
> ## IGNORE_RDIFF_BEGIN
> x0
[1] "2022-01-01 CET"
> str(unclass(x0))
List of 11
$ sec : num 0
$ min : int 0
$ hour : int 0
$ mday : int 1
$ mon : int 0
$ year : int 122
$ wday : int 6
$ yday : int 0
$ isdst : int 0
$ zone : chr "CET"
$ gmtoff: int NA
- attr(*, "tzone")= chr [1:3] "" "CET" "CEST"
- attr(*, "balanced")= logi TRUE
> ## IGNORE_RDIFF_END
>
> x1 <- strptime("2022-07-01", "%Y-%m-%d", tz = "Europe/London")
> x1
[1] "2022-07-01 BST"
> str(unclass(x1)) # gmtoff is NA
List of 11
$ sec : num 0
$ min : int 0
$ hour : int 0
$ mday : int 1
$ mon : int 6
$ year : int 122
$ wday : int 5
$ yday : int 181
$ isdst : int 1
$ zone : chr "BST"
$ gmtoff: int NA
- attr(*, "tzone")= chr [1:3] "Europe/London" "GMT" "BST"
- attr(*, "balanced")= logi TRUE
>
> ## give offset value -- makes most sense in UTC
> x2 <- strptime("2022-07-01 10:55:03 +0300", "%Y-%m-%d %H:%M:%S %z", tz = "UTC")
> x2
[1] "2022-07-01 07:55:03 UTC"
> str(unclass(x2))
List of 11
$ sec : num 3
$ min : int 55
$ hour : int 7
$ mday : int 1
$ mon : int 6
$ year : int 122
$ wday : int 5
$ yday : int 181
$ isdst : int 0
$ zone : chr "UTC"
$ gmtoff: int 0
- attr(*, "tzone")= chr "UTC"
- attr(*, "balanced")= logi TRUE
> ## in another tzone it will report the time in that zone, with its DST ....
> x3 <- strptime("2022-07-01 10:55:03 +0300", "%Y-%m-%d %H:%M:%S %z",
+ tz = "Europe/Vienna")
> x3
[1] "2022-07-01 09:55:03"
> str(unclass(x3))
List of 11
$ sec : num 3
$ min : int 55
$ hour : int 9
$ mday : int 1
$ mon : int 6
$ year : int 122
$ wday : int 5
$ yday : int 181
$ isdst : int 1
$ zone : chr ""
$ gmtoff: int NA
- attr(*, "tzone")= chr [1:3] "Europe/Vienna" "CET" "CEST"
- attr(*, "balanced")= logi TRUE
>
> x4 <- strptime("2022-07-01", "%Y-%m-%d", tz ="Pacific/Fiji")
> x4
[1] "2022-07-01 +12"
> str(unclass(x4)) # abbreviations may be numbers.
List of 11
$ sec : num 0
$ min : int 0
$ hour : int 0
$ mday : int 1
$ mon : int 6
$ year : int 122
$ wday : int 5
$ yday : int 181
$ isdst : int 0
$ zone : chr "+12"
$ gmtoff: int NA
- attr(*, "tzone")= chr [1:3] "Pacific/Fiji" "+12" "+13"
- attr(*, "balanced")= logi TRUE
> # Kiribati does/did not have DST, so second abbreviation may be repeat or empty
> x5 <- strptime("2022-07-01", "%Y-%m-%d", tz ="Pacific/Kiritimati")
> x5
[1] "2022-07-01 +14"
> str(unclass(x5)) # does not have DST, hence no DST abbreviation on some platforms
List of 11
$ sec : num 0
$ min : int 0
$ hour : int 0
$ mday : int 1
$ mon : int 6
$ year : int 122
$ wday : int 5
$ yday : int 181
$ isdst : int 0
$ zone : chr "+14"
$ gmtoff: int NA
- attr(*, "tzone")= chr [1:3] "Pacific/Kiritimati" "+14" "+14"
- attr(*, "balanced")= logi TRUE
>
> ## edge of range and out of range offsets
> strptime("2022-01-01 +1400", "%Y-%m-%d %z", tz = "UTC")
[1] "2021-12-31 10:00:00 UTC"
> strptime("2022-01-01 -1400", "%Y-%m-%d %z", tz = "UTC")
[1] "2022-01-01 14:00:00 UTC"
> strptime("2022-01-01 +1500", "%Y-%m-%d %z", tz = "UTC")
Warning in strptime("2022-01-01 +1500", "%Y-%m-%d %z", tz = "UTC") :
values for %z outside +/-1400 are an error
[1] NA
> strptime("2022-01-01 -1500", "%Y-%m-%d %z", tz = "UTC")
Warning in strptime("2022-01-01 -1500", "%Y-%m-%d %z", tz = "UTC") :
values for %z outside +/-1400 are an error
[1] NA
>
>
> ## extreme values for as.Date (negative ones were wrong in R 4.2.2)
> as.Date(2^(30:33))
[1] "2941775-04-07" "5881580-07-12" "11761191-01-21" "23520412-02-10"
> as.Date(-2^(30:33))
[1] "-2937836-09-26" "-5877641-06-23" "-11757252-12-12" "-23516473-11-23"
> ## tm$year will overflow ints in less than 800 billion days from present.
> as.Date(c(7e11, 8e11, -7e11, -8e11))
[1] "1916536874-11-22" NA "-1916532935-02-09"
[4] NA
>
>
> ## handling of names
> # conversion of R objects
> x <- seq(as.Date("2022-09-01"), by = "weeks", length = 10)
> names(x) <- paste("week", 1:10)
> x
week 1 week 2 week 3 week 4 week 5 week 6
"2022-09-01" "2022-09-08" "2022-09-15" "2022-09-22" "2022-09-29" "2022-10-06"
week 7 week 8 week 9 week 10
"2022-10-13" "2022-10-20" "2022-10-27" "2022-11-03"
> (xl <- as.POSIXlt(x))
week 1 week 2 week 3 week 4
"2022-09-01 UTC" "2022-09-08 UTC" "2022-09-15 UTC" "2022-09-22 UTC"
week 5 week 6 week 7 week 8
"2022-09-29 UTC" "2022-10-06 UTC" "2022-10-13 UTC" "2022-10-20 UTC"
week 9 week 10
"2022-10-27 UTC" "2022-11-03 UTC"
> str(unclass(xl))
List of 11
$ sec : num [1:10] 0 0 0 0 0 0 0 0 0 0
$ min : int [1:10] 0 0 0 0 0 0 0 0 0 0
$ hour : int [1:10] 0 0 0 0 0 0 0 0 0 0
$ mday : int [1:10] 1 8 15 22 29 6 13 20 27 3
$ mon : int [1:10] 8 8 8 8 8 9 9 9 9 10
$ year : Named int [1:10] 122 122 122 122 122 122 122 122 122 122
..- attr(*, "names")= chr [1:10] "week 1" "week 2" "week 3" "week 4" ...
$ wday : int [1:10] 4 4 4 4 4 4 4 4 4 4
$ yday : int [1:10] 243 250 257 264 271 278 285 292 299 306
$ isdst : int [1:10] 0 0 0 0 0 0 0 0 0 0
$ zone : chr [1:10] "UTC" "UTC" "UTC" "UTC" ...
$ gmtoff: int [1:10] 0 0 0 0 0 0 0 0 0 0
- attr(*, "tzone")= chr "UTC"
- attr(*, "balanced")= logi TRUE
> xx <- as.POSIXct(x, tz = "Europe/London")
> xx
week 1 week 2 week 3
"2022-09-01 01:00:00 BST" "2022-09-08 01:00:00 BST" "2022-09-15 01:00:00 BST"
week 4 week 5 week 6
"2022-09-22 01:00:00 BST" "2022-09-29 01:00:00 BST" "2022-10-06 01:00:00 BST"
week 7 week 8 week 9
"2022-10-13 01:00:00 BST" "2022-10-20 01:00:00 BST" "2022-10-27 01:00:00 BST"
week 10
"2022-11-03 00:00:00 GMT"
> as.POSIXlt(xx)
week 1 week 2 week 3
"2022-09-01 01:00:00 BST" "2022-09-08 01:00:00 BST" "2022-09-15 01:00:00 BST"
week 4 week 5 week 6
"2022-09-22 01:00:00 BST" "2022-09-29 01:00:00 BST" "2022-10-06 01:00:00 BST"
week 7 week 8 week 9
"2022-10-13 01:00:00 BST" "2022-10-20 01:00:00 BST" "2022-10-27 01:00:00 BST"
week 10
"2022-11-03 00:00:00 GMT"
> as.Date(xl)
week 1 week 2 week 3 week 4 week 5 week 6
"2022-09-01" "2022-09-08" "2022-09-15" "2022-09-22" "2022-09-29" "2022-10-06"
week 7 week 8 week 9 week 10
"2022-10-13" "2022-10-20" "2022-10-27" "2022-11-03"
>
> # character vector -> R objects
> y <- format(x)
> as.Date(y)
week 1 week 2 week 3 week 4 week 5 week 6
"2022-09-01" "2022-09-08" "2022-09-15" "2022-09-22" "2022-09-29" "2022-10-06"
week 7 week 8 week 9 week 10
"2022-10-13" "2022-10-20" "2022-10-27" "2022-11-03"
> ## IGNORE_RDIFF_BEGIN
> as.POSIXct(y)
week 1 week 2 week 3 week 4
"2022-09-01 CEST" "2022-09-08 CEST" "2022-09-15 CEST" "2022-09-22 CEST"
week 5 week 6 week 7 week 8
"2022-09-29 CEST" "2022-10-06 CEST" "2022-10-13 CEST" "2022-10-20 CEST"
week 9 week 10
"2022-10-27 CEST" "2022-11-03 CET"
> (yy <- as.POSIXlt(y))
week 1 week 2 week 3 week 4
"2022-09-01 CEST" "2022-09-08 CEST" "2022-09-15 CEST" "2022-09-22 CEST"
week 5 week 6 week 7 week 8
"2022-09-29 CEST" "2022-10-06 CEST" "2022-10-13 CEST" "2022-10-20 CEST"
week 9 week 10
"2022-10-27 CEST" "2022-11-03 CET"
> unclass(yy)
$sec
[1] 0 0 0 0 0 0 0 0 0 0
$min
[1] 0 0 0 0 0 0 0 0 0 0
$hour
[1] 0 0 0 0 0 0 0 0 0 0
$mday
[1] 1 8 15 22 29 6 13 20 27 3
$mon
[1] 8 8 8 8 8 9 9 9 9 10
$year
week 1 week 2 week 3 week 4 week 5 week 6 week 7 week 8 week 9 week 10
122 122 122 122 122 122 122 122 122 122
$wday
[1] 4 4 4 4 4 4 4 4 4 4
$yday
[1] 243 250 257 264 271 278 285 292 299 306
$isdst
[1] 1 1 1 1 1 1 1 1 1 0
$zone
[1] "CEST" "CEST" "CEST" "CEST" "CEST" "CEST" "CEST" "CEST" "CEST" "CET"
$gmtoff
[1] NA NA NA NA NA NA NA NA NA NA
attr(,"tzone")
[1] "" "CET" "CEST"
attr(,"balanced")
[1] TRUE
>
> strptime(y, "%Y-%m-%d")
week 1 week 2 week 3 week 4
"2022-09-01 CEST" "2022-09-08 CEST" "2022-09-15 CEST" "2022-09-22 CEST"
week 5 week 6 week 7 week 8
"2022-09-29 CEST" "2022-10-06 CEST" "2022-10-13 CEST" "2022-10-20 CEST"
week 9 week 10
"2022-10-27 CEST" "2022-11-03 CET"
> strftime(y, "%Y-%m-%d")
week 1 week 2 week 3 week 4 week 5 week 6
"2022-09-01" "2022-09-08" "2022-09-15" "2022-09-22" "2022-09-29" "2022-10-06"
week 7 week 8 week 9 week 10
"2022-10-13" "2022-10-20" "2022-10-27" "2022-11-03"
> y2 <- paste(y, "10:01:02"); names(y2) <- names(y)
> fmt <- c("%Y-%m-%d", "%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M:%S %Z")
> (strptime(y2, fmt[1:2]) -> sy2)
week 1 week 2
"2022-09-01 00:00:00 CEST" "2022-09-08 10:01:02 CEST"
week 3 week 4
"2022-09-15 00:00:00 CEST" "2022-09-22 10:01:02 CEST"
week 5 week 6
"2022-09-29 00:00:00 CEST" "2022-10-06 10:01:02 CEST"
week 7 week 8
"2022-10-13 00:00:00 CEST" "2022-10-20 10:01:02 CEST"
week 9 week 10
"2022-10-27 00:00:00 CEST" "2022-11-03 10:01:02 CET"
> ## IGNORE_RDIFF_END
> sy2.15 <- strptime(y2, rep(fmt[1:2], length = 15)) # failed to recycle names
> stopifnot(suppressWarnings(sy2 == sy2.15))
>
> xl. <- xl[1:9] # length(fmt) == 3 -- fully recycles in xl.
> (strftime(xl., fmt) -> sx)
week 1 week 2 week 3
"2022-09-01" "2022-09-08 00:00:00" "2022-09-15 00:00:00 UTC"
week 4 week 5 week 6
"2022-09-22" "2022-09-29 00:00:00" "2022-10-06 00:00:00 UTC"
week 7 week 8 week 9
"2022-10-13" "2022-10-20 00:00:00" "2022-10-27 00:00:00 UTC"
> (strftime(xl., rep(fmt, length = 15)) -> sx15)
week 1 week 2 week 3
"2022-09-01" "2022-09-08 00:00:00" "2022-09-15 00:00:00 UTC"
week 4 week 5 week 6
"2022-09-22" "2022-09-29 00:00:00" "2022-10-06 00:00:00 UTC"
week 7 week 8 week 9
"2022-10-13" "2022-10-20 00:00:00" "2022-10-27 00:00:00 UTC"
week 1 week 2 week 3
"2022-09-01" "2022-09-08 00:00:00" "2022-09-15 00:00:00 UTC"
week 4 week 5 week 6
"2022-09-22" "2022-09-29 00:00:00" "2022-10-06 00:00:00 UTC"
> stopifnot(exprs = { # with warnings ".. length is not a multiple of shorter .."
+ sx == sx15
+ names(sx) == names(sx15)
+ })
Warning in sx == sx15 :
longer object length is not a multiple of shorter object length
Warning in names(sx) == names(sx15) :
longer object length is not a multiple of shorter object length
>
> x2 <- xl[1:5]
> x2$year <- xl$year[1:3]
> x2 # correctly has missing names as NA
week 1 week 2 week 3 <NA>
"2022-09-01 UTC" "2022-09-08 UTC" "2022-09-15 UTC" "2022-09-22 UTC"
<NA>
"2022-09-29 UTC"
> balancePOSIXlt(x2) # recycles names
week 1 week 2 week 3 week 1
"2022-09-01 UTC" "2022-09-08 UTC" "2022-09-15 UTC" "2022-09-22 UTC"
week 2
"2022-09-29 UTC"
> strftime(x2, fmt)
week 1 week 2 week 3
"2022-09-01" "2022-09-08 00:00:00" "2022-09-15 00:00:00 UTC"
<NA> <NA>
"2022-09-22" "2022-09-29 00:00:00"
> strftime(x2, rep(fmt, length = 10))
week 1 week 2 week 3
"2022-09-01" "2022-09-08 00:00:00" "2022-09-15 00:00:00 UTC"
<NA> <NA> week 1
"2022-09-22" "2022-09-29 00:00:00" "2022-09-01 00:00:00 UTC"
week 2 week 3 <NA>
"2022-09-08" "2022-09-15 00:00:00" "2022-09-22 00:00:00 UTC"
<NA>
"2022-09-29"
>
|