Get current time in milliseconds

I am trying to make an API call that takes time in milliseconds. I am new to R and have been working at Googling for a long time to achieve something like what was in Java:

System.currentTimeMillis(); 

The only thing I see is things like

Sys.Date() and Sys.time

which returns a formatted date instead of time in milliseconds.

I hope someone can give me an oneliner that solves my problem.

+11
source share
3 answers

Sys.time does not return "formatted time". It returns the POSIXct class, which is the number of seconds since Unix. Of course, when you print this object, it returns a formatted time. But something is not printing 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 create, you may need to remove fractional milliseconds.

+23
source

No need to set the global variable digits.secs . See strptime more 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 
+5
source

To find out the current time of an era (in seconds):

 as.numeric(Sys.time()) 

If you want to get the time difference (for example, to calculate the duration), just Sys.time() directly, and you will get a beautifully formatted string:

 currentTs <- Sys.time() # about five seconds later elapsed <- Sys.time() - currentTs print(elapsed) # Time difference of 4.926194 secs 
0
source

Source: https://habr.com/ru/post/1258256/


All Articles