I have a local time like "2013-08-27 10:01:22"
, how do I convert it to epoch time?
我有一个本地时间,比如“2013-08-27 10:01:22”,我该如何将它转换为*时间呢?
basically I need the opposite of as.POSIXct
, I searched on google and surprisingly didn't find any.
基本上我需要a的反义词。POSIXct,我在谷歌上搜索,却没有找到。
2 个解决方案
#1
23
You can use as.integer
to get seconds since epoch began...
你可以使用as.integer来获取秒,从纪元开始……
x <- as.POSIXct( Sys.time() )
#[1] "2013-08-27 12:37:17 BST"
class(x)
#[1] "POSIXct" "POSIXt"
as.integer( x )
#[1] 1377603437
Using a vector of strings called times
:
用一个叫做时间的弦向量:
times
#[1] "2013-08-27 12:39:32" "2013-08-27 12:39:33" "2013-08-27 12:39:34"
#[4] "2013-08-27 12:39:35" "2013-08-27 12:39:36" "2013-08-27 12:39:37"
#[7] "2013-08-27 12:39:38" "2013-08-27 12:39:39" "2013-08-27 12:39:40"
#[10] "2013-08-27 12:39:41"
as.integer( as.POSIXct( times ) )
#[1] 1377603609 1377603610 1377603611 1377603612 1377603613 1377603614
#[7] 1377603615 1377603616 1377603617 1377603618
Without the timezone in the strings you will probably have to specify the tz
argument to as.POSIXct
, e.g. as.integer( as.POSIXct( times ) , tz = "BST" )
for British Summertime.
如果没有字符串中的时区,您可能需要将tz参数指定为。POSIXct,例如as.integer(如。英国夏令时的POSIXct(times), tz =“BST”。
#2
3
The accepted answer truncates time to the whole second. POSIXct
actually provides sub-second resolution, though. As mentioned in the comments by “statquant”, you can use as.numeric
to obtain the exact epoch:
被接受的答案将时间缩短到整个秒。然而,POSIXct实际上提供了亚秒分辨率。正如“statquant”评论中提到的,您可以使用As。数值以获得确切的历元:
result = as.numeric(as.POSIXct(Sys.time()))
Beware that with the default options for digit display in R this will look like it has no digits behind the decimal point:
请注意,在R中显示数字的默认选项将会看起来在小数点后没有数字:
> result
[1] 1480599768
However, these are simply truncated in the display. To make them visible, use:
然而,这些只是在显示中被截断。要使它们可见,请使用:
> dput(result)
1480599767.58447
… or set options('digits')
to a higher value.
或将选项(“位数”)设置为更高的值。
#1
23
You can use as.integer
to get seconds since epoch began...
你可以使用as.integer来获取秒,从纪元开始……
x <- as.POSIXct( Sys.time() )
#[1] "2013-08-27 12:37:17 BST"
class(x)
#[1] "POSIXct" "POSIXt"
as.integer( x )
#[1] 1377603437
Using a vector of strings called times
:
用一个叫做时间的弦向量:
times
#[1] "2013-08-27 12:39:32" "2013-08-27 12:39:33" "2013-08-27 12:39:34"
#[4] "2013-08-27 12:39:35" "2013-08-27 12:39:36" "2013-08-27 12:39:37"
#[7] "2013-08-27 12:39:38" "2013-08-27 12:39:39" "2013-08-27 12:39:40"
#[10] "2013-08-27 12:39:41"
as.integer( as.POSIXct( times ) )
#[1] 1377603609 1377603610 1377603611 1377603612 1377603613 1377603614
#[7] 1377603615 1377603616 1377603617 1377603618
Without the timezone in the strings you will probably have to specify the tz
argument to as.POSIXct
, e.g. as.integer( as.POSIXct( times ) , tz = "BST" )
for British Summertime.
如果没有字符串中的时区,您可能需要将tz参数指定为。POSIXct,例如as.integer(如。英国夏令时的POSIXct(times), tz =“BST”。
#2
3
The accepted answer truncates time to the whole second. POSIXct
actually provides sub-second resolution, though. As mentioned in the comments by “statquant”, you can use as.numeric
to obtain the exact epoch:
被接受的答案将时间缩短到整个秒。然而,POSIXct实际上提供了亚秒分辨率。正如“statquant”评论中提到的,您可以使用As。数值以获得确切的历元:
result = as.numeric(as.POSIXct(Sys.time()))
Beware that with the default options for digit display in R this will look like it has no digits behind the decimal point:
请注意,在R中显示数字的默认选项将会看起来在小数点后没有数字:
> result
[1] 1480599768
However, these are simply truncated in the display. To make them visible, use:
然而,这些只是在显示中被截断。要使它们可见,请使用:
> dput(result)
1480599767.58447
… or set options('digits')
to a higher value.
或将选项(“位数”)设置为更高的值。