Skip to content

Commit

Permalink
[Fix #1085] Preserve time zone of the input POSIXt in as_datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
vspinu committed Nov 5, 2022
1 parent c4cb901 commit fbd991d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Version 1.8.0.9000

### BUG FIXES

* [#1085](https://github.com/tidyverse/lubridate/issues/1085) `as_datetime()` now preserves the time zone of the POSIXt input.
* [#1072](https://github.com/tidyverse/lubridate/issues/1072) Names are now handled correctly when combining multiple Period or Interval objects.
* [#1003](https://github.com/tidyverse/lubridate/issues/1003) Correctly handle r and R formats in locales which have no p format
* [#1074](https://github.com/tidyverse/lubridate/issues/1074) Fix concatination of named Period, Interval and Duration vectors.
Expand Down
4 changes: 1 addition & 3 deletions R/accessors-tz.r
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ tz.logical <- function(x) {

#' @export
tz.default <- function(x) {
### FIXME: make this `stop` in next major release
## VS[21-03-2020]: v.1.7.8 already sent notification to broken packages
warning(
"tz(): Don't know how to compute timezone for object of class ",
paste0(class(x), collapse = "/"),
"; returning \"UTC\". This warning will become an error in the next major version of lubridate.",
"; returning \"UTC\".",
call. = FALSE
)
"UTC"
Expand Down
4 changes: 2 additions & 2 deletions R/coercion.r
Original file line number Diff line number Diff line change
Expand Up @@ -772,15 +772,15 @@ setGeneric("as_datetime",
#' @rdname as_date
#' @export
setMethod("as_datetime", "ANY",
function(x, tz = "UTC") {
function(x, tz = lubridate::tz(x)) {
with_tz(as.POSIXct(x, tz = tz), tzone = tz)
}
)

#' @rdname as_date
#' @export
setMethod("as_datetime", "POSIXt",
function(x, tz = "UTC") {
function(x, tz = lubridate::tz(x)) {
with_tz(x, tz)
}
)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-POSIXt.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ test_that("converts character", {
})

test_that("changes timezone of POSIXct", {
dt <- as_datetime(make_datetime(tz = "America/Chicago"))
dt <- as_datetime(make_datetime(tz = "America/Chicago"), tz = "UTC")
expect_equal(tz(dt), "UTC")
})

Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-time-zones.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,14 @@ test_that("If original post/pre is known, ignore roll_DST", {
tzone_out = "America/Chicago",
roll_dst = "post"))
})


test_that("as_datetime does not loose time zone", {
## #1085
x <- ymd_hms("2022-01-01 23:40:36", tz = "US/Pacific")
expect_identical(x, as_datetime(x))
expect_identical(with_tz(x, "Europe/Amsterdam"), as_datetime(x, tz = "Europe/Amsterdam"))
x <- as.POSIXlt(x)
expect_identical(x, as_datetime(x))
expect_identical(with_tz(x, "Europe/Amsterdam"), as_datetime(x, tz = "Europe/Amsterdam"))
})

0 comments on commit fbd991d

Please sign in to comment.