File: test-python-datetime.R

package info (click to toggle)
r-cran-reticulate 1.41.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,088 kB
  • sloc: cpp: 5,154; python: 620; sh: 13; makefile: 2
file content (96 lines) | stat: -rw-r--r-- 2,291 bytes parent folder | download
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
context("datetime")

test_that("R dates can be converted to / from Python datetimes", {
  skip_if_no_python()

  before <- list(Sys.Date(), as.Date("2019-04-06"))
  after  <- py_to_r(r_to_py(before))

  expect_equal(before, after)
})

test_that("R times can be converted to / from Python datetimes", {
  skip_if_no_numpy()

  before <- Sys.time()
  attr(before, "tzone") <- "UTC"
  after <- py_to_r(r_to_py(before))

  expect_equal(as.numeric(before), as.numeric(after))
})

test_that("lists of times are converted", {
  skip_if_no_python()

  dates <- replicate(3, Sys.Date(), simplify = FALSE)
  expect_equal(
    py_to_r(r_to_py(dates)),
    dates
  )

})

test_that("R times are converted to NumPy datetime64", {
  skip_if_no_numpy()

  np <- import("numpy", convert = TRUE)

  before <- rep(Sys.time(), 3)
  converted <- r_to_py(before)
  expect_true(np$issubdtype(converted$dtype, np$datetime64))

  after <- py_to_r(converted)
  expect_equal(
    as.numeric(as.POSIXct(before)),
    as.numeric(as.POSIXct(after))
  )

})

test_that("R datetimes can be passed to Python functions", {
  skip_if_no_python()
  py_run_string("def identity(x): return x")
  main <- import_main()
  date <- Sys.Date()
  expect_equal(date, main$identity(Sys.Date()))
})

test_that("timezone information is not lost during conversion", {

  skip_if_no_python()
  skip_if(py_version() < "3")

  datetime <- import("datetime", convert = FALSE)
  if (!py_has_attr(datetime, "timezone"))
    skip("datetime.timezone is not available")

  pdt <- datetime$datetime(
    year   = 2020L,
    month  = 8L,
    day    = 24L,
    hour   = 3L,
    minute = 4L,
    second = 5L,
    tzinfo = datetime$timezone$utc
  )

  rdt <- py_to_r(pdt)

  tzone <- attr(rdt, "tzone", exact = TRUE)
  expect_identical(tzone, "UTC")

  expect_identical(
    format(rdt, "%Y-%m-%dT%H:%M:%S%z"),
    "2020-08-24T03:04:05+0000")
  # py_to_r(pdt$isoformat(timespec="seconds")))

  if (py_version() >= "3.9") {
    zoneinfo <- import("zoneinfo")
    pdt <- datetime$datetime$now(zoneinfo$ZoneInfo("America/New_York"))
    rdt <- py_to_r(pdt)
    expect_identical(attr(rdt, "tzone", TRUE), "America/New_York")
    expect_identical(format(rdt, "%Y-%m-%dT%H:%M:%S%z"),
                     py_to_r(pdt$strftime("%Y-%m-%dT%H:%M:%S%z")))
  }

})