Get current time in milliseconds - r

I am trying to do an API call which requires a time in milliseconds. I am pretty new in R and been Googling for hours to achieve something like what In Java would be:
System.currentTimeMillis();
Only thing i see is stuff like
Sys.Date() and Sys.time
which returns a formatted date instead of time in millis.
I hope someone can give me a oneliner which solves my problem.

Sys.time does not return a "formatted time". It returns a POSIXct classed object, which is the number of seconds since the Unix epoch. Of course, when you print that object, it returns a formatted time. But how something prints is not what it is.
To get the current time in milliseconds, you just need to convert the output of Sys.time to numeric, and multiply by 1000.
R> print(as.numeric(Sys.time())*1000, digits=15)
[1] 1476538955719.77
Depending on the API call you want to make, you might need to remove the fractional milliseconds.

No need for setting the global variable digits.secs.
See strptime for details.
# Print milliseconds of current time
# See ?strptime for details, specifically
# the formatting option %OSn, where 0 <= n <= 6
as.numeric(format(Sys.time(), "%OS3")) * 1000

To get current epoch time (in second):
as.numeric(Sys.time())
If you want to get the time difference (for computing duration for example), just subtract Sys.time() directly and you will get nicely formatted string:
currentTs <- Sys.time()
# about five seconds later
elapsed <- Sys.time() - currentTs
print(elapsed) # Time difference of 4.926194 secs

Related

R: Turn timestamps into (as short as possible) integers

Edit 1: I think a possible solution would be to count the number of 15-minute intervals elapsed since a starting date. If anyone has thoughts on this, please come forward. Thanks
As the title says, I am looking for a way to turn timestamps into as small as possible integers.
Explanation of the situation:
I am working with "panelAR". I have T>N panel-data containing different timestamps that look like this (300,000 rows in total):
df$timestamp[1]
[1] "2013-08-01 00:15:00 UTC"
class(df$timestamp)
[1] "POSIXct" "POSIXt"
I am using panelAR and thus need the timestamp as an integer. I can't simply use "as.integer" because I would hit the max length for integers resulting in only NA's. This was my first try to work around this problem:
df$timestamp <- as.numeric(gsub("[: -]", "" , df$timestamp, perl=TRUE))
Subtract the numbers starting at te 3rd position (Because "20" is irrelevant) and stop before the 2nd last position (Because they all end at 00 seconds)
(I need shorter integers in order to not hit the max level of integers in R)
df$timestamp <- substr(df$timestamp, 3, nchar(df$timestamp)-2)
#Save as integer
df$timestamp <- as.integer(df$timestamp)
#Result
df$timestamp[1]
1308010015
This allows panelAR to work with it, but the numbers seem to be way too large. When I try to run a regression with it, i get the following error message:
"cannot allocate vector of size 1052.2 GB"
I am looking for a way to turn these timestamps into (as small as possible) integers in order to work with panelAR.
Any help is greatly appreciated.
this big number that you get corresponds to the number of seconds elapsed since 1970-01-01 00:00:00. Do your time stamps have regular intervals? If it is, let's say, every 15 minutes you could divide all integers by 900, and it might help.
Another option is to pick your earliest date and subtract it from the others
#generate some dates:
a <- as.POSIXct("2013-01-01 00:00:00 UTC")
b <- as.POSIXct("2013-08-01 00:15:00 UTC")
series <- seq(a,b, by = 'min')
#calculate the difference (result are integers/seconds)
integer <- as.numeric(series - min(series))
If you still get memory problems, I might combine both.
I managed to solve the main question. Since this still results in a memory error, I think it stems from the number of observations and the way panelAR computes things. I will open a separate question for that matter.
I used
df$timestampnew <- as.integer(difftime(df$timestamp, "2013-01-01 00:00:00", units = "min")/15)
to get integers that count the number of 15-min intervals elapsed since a certain date.

how would you calculate the number of seconds since epoch in eiffel for a DATE_TIME object

how would you calculate the number of seconds since epoch in eiffel for a DATE_TIME object?
Assuming variable t is of type DATE_TIME (in UTC), the code to get the corresponding number of seconds since the epoch is
t.definite_duration (create {DATE_TIME}.make_from_epoch (0)).seconds_count
Note. If the calculation is performed rather often, it might be more efficient to create an object epoch of type DATE_TIME with create epoch.make_from_epoch (0) in advance and to use it in the calculation instead:
t.definite_duration (epoch).seconds_count
This seems to work:
make
-- Execute the example
local
l_date:DATE_TIME
l_seconds:INTEGER_64
do
create l_date.make_now
l_seconds := l_date.relative_duration (create {DATE_TIME}.make_from_epoch (0)).seconds_count
print("The number of seconds: " + l_seconds.out + "%N")
end

Calculating Time Differences using xts in R

