Search code examples
rtimestamptime-seriesunix-timestampsequences

Create a series of timestamps along milliseconds in R


I have start date as "2017-11-14 10:11:01 CET" and end date as "2017-11-15 01:15:59 CET". I want to create timestamps of 500 milliseconds each, between these start and end timestamps. E.g. I need a dataframe containing the following output

timestamp
2017-11-14 10:11:01.000
2017-11-14 10:11:01.500
2017-11-14 10:11:02.000
2017-11-14 10:11:02.500
.
.
.
2017-11-15 01:15:59.000

Somehow, I managed to get the start and end date in milliseconds using the following code:

start.date <- strptime("2017-11-14 10:11:01 CET", "%Y-%m-%d %H:%M:%OS")
start.date <- format(start.date, "%Y-%m-%d %H:%M:%OS3")
Output: "2017-11-14 10:11:01.000"

Similarly, I got end date as 2017-11-15 01:15:59.000. I now want to use sequence function to create sequence of timestamps each of 500 milliseconds. I did something like

seq.POSIXt(as.POSIXct(start), as.POSIXct(end), units = "milliseconds", by = 500)

but it created a series of 20 seconds. e.g.,

"2017-11-14 10:11:01 CET" 
"2017-11-14 10:19:21 CET" 
"2017-11-14 10:27:41 CET"
.
.
.
"2017-11-15 01:11:01 CET"

Once, I am able to get this series, I further want to convert it into UNIX timestamps. Can someone help me to resolve this issue?


Solution

  • I don't know if this is what you are looking for but it seems to be working

    seq.POSIXt(as.POSIXct(start), as.POSIXct(end), units = "seconds", by = .5)