Skip to content

Commit

Permalink
Use POSIXct instead of POSIXlt for dates
Browse files Browse the repository at this point in the history
This was the advice I got back from the `{vctrs}` team.
  • Loading branch information
Moohan committed Apr 25, 2024
1 parent 24d7eee commit c933624
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 23 deletions.
4 changes: 2 additions & 2 deletions R/add_context.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ add_context <- function(data, id, name, created_date, modified_date) {
}

# Parse the date values
created_date <- strptime(created_date, format = "%FT%X", tz = "UTC")
modified_date <- strptime(modified_date, format = "%FT%X", tz = "UTC")
created_date <- as.POSIXct(created_date, format = "%FT%X", tz = "UTC")
modified_date <- as.POSIXct(modified_date, format = "%FT%X", tz = "UTC")

# The platform can record the modified date as being before the created date
# by a few microseconds, this will catch any rounding which ensure
Expand Down
19 changes: 0 additions & 19 deletions tests/testthat/test-add_context.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,6 @@ test_that("Returned context is the same for resource and dataset", {
include_context = TRUE
)

# --- Remove from here
# This code works around an issue with vctrs
# https://github.com/r-lib/vctrs/issues/1930
dataset_has_POSIXlt <- inherits(dataset$res_created_date, "POSIXlt")

# If this test fails, that's good (probably) and this can all be removed
expect_error(stopifnot(dataset_has_POSIXlt))

if (!dataset_has_POSIXlt) {
dataset <- dataset %>%
dplyr::mutate(
dplyr::across(
c("res_created_date", "res_modified_date"),
as.POSIXlt
)
)
}
# --- Remove to here

expect_equal(
dataset %>%
dplyr::filter(res_id == res_id_1) %>%
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-get_dataset_context.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ test_that("returns expected context with the data", {
)

expect_s3_class(data, "tbl_df")
expect_type(data$res_id, "character")
expect_type(data$res_name, "character")
expect_s3_class(data$res_created_date, "POSIXct")
expect_s3_class(data$res_modified_date, "POSIXct")

expect_equal(nrow(data), n_resources * n_rows)
expect_length(data, 28)
expect_named(
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-get_resource_context.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ test_that("returns expected context with the data", {
expect_s3_class(data, "tbl_df")
expect_type(data$res_id, "character")
expect_type(data$res_name, "character")
expect_s3_class(data$res_created_date, "POSIXlt")
expect_s3_class(data$res_modified_date, "POSIXlt")
expect_s3_class(data$res_created_date, "POSIXct")
expect_s3_class(data$res_modified_date, "POSIXct")

expect_length(data, 19)
expect_equal(nrow(data), 10)
Expand Down

0 comments on commit c933624

Please sign in to comment.