converting seconds from the Epoch into localtime

Suppose I have a few timestamps given as integers (seconds) from the Unix Epoch (1970-01-01 00:00:00Z).

How do I convert them into the proper date-time in the local timezone? I've been looking at the as.POSIXct help page as well as related questions on SO. It's easy to do in UTC, but for some reason I can't seem able to do it straightforwardly for the local timezone, or for another timezone (BTW, I happen to be in "America/Los_Angeles", or "PST5PDT", which becomes "PST or "PDT" depending of whether daytime savings is in effect at the specified time; so I usually specify the location-based timezone rather than "PST" or "PDT", which are finicky).

Setup for the examples:

z <- c(1360527317,1363019665)

Quick verification in perl:

echo -n 1360527317,1363019665 | perl -ne '
use POSIX /strftime/;
$fmt = "%Y-%m-%d %H:%M:%S";
for (split /,/) {
  $loc=strftime("$fmt %Z", localtime($_));
  $gmt=strftime("$fmt GMT", gmtime($_));
  print "$_: $loc $gmtn";
}'
# gives:
1360527317: 2013-02-10 12:15:17 PST 2013-02-10 20:15:17 GMT
1363019665: 2013-03-11 09:34:25 PDT 2013-03-11 16:34:25 GMT

First, the obvious (in UTC):

as.POSIXct(z, origin='1970-01-01', tz='GMT')
# --> [1] "2013-02-10 20:15:17 GMT" "2013-03-11 16:34:25 GMT"

Here are things I tried that DON'T work :

as.POSIXct(z, origin='1970-01-01')
# --> (wrong) [1] "2013-02-10 20:15:17 PST" "2013-03-11 17:34:25 PDT"

as.POSIXct(z, origin='1970-01-01 00:00:00 Z')
# --> (wrong) [1] "2013-02-10 20:15:17 PST" "2013-03-11 17:34:25 PDT"

as.POSIXct(z, origin='1970-01-01', tz='America/Los_Angeles')
# --> (wrong) [1] "2013-02-10 20:15:17 PST" "2013-03-11 17:34:25 PDT"

At my wits' end, here is something that gives me the correct outcome :

now=Sys.time(); now+(z-unclass(now))
# --> [1] "2013-02-10 12:15:17 PST" "2013-03-11 09:34:25 PDT"

And BTW on my system:

now=Sys.time()
now
# --> [1] "2013-03-13 18:26:05 PDT"
unclass(now)
# --> [1] 1363224365

So it appears that my settings and local timezone are correct.

Any idea what I'm doing wrong with the lines that don't work above?

In the meantime, I'll be using the following trick, hopefully useful to someone else:

localtime <- function(t) {
  now = Sys.time();
  return(now+(unclass(t)-unclass(now)))
}
# quick test:
localtime(Sys.time())
# --> [1] "2013-03-13 18:33:40 PDT"
localtime(z)
# --> [1] "2013-02-10 12:15:17 PST" "2013-03-11 09:34:25 PDT"

尝试将Date传递给origin而不是字符串

as.POSIXct(z, origin=as.Date('1970-01-01'), tz='America/Los_Angeles')

You need to differentiate between parsing / storing as UTC

R> pt <- as.POSIXct(z, origin=as.Date("1970-01-01"))
R> pt
[1] "2013-02-10 14:15:17 CST" "2013-03-11 11:34:25 CDT"

Now you can show in whichever TZ you want:

R> format(pt, tz="America/Chicago")
[1] "2013-02-10 14:15:17" "2013-03-11 11:34:25"
R> format(pt, tz="America/Los_Angeles")
[1] "2013-02-10 12:15:17" "2013-03-11 09:34:25"
R> 
链接地址: http://www.djcxy.com/p/18494.html

上一篇: 如何在.NET中使用自定义格式String.Format TimeSpan对象?

下一篇: 将Epoch秒数转换为本地时间