Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Unix timestamp into datetime

I have the following data frame

> head(try)
     creates       time
1  128.29508 1417392072
3  236.98361 1417392072
7   98.45902 1417392072
9  157.44068 1417392131
10 227.38333 1417392131
11 242.03390 1417392131

> str(try)
'data.frame':   102968 obs. of  2 variables:
 $ creates: num  128.3 237 98.5 157.4 227.4 ...
 $ time   : Factor w/ 26418 levels "1417392071","1417392072",..: 2 2 2 3 3 3 3 3 5 5 ...

I am unable to convert the UNIX timestamp into datetime using the following methods I tried

> head(as.POSIXlt(as.numeric(try$time),origin="1970-01-01",tz="GMT"))
[1] "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:03 UTC" "1970-01-01 00:00:03 UTC"
[6] "1970-01-01 00:00:03 UTC"

> head(as.POSIXct(as.character(try$time),tz="GMT"))

Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format

> head(as.POSIXlt(as.POSIXct(as.vector(as.numeric(try$time)),origin="1970-01-01")))
[1] "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:03 UTC" "1970-01-01 00:00:03 UTC"
[6] "1970-01-01 00:00:03 UTC"

I'm not sure what I'm doing incorrectly here.

like image 516
user3006691 Avatar asked Dec 10 '14 18:12

user3006691


People also ask

How do I convert timestamp to time in Unix?

The getTime method returns the number of milliseconds since the Unix Epoch (1st of January, 1970 00:00:00). To get a Unix timestamp, we have to divide the result from calling the getTime() method by 1000 to convert the milliseconds to seconds. What is this?

How do I convert timestamp to date?

The constructor of the Date class receives a long value as an argument. Since the constructor of the Date class requires a long value, we need to convert the Timestamp object into a long value using the getTime() method of the TimeStamp class(present in SQL package).

How do I cast a timestamp to a datetime?

The to_date() function in Apache PySpark is popularly used to convert Timestamp to the date. This is mostly achieved by truncating the Timestamp column's time part. The to_date() function takes TimeStamp as it's input in the default format of "MM-dd-yyyy HH:mm:ss.

What is Unix time Converter?

The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch.


3 Answers

You have to convert from Factor to Character To Number before using as.POSIXct. The function is expecting an integer as a Unixtime

head(as.POSIXct(as.numeric(as.character(try$time)), origin="1970-01-01", tz="GMT"))
like image 89
Emer Avatar answered Oct 30 '22 15:10

Emer


It's because the epoch is not numeric, try converting it into a number and it works like wonder!

try$time <- as.POSIXct(as.numeric(try$time), origin = '1970-01-01', tz = 'GMT')
like image 40
Kushal Shah Avatar answered Oct 30 '22 15:10

Kushal Shah


Try

head(as.POSIXct(as.integer(as.numeric(as.character(try$time)) / 1000.0), 
origin='1970-01-01', tz="GMT"))
like image 41
Guruprasad Sridharan Avatar answered Oct 30 '22 15:10

Guruprasad Sridharan