# Problem with getting time using a function – Programming Questions

Looks like NOW2() (your second example) has an overflow problem between 4292 and 4296 seconds. That is probably a uint32_t rollover at 4,294,967,296 microseconds.

millis() NOW1()         NOW2()
4279515, 4279515796000, 4279515820000
4283719, 4283719804000, 4283719824000
4287923, 4287923800000, 4287923820000
4292127, 4292127820000, 4292127844000
4296331, 4296331840000, 1364568000
4300534, 4300535164000, 5567892000
4304738, 4304738608000, 9771332000
4308941, 4308942072000, 13974796000
4313145, 4313145612000, 18178340000


The “NOW1()” function is still keeping the correct time.

const uint64_t MICROSECONDS = 1000ull;

int64_t NOW1()
{
static uint32_t g_lastMicroseconds = 0;
static int64_t g_currentTime = 0;

uint32_t time = micros();

if (time != g_lastMicroseconds)
{
g_currentTime += int64_t(time - g_lastMicroseconds) * MICROSECONDS;
g_lastMicroseconds = time;
}

return g_currentTime;
}

int64_t NOW2()
{
static int64_t g_lastMicroseconds = 0;
static int64_t g_currentTime = 0;

int64_t time = micros();

if (time != g_lastMicroseconds)
{
g_currentTime += (int64_t(time - g_lastMicroseconds)) * MICROSECONDS;
g_lastMicroseconds = time;
}

return g_currentTime;
}

void printull(uint64_t value)
{
if (value >= 10)
printull(value / 10);
Serial.print(char((value % 10) + '0'));
}

void setup()
{
Serial.begin(115200);
}

void loop()
{
uint64_t now1 = NOW1();
uint64_t now2 = NOW2();

Serial.print(millis());
Serial.print(", ");
printull(now1);
Serial.print(", ");
printull(now2);
Serial.println();

delay(4200);
}