File: datetime2.R

package info (click to toggle)
r-base 3.1.1-1%2Bdeb8u1
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 85,436 kB
  • ctags: 35,389
  • sloc: ansic: 306,779; fortran: 91,908; sh: 11,216; makefile: 5,311; yacc: 4,994; tcl: 4,562; objc: 746; perl: 655; asm: 553; java: 31; sed: 6
file content (47 lines) | stat: -rw-r--r-- 1,773 bytes parent folder | download | duplicates (6)
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
### Tests of often platform-dependent features of the POSIX[cl]t implementation.

### Expect differences, especially with 32-bit time_t

z <- ISOdate(1890:1912, 1, 10, tz="UTC")
## Rome changed to CET for 1894
as.POSIXlt(z, tz="Europe/Rome")
## Paris changed to PMT for 1892, WET for 1912
(zz <- as.POSIXlt(z, tz="Europe/Paris"))
strftime(zz, "%Y-%m-%d %H:%M:%S %Z")
## The offset was really 00:09:21 until 1911, then 00:00
## Many platforms will give the current offset, +0100
strftime(zz, "%Y-%m-%d %H:%M:%S %z")

## Some platforms give details of the latest conversion.
z <- ISOdate(c(seq(1890, 1940, 5), 1941:1946, 1950), 1, 10, tz="UTC")
as.POSIXlt(z, tz="Europe/Paris")
for(i in seq_along(z)) print(as.POSIXlt(z[i], tz="Europe/Paris"))
for(i in seq_along(z))
    print(strftime(as.POSIXlt(z[i], tz="Europe/Paris"), "%Y-%m-%d %H:%M:%S %z"))

strptime("1920-12-27 08:18:23", "%Y-%m-%d %H:%M:%S", tz="Europe/Paris")

## check %V etc

d <- expand.grid(day = 1:7, year = 2000:2010)
z1 <- with(d, ISOdate(year, 1, day))
d <- expand.grid(day = 25:31, year = 2000:2010)
z2 <- with(d, ISOdate(year, 12, day))
z <- sort(c(z1, z2))
strftime(z, "%G %g %W %U %u %V %W %w")

## tests of earlier years.  Default format is OS-dependent, so don't test it.
## ISOdate only accepts positive years.
z <- as.Date(ISOdate(c(0, 8, 9, 10, 11, 20, 110, 1010), 1, 10)) - 3630
strftime(z, "%04Y-%m-%d") # with leading zero(s)
strftime(z, "%_4Y-%m-%d") # with leading space(s)
strftime(z, "%0Y-%m-%d") # without


## more test of strftime
x <- ISOdate(2014, 3, 10, c(7, 13))
fmts <- c("%Y-%m-%d %H:%M:%S", "%F", "%A %a %b %h %e %I %j",
          ## locale-dependent ones
          "%X", # but the same in all English locales
          "%c", "%x", "%p", "%r")
for (f in fmts) print(format(x, f))