hearse
hearse

Reputation: 389

R: Time stamps, Unix time and correct usage of 'strptime'

I have a column named timings of class factor with time stamps in the following format:

1/11/07 15:15

I applied strptime on timings to generate tStamp as follows:

tStamp=strptime(timings,format="%m/%d/%Y %H:%M")

i) The corresponding entry in tStamp looks like 0007-01-11 15:15:00 now. Why has it made 2007 or 07 into 0007? What is a correct way to generate tStamp?

ii) After generating tStamp correctly, how do we convert it to the Unix time Seconds. (Seconds since...1970) format?

Upvotes: 2

Views: 322

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368201

You need the lowercase %y for 2-digit years:

R> pt <- strptime("1/11/07 15:15",format="%m/%d/%y %H:%M")
R> pt
[1] "2007-01-11 15:15:00 CST"
R>

where CST is my local timezone.

And as.numeric() or as.double() converts to a double ...

R> as.numeric(pt)
[1] 1168550100

... which has fractional seconds if those are in the input:

R> options("digits.secs"=3)    # show milliseconds
R> as.numeric(Sys.time())      # convert current time
[1] 1372201674.52              # now with sub0seconds.

Upvotes: 5

Related Questions