How do I convert correctly timezones

前端 未结 3 2131
渐次进展
渐次进展 2021-02-11 02:10

I am using the fasttime package for its fastPOSIXct function that can read character datetimes very efficiently. My problem is that it can only read character datet

3条回答
  •  别那么骄傲
    2021-02-11 02:48

    It's a daylight savings issue: http://www.timeanddate.com/time/dst/2010a.html

    In 2010 it began on the 14th March in Canada, but not until the 28th March in the UK.

    You can use POSIXlt objects to modify timezones directly:

    lt <- as.POSIXlt(as.POSIXct("2010-03-15 12:37:17.223",tz="GMT"))
    attr(lt,"tzone") <- "America/Montreal"
    as.POSIXct(lt)
    [1] "2010-03-15 12:37:17 EDT"
    

    Or you could use format to convert to a string and set the timezone in a call to as.POSIXct. You can therefore modify forceTZ:

    forceTZ <- function(x,tz)
    {
      return(as.POSIXct(format(x),tz=tz))
    }
    
    
    forceTZ(as.POSIXct("2010-03-15 12:37:17.223",tz="GMT"),"America/Montreal")
    [1] "2010-03-15 12:37:17 EDT"
    

提交回复
热议问题