File: datetime5.R

package info (click to toggle)
r-base 4.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 112,924 kB
  • sloc: ansic: 291,338; fortran: 111,889; javascript: 14,798; yacc: 6,154; sh: 5,689; makefile: 5,239; tcl: 4,562; perl: 963; objc: 791; f90: 758; asm: 258; java: 31; sed: 1
file content (52 lines) | stat: -rw-r--r-- 2,098 bytes parent folder | download | duplicates (2)
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
### 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)))
}

## '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)))
}

## week numbers
dt2 <- as.POSIXlt(sprintf("%d-01-01 09:03;04", 2015:2018))
cat(format(dt2, "%Y: %U %V %W"), sep = "\n")

## recycling *both* {x, format} "heavily"; digits = <n> must influence %OS<empty>; 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))))))
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))
(Lf. <- split(f. <- format(week.25, format = fmt),             fmt))
(Lf3 <- split(f3 <- format(week.25, format = fmt, digits = 3), fmt))
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<n>' 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|"))
unique(fOO_1 <- format(week.25, "%OS3|%OS1|%OS3|"))