From 1b82f153e67fd21f0a19193e1ade6fb061047eed Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Fri, 15 Mar 2024 13:10:12 +0100 Subject: [PATCH] Remove `utctime` ref #2 --- R/cpp11.R | 4 ---- R/parser.R | 7 ------ src/cpp11.cpp | 8 ------- src/datetime.cpp | 33 -------------------------- tests/testthat/test-parsing-datetime.R | 25 ++++--------------- 5 files changed, 5 insertions(+), 72 deletions(-) delete mode 100644 src/datetime.cpp diff --git a/R/cpp11.R b/R/cpp11.R index 0d351a4..2294a21 100644 --- a/R/cpp11.R +++ b/R/cpp11.R @@ -4,10 +4,6 @@ collectorGuess <- function(input, locale_, guessInteger) { .Call(`_minty_collectorGuess`, input, locale_, guessInteger) } -utctime_ <- function(year, month, day, hour, min, sec, psec) { - .Call(`_minty_utctime_`, year, month, day, hour, min, sec, psec) -} - parse_vector_ <- function(x, collectorSpec, locale_, na, trim_ws) { .Call(`_minty_parse_vector_`, x, collectorSpec, locale_, na, trim_ws) } diff --git a/R/parser.R b/R/parser.R index c0b4138..7aa29f7 100644 --- a/R/parser.R +++ b/R/parser.R @@ -1272,13 +1272,6 @@ is_named <- function(x) { all(nms != "" & !is.na(nms)) } -utctime <- function(year, month, day, hour, min, sec, psec) { - utctime_( - as.integer(year), as.integer(month), as.integer(day), - as.integer(hour), as.integer(min), as.integer(sec), as.numeric(psec) - ) -} - POSIXct <- function(x, tz = "UTC") { structure(x, class = c("POSIXct", "POSIXt"), tzone = tz) } diff --git a/src/cpp11.cpp b/src/cpp11.cpp index fffaad4..d033f9f 100644 --- a/src/cpp11.cpp +++ b/src/cpp11.cpp @@ -12,13 +12,6 @@ extern "C" SEXP _minty_collectorGuess(SEXP input, SEXP locale_, SEXP guessIntege return cpp11::as_sexp(collectorGuess(cpp11::as_cpp>(input), cpp11::as_cpp>(locale_), cpp11::as_cpp>(guessInteger))); END_CPP11 } -// datetime.cpp -cpp11::writable::doubles utctime_(const cpp11::integers& year, const cpp11::integers& month, const cpp11::integers& day, const cpp11::integers& hour, const cpp11::integers& min, const cpp11::integers& sec, const cpp11::doubles& psec); -extern "C" SEXP _minty_utctime_(SEXP year, SEXP month, SEXP day, SEXP hour, SEXP min, SEXP sec, SEXP psec) { - BEGIN_CPP11 - return cpp11::as_sexp(utctime_(cpp11::as_cpp>(year), cpp11::as_cpp>(month), cpp11::as_cpp>(day), cpp11::as_cpp>(hour), cpp11::as_cpp>(min), cpp11::as_cpp>(sec), cpp11::as_cpp>(psec))); - END_CPP11 -} // parse.cpp SEXP parse_vector_(const cpp11::strings& x, const cpp11::list& collectorSpec, const cpp11::list& locale_, const std::vector& na, bool trim_ws); extern "C" SEXP _minty_parse_vector_(SEXP x, SEXP collectorSpec, SEXP locale_, SEXP na, SEXP trim_ws) { @@ -39,7 +32,6 @@ static const R_CallMethodDef CallEntries[] = { {"_minty_collectorGuess", (DL_FUNC) &_minty_collectorGuess, 3}, {"_minty_parse_vector_", (DL_FUNC) &_minty_parse_vector_, 5}, {"_minty_type_convert_col", (DL_FUNC) &_minty_type_convert_col, 6}, - {"_minty_utctime_", (DL_FUNC) &_minty_utctime_, 7}, {NULL, NULL, 0} }; } diff --git a/src/datetime.cpp b/src/datetime.cpp deleted file mode 100644 index f661109..0000000 --- a/src/datetime.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "cpp11/doubles.hpp" -#include "cpp11/integers.hpp" -#include "cpp11/protect.hpp" - -#include "DateTime.h" - -[[cpp11::register]] cpp11::writable::doubles utctime_( - const cpp11::integers& year, - const cpp11::integers& month, - const cpp11::integers& day, - const cpp11::integers& hour, - const cpp11::integers& min, - const cpp11::integers& sec, - const cpp11::doubles& psec) { - int n = year.size(); - if (month.size() != n || day.size() != n || hour.size() != n || - min.size() != n || sec.size() != n || psec.size() != n) { - cpp11::stop("All inputs must be same length"); - } - - cpp11::writable::doubles out(n); - - for (int i = 0; i < n; ++i) { - DateTime dt( - year[i], month[i], day[i], hour[i], min[i], sec[i], psec[i], "UTC"); - out[i] = dt.datetime(); - } - - out.attr("class") = {"POSIXct", "POSIXt"}; - out.attr("tzone") = "UTC"; - - return out; -} diff --git a/tests/testthat/test-parsing-datetime.R b/tests/testthat/test-parsing-datetime.R index 136bb55..012beef 100644 --- a/tests/testthat/test-parsing-datetime.R +++ b/tests/testthat/test-parsing-datetime.R @@ -1,23 +1,9 @@ -test_that("utctime is equivalent to R conversion", { - year <- seq(0, 4000) - mon <- rep(3L, length(year)) - day <- rep(1L, length(year)) - zero_i <- rep(0L, length(year)) - zero_d <- rep(0, length(year)) - - expect_equal( - utctime(year, mon, day, zero_i, zero_i, zero_i, zero_d), - ISOdatetime(year, mon, day, zero_i, zero_i, zero_i, tz = "UTC") - ) -}) - # Parsing ---------------------------------------------------------------------- r_parse <- function(x, fmt) as.POSIXct(strptime(x, fmt, tz = "UTC")) test_that("%d, %m and %y", { - target <- utctime(2010L, 2L, 3L, 0L, 0L, 0L, 0) - + target <- as.POSIXct("2010-02-03", tz = "UTC") expect_equal(parse_datetime("10-02-03", "%y-%m-%d"), target) expect_equal(parse_datetime("10-03-02", "%y-%d-%m"), target) expect_equal(parse_datetime("03/02/10", "%d/%m/%y"), target) @@ -25,8 +11,7 @@ test_that("%d, %m and %y", { }) test_that("Compound formats work", { - target <- utctime(2010L, 2L, 3L, 0L, 0L, 0L, 0) - + target <- as.POSIXct("2010-02-03", tz = "UTC") expect_equal(parse_datetime("02/03/10", "%D"), target) expect_equal(parse_datetime("2010-02-03", "%F"), target) expect_equal(parse_datetime("10/02/03", "%x"), target) @@ -44,7 +29,7 @@ test_that("%y matches R behaviour", { }) test_that("%e allows leading space", { - expect_equal(parse_datetime("201010 1", "%Y%m%e"), utctime(2010L, 10L, 1L, 0L, 0L, 0L, 0)) + expect_equal(parse_datetime("201010 1", "%Y%m%e"), as.POSIXct("2010-10-1", tz = "UTC")) }) test_that("%OS captures partial seconds", { @@ -221,8 +206,8 @@ test_that("same times with different offsets parsed as same time", { # From http://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC same_time <- paste("2010-02-03", c("18:30Z", "22:30+04", "1130-0700", "15:00-03:30")) parsed <- parse_datetime(same_time) - - expect_equal(parsed, rep(utctime(2010L, 2L, 3L, 18L, 30L, 0L, 0), 4)) + target <- as.POSIXct("2010-02-03 18:30:00", tz = "UTC") + expect_equal(parsed, rep(target, 4)) }) test_that("offsets can cross date boundaries", {