I was wondering if there was a way to calculate time differences using the xts package without having to convert time values etc. if possible. I have an xts object with a time format given as 2010-02-15 13:35:59.123 (where the .123 is the milliseconds).
Now, I would like to find the number of milliseconds until the end of the day (i.e. 17:00:00). The problem however is that I basically have to do a few conversions of the data before I can do this (such as using as.POSIXct) and this becomes more complicated since I have to do it for several different days and possibly even different times. For this reason, I would prefer to not have to convert the "end of day time" and leave it as 17:00:00 such that in order to find the number of milliseconds between the present time and the end of day time I can just have a fairly simple operation such as 17:00:00.000 - 13:35:59.123 = ...
Is there a simple way to do this with minimal conversions? I'm certain xts has a function which I don't know of but I couldn't find anything in the documentation :/
EDIT: I forgot to mention, I tried the more 'straightforward' route by trying to compute the time differences by first trying to use the function as.POSIXct(16:00:00, format = "%H:%M:%S") but this gives an error, and I'm honestly not sure why...
You should be able to do this using a combination of ave(), .indexDate(), and a custom function. You didn't provide a reproducible example, so here's one using the daily data that comes with xts.
library(xts)
data(sample_matrix)
x <- as.xts(sample_matrix)
secsRemaining <- function(x) { end(x)-index(x) })
tdiff <- ave(x[,1], as.yearmon(index(x)), FUN = secsRemaining)
tdiff[86:92,]
# Open
# 2007-03-28 259200
# 2007-03-29 172800
# 2007-03-30 86400
# 2007-03-31 0
# 2007-04-01 2505600
# 2007-04-02 2419200
# 2007-04-03 2332800
In your case, the call would use .indexDate(x) instead of as.yearmon(index(x)).
tdiff <- ave(x[,1], .indexDate(x), FUN = secsRemaining)
Also note that this call to ave() only works on a 1-column xts object. Seems like a bug that it doesn't. Also note that you have to use FUN = with ave(), since the FUN argument occurs after ....

How to convert time difference into minutes in R?

I have the following program:
timeStart<-Sys.time()
timeEnd<-Sys.time()
difference<-timeEnd-timeStart
anyVector<-c(difference)
at the end I need to put that data into a vector, the problem that I have is than when the difference is in seconds the value is like:
4.46809 seconds
and when it passes some minutes the values is like:
2.344445 minutes
I would like that the answer is converted to minutes in any case, but when I do something like this:
anyVector<-c(difference/60)
for the case that the value of the difference is in seconds work fine, but it will also transform the data when is in minutes giving me an incorrect number.
So, how I can convert to minutes only when the answer is in seconds and not where is in minutes already?
You can do it with the difftime function from base R:
timeStart<-Sys.time()
timeEnd<-Sys.time()
difference <- difftime(timeEnd, timeStart, units='mins')
Output
> difference
Time difference of 0.1424748 mins
You just specify the units argument and set it to mins and the output will always be in minutes.

Set units of difference between datetime objects

The diff command returns the differences between dates in a vector of dates in the R date format. I'd like to control the units that are returned, but it seems like they are automatically determined, with no way to control it w/ an argument. Here's an example:
> t = Sys.time()
> diff(c(t, t + 1))
Time difference of 1 secs
And yet:
> diff(c(t, t+10000))
Time difference of 1.157407 days
The "time delta" object has a units attribute, but it seems silly to write a bunch of conditionals to coerce everything into days, seconds etc.
I'm not sure what you mean by "a bunch of conditionals," just change the units manually.
> t = Sys.time()
> a <- diff(c(t,t+1))
> b <- diff(c(t, t+10000))
> units(a) <- "mins"
> units(b) <- "mins"
> a
Time difference of 0.01666667 mins
> b
Time difference of 166.6667 mins
See ?difftime. If you only need to use diff to get the difference between two times (rather than a longer vector), then, as Dirk suggests, use the difftime function with the units parameter.
A POSIXct type (which you created by calling Sys.time()) always use fractional seconds since the epoch.
The difftime() functions merely formats this differently for your reading pleasure. If you actually specify the format, you get what you specified:
R> difftime(t+ 10000,t,unit="secs")
Time difference of 10000 secs
R> difftime(t+ 10000,t,unit="days")
Time difference of 0.115741 days
R>
I think you need difftime in which you can specify the desired units. See:
> difftime(Sys.time(), Sys.time()+10000)
Time difference of -2.777778 hours
> difftime(Sys.time(), Sys.time()+10000, units="secs")
Time difference of -10000 secs
Not sure how precise you care to be, but you can get very specific about date-times with the lubridate package. A wonky thing about time units is that their length depends on when they occur because of leap seconds, leap days, and other conventions.
After you load lubridate, subtracting date times automatically creates a time interval object.
library(lubridate)
int <- Sys.time() - (Sys.time() + 10000)
You can then change it to a duration, which measures the exact length of time. Durations display in seconds because seconds are the only unit that has a consistent length. If you want your answer in a specific unit, just divide by a duration object that has the length of one of those units.
as.duration(int)
int / dseconds(1)
int / ddays(1)
int / dminutes(5) #to use "5 minutes" as a unit
Or you could just change the int to a period. Unlike durations, periods don't have an exact and consistent length. But they faithfully map clock times. You can do math by adding and subtracting both periods and durations to date-times.
as.period(int)
Sys.time() + dseconds(5) + dhours(2) - ddays(1)
Sys.time() + hours(2) + months(5) - weeks(1) #these are periods
If yon need to use the diff() function (e.g. as I do in within a ddply function), you can also turn the input data into numeric format to always receive differences in seconds like this:
> t = Sys.time()
> diff(as.numeric(c(t, t+1)))
[1] 1
> diff(as.numeric(c(t, t+10000)))
[1] 10000
From that point on you can use the diff seconds to calculate differences in other units.

Resources