Skip to content

Commit

Permalink
replace as.character.POSIX & co with format.POSIX & co for compat wit…
Browse files Browse the repository at this point in the history
…h new R version
  • Loading branch information
stefan-fleck committed Oct 11, 2022
1 parent 41cd049 commit db42e9f
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: rotor
Title: Log Rotation and Conditional Backups
Version: 0.3.6
Version: 0.3.7
Authors@R:
person(given = "Stefan",
family = "Fleck",
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# rotor 0.3.7

* Some internal changes to date formatting for compatibility with
R-devel revision >= r82904 (2022-09-24 19:32:52).


# rotor 0.3.6

* `rotate()`, `backup()` and co. no longer fail on filenames that
Expand Down
10 changes: 5 additions & 5 deletions R/BackupQueue.R
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ BackupQueueDateTime <- R6::R6Class(
self$files$path,
self$files$timestamp,
max_age = max_backups,
now = as.Date(as.character(self$last_rotation))
now = as.Date(format(self$last_rotation))
)
}

Expand Down Expand Up @@ -813,7 +813,7 @@ BackupQueueDate <- R6::R6Class(
if (nrow(bus) < 1) {
NULL
} else {
as.Date(as.character(max(get("files", envir = self)$timestamp)))
as.Date(format(max(get("files", envir = self)$timestamp)))
}
}
)
Expand Down Expand Up @@ -933,7 +933,7 @@ select_prune_files_by_age <- function(

if (is_parsable_date(max_age)){
limit <- parse_date(max_age)
to_remove <- path[as.Date(as.character(timestamp)) < limit]
to_remove <- path[as.Date(format(timestamp)) < limit]

} else if (is_parsable_datetime(max_age)){
limit <- parse_datetime(max_age)
Expand All @@ -956,10 +956,10 @@ select_prune_files_by_age <- function(
limit <- dint::first_of_isoweek(dint::as_date_yw(now) - max_age$value + 1L)

} else if (identical(max_age[["unit"]], "day")){
limit <- as.Date(as.character(now)) - max_age$value + 1L
limit <- as.Date(format(now)) - max_age$value + 1L
}

to_remove <- path[as.Date(as.character(timestamp)) < limit]
to_remove <- path[as.Date(format(timestamp)) < limit]
} else {
stop(ValueError(paste0(preview_object(max_age), " is not a valid timestamp or interval. See ?rotate_time for more info.")))
}
Expand Down
2 changes: 1 addition & 1 deletion R/Cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Cache <- R6::R6Class(
assert(is.null(max_files) || is.infinite(max_files) || is_n0(max_files))
files <- self$files
files <- files[order(files$mtime), ]
now <- as.Date(as.character(now))
now <- as.Date(format(now))

rem <- list()

Expand Down
8 changes: 4 additions & 4 deletions R/parsers.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ parse_date <- function(x){
return(x)

} else if (is_POSIXct(x)){
return(as.Date(as.character(x)))
return(as.Date(format(x)))

} else if (!is.character(x) && !is_integerish(x)) {
stop(
Expand All @@ -100,7 +100,7 @@ parse_datetime <- function(x){
if (is_POSIXct(x)){
return(x)
} else if (is_Date(x)) {
return(as.POSIXct(as.character(x)))
return(as.POSIXct(format(x)))
} else if (!is.character(x) && !is_integerish(x)) {
stop(
"`", deparse(substitute(x)), "` must be a character, Date, or POSIXt, ",
Expand Down Expand Up @@ -145,14 +145,14 @@ prep_hms <- function(.x){


standardize_datetime_stamp <- function(x){
gsub("T|-|_|\\s", "", as.character(x))
gsub("T|-|_|\\s", "", format(x))
}




standardize_date_stamp <- function(x){
gsub("-|_|\\s", "", as.character(x))
gsub("-|_|\\s", "", format(x))
}


Expand Down
6 changes: 3 additions & 3 deletions R/utils-predicates.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ is_backup_older_than_datetime <- function(
verbose = FALSE
){
if (is_Date(backup_date))
backup_date <- as.POSIXct(as.character(backup_date))
backup_date <- as.POSIXct(format(backup_date))

assert(is_scalar_POSIXct(backup_date))
assert(is_parsable_datetime(datetime))
Expand Down Expand Up @@ -212,10 +212,10 @@ is_backup_older_than_interval <- function(
verbose = FALSE
){
if (is_POSIXct(backup_date))
backup_date <- as.Date(as.character(backup_date))
backup_date <- as.Date(format(backup_date))

if (is_POSIXct(now)){
now <- as.Date(as.character(now))
now <- as.Date(format(now))
} else if (is.character(now)){
now <- as.Date(parse_datetime(now))
}
Expand Down
3 changes: 2 additions & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

0 errors | 0 warnings | 0 notes

Rebuild help files for new R 4.2.0
Fix issues related to the change in behaviour of `as.character.POSIXct()` & co
in r-devel.
8 changes: 4 additions & 4 deletions tests/testthat/test_BackupQueue.R
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ test_that("BackupQueueDatetime works with supported timestamp formats", {
)

expect_identical(
as.character(bq$files$timestamp),
format(bq$files$timestamp),
eres
)

Expand All @@ -719,7 +719,7 @@ test_that("BackupQueueDatetime works with supported timestamp formats", {
file.create(noback)

expect_identical(
as.character(bq$files$timestamp),
format(bq$files$timestamp),
eres
)

Expand Down Expand Up @@ -955,7 +955,7 @@ test_that("BackupQueueDateTime: `age` works with no backups", {
file.create(tf)
on.exit(unlink(tf), add = TRUE)

now <- as.Date(as.character(file.info(tf)$ctime))
now <- as.Date(format(file.info(tf)$ctime))
bq <- BackupQueueDateTime$new(tf)
expect_false(bq$should_rotate(age = "10 day", now = now, size = 0))
expect_true(bq$should_rotate(age = "0 day", now = now, size = 0))
Expand Down Expand Up @@ -1377,7 +1377,7 @@ test_that("BackupQueueDateTime: `age` works with no backups", {
file.create(tf)
on.exit(unlink(tf), add = TRUE)

now <- as.Date(as.character(file.info(tf)$ctime))
now <- as.Date(format(file.info(tf)$ctime))
bq <- BackupQueueDate$new(tf)
expect_false(bq$should_rotate(age = "10 day", now = now, size = 0))
expect_true(bq$should_rotate(age = "0 day", now = now, size = 0))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_parsers.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test_that("parse_size throws warning when it encounters floats", {

test_that("parse_datetime works as expected", {
d <- as.Date("2019-12-01")
expect_equal(parse_datetime(d), as.POSIXct(as.character(d)))
expect_equal(parse_datetime(d), as.POSIXct(format(d)))

expect_equal(parse_datetime("2018-12-01"), as.POSIXct("2018-12-01"))
expect_equal(parse_datetime("20181201"), as.POSIXct("2018-12-01"))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_rotate.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test_that("backup/rotate happy path", {
expect_identical(bq$n, 3L)
expect_equal(file.size(tf), 0)
expect_equal(file.size(bq$files$path[[1]]), tf_size)
expect_equal(bq$files$sfx, as.character(1:3))
expect_equal(bq$files$sfx, format(1:3))

bq$prune(0)
file.remove(tf)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_rotate_time.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test_that("backup_time common usecases", {
expect_identical(bq$n, 4L)
expect_equal(bq$last_rotation, as.POSIXct("2019-03-01 00:00:01"))
expect_identical(
as.character(min(bq$files$timestamp)),
format(min(bq$files$timestamp)),
"2019-01-01"
)

Expand Down

0 comments on commit db42e9f

Please sign in to comment.