11

I am trying to convert UTC time to local standard time. I have found many functions which convert to Local Daylight Time, but I was not successful in getting the standard time. Right now, I have the following code which converts to local daylight time at my specific timezone:

pb.date <- as.POSIXct(date,tz="UTC")
format(pb.date, tz="timeZone",usetz=TRUE)

I would appreciate any help.

user227710
  • 3,164
  • 18
  • 35
newbie
  • 757
  • 1
  • 9
  • 19

2 Answers2

10

First, POSIXct date-times are always UCT internally. The print.POSIXt and format.POSIXt methods will appropriately make the TZ shift on output from their internal representations:

pb.date <- as.POSIXct(Sys.Date())
Sys.Date()
#[1] "2015-07-09"

So that was midnight of the current date in Greenwich:

format(pb.date, tz="America/Los_Angeles",usetz=TRUE)
#[1] "2015-07-08 17:00:00 PDT"

When it's midnight in Greenwich, it's 5PM Daylight Time in the previous day on the Left Coast of the US. You need to use the correct character values for your TZ (and your OS) both of which at the moment are unspecified.

The US Pacific timezone is 8 hours behind GMT (in winter months) so you can use a timezone that is Standard/Daylight-agnostic:

> format(pb.date,usetz=TRUE, tz="Etc/GMT+8")
[1] "2015-07-08 16:00:00 GMT+8"

(Notice the reversal of + with "behind" and - with "ahead".)

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I knew that, but somehow I did not get right results when I did not specify "UTC". But still my problem is not solved. In your example, the converted time is PDT which is Daylight Time. How do you get standard time which has an hour shift? – newbie Jul 09 '15 at 18:36
  • You are right tz="timeZone" is not valid. This was a part of a bigger code, I just wrote timeZone here to make my point. But I assure you that is working fine in my code. – newbie Jul 09 '15 at 18:41
  • Does it take into account Daylight Savings? – thentangler Apr 01 '22 at 18:11
  • @thentangler UCT has no Daylight Savings Time. I showed you how to calculate a fixed offset from UCT , A.K.A. GMT. – IRTFM Apr 01 '22 at 18:37
  • Sorry what I meant to as was: does the “tz” parameter adjust for daylight savings automatically as the year progresses? – thentangler Apr 02 '22 at 20:49
  • If that timezone's entry in timezone database exists, then there should be adjustment for progression (and regression) at the correct date and time of that locale in that particular year. Depending on the function being used you may also need to set 'usetz' to `TRUE`. If you are having problems with input of datetimes then read `?strptime` and `?as.POSIXct`, then do some SO searching. There are tons of worked examples – IRTFM Apr 02 '22 at 21:25
0

I know this question has an accepted answer, but in case anyone comes along and this can help. I needed a function to convert UTC times to MTN time (Server is in UTC, we operate in MTN). Not sure why, but needed to force it to UTC/GMT first and the convert it to MTN. However it does work

mtn_ts = function(utcTime){
  library(lubridate)
  toTz = "us/mountain"
  utcTime = force_tz(utcTime,tzone= 'GMT')
  dt = as.POSIXct(format(utcTime,tz = toTz,origin ='GMT', usetz=TRUE))
  dt = force_tz(dt,tzone= toTz)
  return(dt)
}
mtn_ts(as.POSIXct("2021-09-27 14:48:51.000000000"))
Josh Pachner
  • 151
  • 6