Datetime formatting in R3.0.3 and R3.1.3
Asked Answered
G

1

12

I have a package on CRAN called UNF, which creates a hash of a data.frame (for use in data citation). I have some tests in the package related to the formatting of datetimes (I'm using testthat). They work correctly on the current version of R (3.1.3), but once I submitted to CRAN, one of these tests fail on "r-oldrel-windows" (3.0.3).

I've tracked down the difference to the following code, which yields different results in the two versions of R. Here's the correct output (from 3.1.3):

x = strptime("2014-08-22T16:51:05Z", "%FT%H:%M:%OSZ", tz="UTC")
x
# [1] "2014-08-22 16:51:05 UTC"
strftime(x, "%F")
# [1] "2014-08-22"

And here's the output from 3.0.3:

x = strptime("2014-08-22T16:51:05Z", "%FT%H:%M:%OSZ", tz="UTC")
x
# [1] "2014-08-22 16:51:05 UTC"
strftime(x, "%F")
# [1] ""

As you can see, the output of strftime is an empty character string rather than an ISO 8601 formatted date. Any idea what the change was between these two versions? And how can I correct this? Or, how can I avoid having the tests fail on CRAN?

Gomel answered 24/3, 2015 at 10:23 Comment(0)
W
2

It is possible that %F was not an option in earlier versions of R. So the base code is ignoring the string so it formats as empty string. I tried using a letter not in the current help and it returns a string with that letter not a date.

Thomas, there is a skip() function in testthat and skip_on_CRAN functionality.

  1. Take a look at help

    ?testthat::skip_on_cran
    
  2. wbeasley has some test code which may assist you. (See his helpful comments in this answer to Rappster in 25595487). You will see how he puts this skip command inside the testthat functions brackets. Paraphrasing it below:

    library(testthat)        
    
    testthat("example"),{
      testthat::skip_on_cran()
      # test code below
      x <-2
      expect_equal(x,2)
    })
    
  3. It might be an OS thing with Windows. Doing some more digging revealed this - Take a look at this describing R 3.0.2. http://www.inside-r.org/r-doc/base/strftime

    The docs warn of some issues with some % flags in Windows. Quoting (my bold):

Also defined in the current standards but less widely implemented (e.g. not for output on Windows) are

%C Century (00--99): the integer part of the year divided by 100.

...

%F Equivalent to %Y-%m-%d (the ISO 8601 date format).

...

Hope this helps!

Waggish answered 25/3, 2015 at 11:7 Comment(1)
Yup, that was it. %F was not previously supported as an output format on Windows. Thanks!Gomel

© 2022 - 2024 — McMap. All rights reserved.