How to get the current time in native Android code?
Asked Answered
E

4

27

I was wondering if there is an easy way to get the current time in native Android code. Optimally it would be something comparable to System.getTimeMillies(). I will only be using it to see how long certain function calls will take so a long variable with the current time in milliseconds would be the optimal solution for me.

Thanks in advance!

Eolanda answered 30/9, 2010 at 15:24 Comment(0)
C
19

For microsecond resolution you can use gettimeofday(). This uses "wall clock time", which continues to advance when the device is asleep, but is subject to sudden shifts forward or backward if the network updates the device's clock.

You can also use clock_gettime(CLOCK_MONOTONIC). This uses the monotonic clock, which never leaps forward or backward, but stops counting when the device sleeps.

The actual resolution of the timers is device-dependent.

Both of these are POSIX APIs, not Android-specific.

Chari answered 1/10, 2010 at 18:23 Comment(1)
CLOCK_BOOTTIME (since Linux 2.6.39; Linux-specific) would be better?Depredation
E
39

For the lazy, add this to the top of your code:

#include <time.h>

// from android samples
/* return current time in milliseconds */
static double now_ms(void) {

    struct timespec res;
    clock_gettime(CLOCK_REALTIME, &res);
    return 1000.0 * res.tv_sec + (double) res.tv_nsec / 1e6;

}

Call it like this:

double start = now_ms(); // start time

// YOUR CODE HERE

double end = now_ms(); // finish time

double delta = end - start; // time your code took to exec in ms
Epiphytotic answered 14/1, 2013 at 2:55 Comment(0)
C
19

For microsecond resolution you can use gettimeofday(). This uses "wall clock time", which continues to advance when the device is asleep, but is subject to sudden shifts forward or backward if the network updates the device's clock.

You can also use clock_gettime(CLOCK_MONOTONIC). This uses the monotonic clock, which never leaps forward or backward, but stops counting when the device sleeps.

The actual resolution of the timers is device-dependent.

Both of these are POSIX APIs, not Android-specific.

Chari answered 1/10, 2010 at 18:23 Comment(1)
CLOCK_BOOTTIME (since Linux 2.6.39; Linux-specific) would be better?Depredation
M
5

Another one for the lazy, this function returns the current time in nanoseconds using CLOCK_MONOTONIC

#include <time.h>
#define NANOS_IN_SECOND 1000000000

static long currentTimeInNanos() {

    struct timespec res;
    clock_gettime(CLOCK_MONOTONIC, &res);
    return (res.tv_sec * NANOS_IN_SECOND) + res.tv_nsec;
}
Multiflorous answered 21/3, 2017 at 14:29 Comment(1)
I don't believe the monotonic clock is guaranteed to start at any particular value, and it's unwise to depend on the size of long. If you're going to multiply a time_t by 1e9 you should use an explicit 64-bit type (e.g. int64_t) to cast tv_sec and as the return value.Chari
B
0
#include <time.h>

time_t currentTime;
time(&currentTime);
auto readableDate = ctime(&currentTime);
Bicapsular answered 19/6, 2023 at 3:20 Comment(1)
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?Penner

© 2022 - 2024 — McMap. All rights reserved.