R Under development (unstable) (2024-12-05 r87423) -- "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. > ### tests of strftime (formatting POSIXlt objects via format.POSIXlt) > > Sys.setenv(TZ = "Europe/Rome") > > dt <- as.POSIXlt("2022-12-11 09:03;04") > > ff <- c(LETTERS, letters) > ff <- setdiff(c(LETTERS, letters), + c("E", "J", "K", "L", "N", "P", "O", "Q", + "f", "i", "k", "l", "o", "q", + "r", # %r is locale- and implementation-dependent. + "s", "v") + ) > > for (f in ff) { + f <- paste0("%", f) + cat(sprintf("%s: %s\n", f, format(dt, f))) + } %A: Sunday %B: December %C: 20 %D: 12/11/22 %F: 2022-12-11 %G: 2022 %H: 09 %I: 09 %M: 03 %R: 09:03 %S: 00 %T: 09:03:00 %U: 50 %V: 49 %W: 49 %X: 09:03:00 %Y: 2022 %Z: CET %a: Sun %b: Dec %c: Sun Dec 11 09:03:00 2022 %d: 11 %e: 11 %g: 22 %h: Dec %j: 345 %m: 12 %n: %p: AM %t: %u: 7 %w: 0 %x: 12/11/22 %y: 22 %z: +0100 > > ## 'not in the standards and less widely implemented' > ## %P is a glibc extension which we added to IANA tzcode for R. Not in macOS. > for (f in c("P", "k", "l", "s")) { + f <- paste0("%", f) + cat(sprintf("%s: %s\n", f, try(format(dt, f), silent = TRUE))) + } %P: am %k: 9 %l: 9 %s: 1670745780 > > ## week numbers > dt2 <- as.POSIXlt(sprintf("%d-01-01 09:03;04", 2015:2018)) > cat(format(dt2, "%Y: %U %V %W"), sep = "\n") 2015: 00 01 00 2016: 00 53 00 2017: 01 52 00 2018: 00 01 01 > > ## recycling *both* {x, format} "heavily"; digits = must influence %OS; PR#17350 > (fmt <- c("", paste0("%H:%M:%OS", c("", 2), " in %Y"), # || nasty (but "correct") + paste0("%Y-%m-%d", c("", paste0(" %H:%M:%OS", c("", 0, 1, 6, 9, 11)))))) [1] "" "%H:%M:%OS in %Y" "%H:%M:%OS2 in %Y" [4] "%Y-%m-%d" "%Y-%m-%d %H:%M:%OS" "%Y-%m-%d %H:%M:%OS0" [7] "%Y-%m-%d %H:%M:%OS1" "%Y-%m-%d %H:%M:%OS6" "%Y-%m-%d %H:%M:%OS9" [10] "%Y-%m-%d %H:%M:%OS11" > weekD <- seq(as.Date("2020-04-01"), by = "weeks", length.out = 5 * length(fmt)) ; joff <- (0:4)*length(fmt) > weekPlt <- as.POSIXlt(weekD, tz = "UTC") > week.25 <- weekPlt + 0.25 > (Lf1 <- split(f1 <- format(weekPlt, format = fmt), fmt)) [[1]] [1] "2020-04-01" "2020-06-10" "2020-08-19" "2020-10-28" "2021-01-06" $`%H:%M:%OS in %Y` [1] "00:00:00 in 2020" "00:00:00 in 2020" "00:00:00 in 2020" "00:00:00 in 2020" [5] "00:00:00 in 2021" $`%H:%M:%OS2 in %Y` [1] "00:00:00.00 in 2020" "00:00:00.00 in 2020" "00:00:00.00 in 2020" [4] "00:00:00.00 in 2020" "00:00:00.00 in 2021" $`%Y-%m-%d` [1] "2020-04-22" "2020-07-01" "2020-09-09" "2020-11-18" "2021-01-27" $`%Y-%m-%d %H:%M:%OS` [1] "2020-04-29 00:00:00" "2020-07-08 00:00:00" "2020-09-16 00:00:00" [4] "2020-11-25 00:00:00" "2021-02-03 00:00:00" $`%Y-%m-%d %H:%M:%OS0` [1] "2020-05-06 00:00:00" "2020-07-15 00:00:00" "2020-09-23 00:00:00" [4] "2020-12-02 00:00:00" "2021-02-10 00:00:00" $`%Y-%m-%d %H:%M:%OS1` [1] "2020-05-13 00:00:00.0" "2020-07-22 00:00:00.0" "2020-09-30 00:00:00.0" [4] "2020-12-09 00:00:00.0" "2021-02-17 00:00:00.0" $`%Y-%m-%d %H:%M:%OS11` [1] "2020-06-03 00:00:00.01" "2020-08-12 00:00:00.01" "2020-10-21 00:00:00.01" [4] "2020-12-30 00:00:00.01" "2021-03-10 00:00:00.01" $`%Y-%m-%d %H:%M:%OS6` [1] "2020-05-20 00:00:00.000000" "2020-07-29 00:00:00.000000" [3] "2020-10-07 00:00:00.000000" "2020-12-16 00:00:00.000000" [5] "2021-02-24 00:00:00.000000" $`%Y-%m-%d %H:%M:%OS9` [1] "2020-05-27 00:00:00.000000" "2020-08-05 00:00:00.000000" [3] "2020-10-14 00:00:00.000000" "2020-12-23 00:00:00.000000" [5] "2021-03-03 00:00:00.000000" > (Lf. <- split(f. <- format(week.25, format = fmt), fmt)) [[1]] [1] "2020-04-01 00:00:00" "2020-06-10 00:00:00" "2020-08-19 00:00:00" [4] "2020-10-28 00:00:00" "2021-01-06 00:00:00" $`%H:%M:%OS in %Y` [1] "00:00:00 in 2020" "00:00:00 in 2020" "00:00:00 in 2020" "00:00:00 in 2020" [5] "00:00:00 in 2021" $`%H:%M:%OS2 in %Y` [1] "00:00:00.25 in 2020" "00:00:00.25 in 2020" "00:00:00.25 in 2020" [4] "00:00:00.25 in 2020" "00:00:00.25 in 2021" $`%Y-%m-%d` [1] "2020-04-22" "2020-07-01" "2020-09-09" "2020-11-18" "2021-01-27" $`%Y-%m-%d %H:%M:%OS` [1] "2020-04-29 00:00:00" "2020-07-08 00:00:00" "2020-09-16 00:00:00" [4] "2020-11-25 00:00:00" "2021-02-03 00:00:00" $`%Y-%m-%d %H:%M:%OS0` [1] "2020-05-06 00:00:00" "2020-07-15 00:00:00" "2020-09-23 00:00:00" [4] "2020-12-02 00:00:00" "2021-02-10 00:00:00" $`%Y-%m-%d %H:%M:%OS1` [1] "2020-05-13 00:00:00.2" "2020-07-22 00:00:00.2" "2020-09-30 00:00:00.2" [4] "2020-12-09 00:00:00.2" "2021-02-17 00:00:00.2" $`%Y-%m-%d %H:%M:%OS11` [1] "2020-06-03 00:00:00.21" "2020-08-12 00:00:00.21" "2020-10-21 00:00:00.21" [4] "2020-12-30 00:00:00.21" "2021-03-10 00:00:00.21" $`%Y-%m-%d %H:%M:%OS6` [1] "2020-05-20 00:00:00.250000" "2020-07-29 00:00:00.250000" [3] "2020-10-07 00:00:00.250000" "2020-12-16 00:00:00.250000" [5] "2021-02-24 00:00:00.250000" $`%Y-%m-%d %H:%M:%OS9` [1] "2020-05-27 00:00:00.250000" "2020-08-05 00:00:00.250000" [3] "2020-10-14 00:00:00.250000" "2020-12-23 00:00:00.250000" [5] "2021-03-03 00:00:00.250000" > (Lf3 <- split(f3 <- format(week.25, format = fmt, digits = 3), fmt)) [[1]] [1] "2020-04-01 00:00:00.25" "2020-06-10 00:00:00.25" "2020-08-19 00:00:00.25" [4] "2020-10-28 00:00:00.25" "2021-01-06 00:00:00.25" $`%H:%M:%OS in %Y` [1] "00:00:00.25 in 2020" "00:00:00.25 in 2020" "00:00:00.25 in 2020" [4] "00:00:00.25 in 2020" "00:00:00.25 in 2021" $`%H:%M:%OS2 in %Y` [1] "00:00:00.25 in 2020" "00:00:00.25 in 2020" "00:00:00.25 in 2020" [4] "00:00:00.25 in 2020" "00:00:00.25 in 2021" $`%Y-%m-%d` [1] "2020-04-22" "2020-07-01" "2020-09-09" "2020-11-18" "2021-01-27" $`%Y-%m-%d %H:%M:%OS` [1] "2020-04-29 00:00:00.25" "2020-07-08 00:00:00.25" "2020-09-16 00:00:00.25" [4] "2020-11-25 00:00:00.25" "2021-02-03 00:00:00.25" $`%Y-%m-%d %H:%M:%OS0` [1] "2020-05-06 00:00:00" "2020-07-15 00:00:00" "2020-09-23 00:00:00" [4] "2020-12-02 00:00:00" "2021-02-10 00:00:00" $`%Y-%m-%d %H:%M:%OS1` [1] "2020-05-13 00:00:00.2" "2020-07-22 00:00:00.2" "2020-09-30 00:00:00.2" [4] "2020-12-09 00:00:00.2" "2021-02-17 00:00:00.2" $`%Y-%m-%d %H:%M:%OS11` [1] "2020-06-03 00:00:00.21" "2020-08-12 00:00:00.21" "2020-10-21 00:00:00.21" [4] "2020-12-30 00:00:00.21" "2021-03-10 00:00:00.21" $`%Y-%m-%d %H:%M:%OS6` [1] "2020-05-20 00:00:00.250000" "2020-07-29 00:00:00.250000" [3] "2020-10-07 00:00:00.250000" "2020-12-16 00:00:00.250000" [5] "2021-02-24 00:00:00.250000" $`%Y-%m-%d %H:%M:%OS9` [1] "2020-05-27 00:00:00.250000" "2020-08-05 00:00:00.250000" [3] "2020-10-14 00:00:00.250000" "2020-12-23 00:00:00.250000" [5] "2021-03-03 00:00:00.250000" > stopifnot(exprs = { + f3[2L+joff] == f3[3L+joff] + grepl("^00:00:00.25 in 202[01]", f3[2L+joff]) + grepl("00:00:00.25$", f3[5L+joff]) # (was wrong shortly) + ## '%OS' overrules 'digits' {was true forever} + format(week.25, format = "%OS0", digits = 3) == "00" + format(week.25, format = "%OS1", digits = 3) == "00.2" + }) > ## digits = 3 had no effect on "%OS " in R <= 4.4 > ## NB: 2nd %OS _never_ worked (internal C code) -- TODO: fix or doc > unique(fOO_ <- format(week.25, "%OS|%OS3|")) [1] "00|003|" > unique(fOO_1 <- format(week.25, "%OS3|%OS1|%OS3|")) [1] "00.250|001|003|" > >