1

Is there a way to get the elapsed running time of an application in React Native?

If possible to get some form of ticks which is not related to the device's time & date.

Pseudo code:

const ticks = Application.getTicks();
console.log(ticks);

While changing the device's date & time will not affect the returned ticks.

Guy
  • 12,488
  • 16
  • 79
  • 119
  • Why not just keep track of the current`dateTIme` on start up? Then when you want to check subtract the current `datetime` to the one on start up? – Train Aug 05 '18 at 23:12
  • @OrthoHomeDefense We're trying to prevent users from tampering with the date and time of the device to effect their check in time while the device is offline – Guy Aug 05 '18 at 23:51
  • Getting the current `dateTime` in `JavaScript` does not get the device `dateTime` using `new Date()` does not get the device time. Am I just not understanding you correctly? – Train Aug 06 '18 at 00:00
  • In React Native it does get the device time as far as I know. For instance: https://stackoverflow.com/questions/41294576/react-native-show-current-time-and-update-the-seconds-in-real-time – Guy Aug 06 '18 at 00:58

1 Answers1

1

I have found the answer on the following Gist.

To utilize it, I've built a simple module that returns the number of seconds since the OS was started.

Here is the Github repository.

The usage is as following:

import RelativeTime from 'react-native-relative-time';

and:

RelativeTime.getRelativeTime()
    .then(ticks => console.log(ticks);

It returns the number of seconds since the OS started:

  • Restarting the Android simulator resets the value returned (as expected)
  • Restaring the iOS simulator seems to keep the value. Probably means the value which is returned is the Mac's uptime.
  • Changing the device's time does not change the value returned (as expected)

iOS

Android

Guy
  • 12,488
  • 16
  • 79
  • 119
  • since in iOS you use time(&now) to calculate the elapsed time, doesn't that compromise the uptime? if a user changes time settings it will return a false value – daydr3amer Aug 19 '20 at 10:14