21

I have a local time like "2013-08-27 10:01:22", how do I convert it to epoch time?

basically I need the opposite of as.POSIXct, I searched on google and surprisingly didn't find any.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
swang
  • 5,157
  • 5
  • 33
  • 55

3 Answers3

30

You can use as.integer to get seconds since epoch began...

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.

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • 2
    Should you have sub-second resolution you need to use `as.numeric` instead of `as.integer` as sub-second are stored as decimal of a second – statquant Aug 27 '13 at 11:30
  • `POSIXct` represents time to the nearest second. That's a different date/time format. – Simon O'Hanlon Aug 27 '13 at 11:34
  • Thanks both of you, it would be nice if it can work with format like "2013-08-27 10:01:22.123456", but I don't really need the sub second bit at the moment. – swang Aug 27 '13 at 11:35
  • @swang what is your input data like? A vector of strings? – Simon O'Hanlon Aug 27 '13 at 11:35
  • 2
    @swang As I said for sub-millis `x <- as.POSIXct('2012-06-01 12:32:12.123')` `as.numeric(x)` will give you `1338546732.123` (carefull with `options(digits)` it needs to be big enough) – statquant Aug 27 '13 at 11:39
  • @statquant: I tried it but it didn't work: > x<-as.POSIXct("2013-08-27 10:01:22:123456") > as.numeric(x) [1] 1377594082 – swang Aug 27 '13 at 11:41
  • the separator of micros is `.` not `:` and you need `options(digits=16)` – statquant Aug 27 '13 at 11:41
  • tried that too: > x<-as.POSIXct("2013-08-27 10:01:22.123456") > as.numeric(x) [1] 1377594082 – swang Aug 27 '13 at 11:42
8

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:

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:

> 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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
2

we can also use

unclass(Sys.time())
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
miku
  • 95
  • 1
  • 2
  • 11