Iago Giné Vázquez
2024-Aug-22 08:59 UTC
[R] Force conversion of (POSIXct) time zone with base R
Hi, How should POSIXct time zone be changed without modifying the specified time (so fix the time zone). I tried> now <- Sys.time() > now[1] "2024-08-22 10:56:24 CEST"> as.POSIXct(now, tz = "UTC")[1] "2024-08-22 08:56:24 UTC"> as.POSIXct(now, origin = as.POSIXct("1970-01-01", tz = "UTC"), tz = "UTC")[1] "2024-08-22 08:56:24 UTC"> format(now, tz = "UTC")[1] "2024-08-22 08:56:24" But I want "2024-08-22 10:56:24 UTC" Thanks! Best regards, Iago [[alternative HTML version deleted]]
Hi what about now <- Sys.time() now [1] "2024-08-22 11:24:23 CEST" now.utc <- as.POSIXct(now, tz = "UTC")+2*3600 now.utc [1] "2024-08-22 11:24:23 UTC" Cheers. Petr ?t 22. 8. 2024 v 11:00 odes?latel Iago Gin? V?zquez <iago.gine at sjd.es> napsal:> Hi, > > How should POSIXct time zone be changed without modifying the specified > time (so fix the time zone). I tried > > > now <- Sys.time() > > now > [1] "2024-08-22 10:56:24 CEST" > > as.POSIXct(now, tz = "UTC") > [1] "2024-08-22 08:56:24 UTC" > > as.POSIXct(now, origin = as.POSIXct("1970-01-01", tz = "UTC"), tz > "UTC") > [1] "2024-08-22 08:56:24 UTC" > > format(now, tz = "UTC") > [1] "2024-08-22 08:56:24" > > But I want > "2024-08-22 10:56:24 UTC" > > Thanks! > > Best regards, > Iago > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
as.POSIXct(as.character(now),tz="UTC") or as.POSIXct(as.character(Sys.time()),tz="UTC") On Thu, Aug 22, 2024 at 12:00?PM Iago Gin? V?zquez <iago.gine at sjd.es> wrote:> Hi, > > How should POSIXct time zone be changed without modifying the specified > time (so fix the time zone). I tried > > > now <- Sys.time() > > now > [1] "2024-08-22 10:56:24 CEST" > > as.POSIXct(now, tz = "UTC") > [1] "2024-08-22 08:56:24 UTC" > > as.POSIXct(now, origin = as.POSIXct("1970-01-01", tz = "UTC"), tz > "UTC") > [1] "2024-08-22 08:56:24 UTC" > > format(now, tz = "UTC") > [1] "2024-08-22 08:56:24" > > But I want > "2024-08-22 10:56:24 UTC" > > Thanks! > > Best regards, > Iago > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
? Thu, 22 Aug 2024 08:59:46 +0000 Iago Gin? V?zquez <iago.gine at sjd.es> ?????:> How should POSIXct time zone be changed without modifying the > specified time (so fix the time zone).Since POSIXct represents the number of seconds since the specified "epoch" moment (beginning of 1970 UTC), fixing the time zone while retaining the local time requires modifying the stored time. It might be easier to go through POSIXlt, which represents a local time: (now <- as.POSIXlt(Sys.time())) attr(now, 'tzone') <- 'UTC' (now <- as.POSIXct(now)) (Why attr(now, 'tzone') and not now$zone? Historical reasons, I suppose, but at least both are documented in ?POSIXlt.) You might also go through the string representation (which effectively obtains the local time from the epoch time), but that feels brittle, especially if the POSIXct value has attr(., 'tzone') set: # time zone information successfully lost and replaced by UTC Sys.time() |> format() |> as.POSIXct(tz = 'UTC') -- Best regards, Ivan