[ISSUE] Time.minute rolls over before Time.hour

I think I’ve found a small bug with the Time class. I’ve found that about 3 times per day, the Time.minute() will roll over (i.e. 59 -> 00) but Time.hour() doesn’t increment on the same cycle. Here’s some simple code I wrote to catch it, and I have been able to confirm it on an Argon running Device OS v1.4.4 and v1.5.1

int nowHour, nowMin, nowMinLast, nowHourLast, issueCounter;

void setup() {
    Particle.variable("issueCounter", issueCounter);
    issueCounter = 0;
}

void loop() {
    
    // Check the time
    nowHour = Time.hour();
    nowMin = Time.minute();
    
    if (nowMin != nowMinLast) {
        if (nowMin == 0) {
            Particle.publish("log", "Hour: "+String(nowHour), PRIVATE);
            if (nowHour == nowHourLast) {
                issueCounter++;
                Particle.publish("log", "Issue detected", PRIVATE);
            }
        }
        nowMinLast = nowMin;
        nowHourLast = nowHour;
    }
}

Here’s an output example:

2020-05-07 00:00:00  Hour: 0
2020-05-07 01:00:00  Hour: 1
2020-05-07 02:00:00  Hour: 2
2020-05-07 03:00:00  Hour: 3
2020-05-07 04:00:00  Hour: 3
2020-05-07 04:00:00  Issue detected
2020-05-07 05:00:00  Hour: 5
2020-05-07 06:00:00  Hour: 6
2020-05-07 07:00:00  Hour: 7
2020-05-07 08:00:00  Hour: 8
2020-05-07 09:00:00  Hour: 9
2020-05-07 10:00:00  Hour: 10
2020-05-07 11:00:00  Hour: 11
2020-05-07 12:00:00  Hour: 11
2020-05-07 12:00:00  Issue detected
2020-05-07 13:00:00  Hour: 13
2020-05-07 14:00:00  Hour: 14
2020-05-07 15:00:00  Hour: 15
2020-05-07 16:00:00  Hour: 16
2020-05-07 17:00:00  Hour: 17
2020-05-07 18:00:00  Hour: 17
2020-05-07 18:00:00  Issue detected
2020-05-07 19:00:00  Hour: 19
2020-05-07 20:00:00  Hour: 20
2020-05-07 21:00:00  Hour: 21
2020-05-07 22:00:00  Hour: 22
2020-05-07 23:00:00  Hour: 23
2020-05-07 00:00:00  Hour: 0
2020-05-08 01:00:00  Hour: 1
2020-05-08 02:00:00  Hour: 2
2020-05-08 03:00:00  Hour: 2
2020-05-08 03:00:00  Issue detected
2020-05-08 04:00:00  Hour: 4
2020-05-08 05:00:00  Hour: 5
2020-05-08 06:00:00  Hour: 6
2020-05-08 07:00:00  Hour: 7
2020-05-08 08:00:00  Hour: 8
2020-05-08 09:00:00  Hour: 9
2020-05-08 10:00:00  Hour: 10
2020-05-08 11:00:00  Hour: 10
2020-05-08 11:00:00  Issue detected
2020-05-08 12:00:00  Hour: 12
2020-05-08 13:00:00  Hour: 13
2020-05-08 14:00:00  Hour: 14

Am I missing something? I can easily work around it, but I could see it stumping folks trying to build similar mechanisms!

Thanks!

I’d say that is expected behaviour. You get the “current” values, so if you do multiple calls at different times they are independent of each other.

You need to get one consistent snapshot (use now() ?) and pass that value to Hour/Minute

Example:
https://docs.particle.io/reference/device-os/firmware/boron/#hour-

1 Like

@jhwarren Welcome to the Particle Community!

As @evanroode has mentioned the ‘issue’ you are seeing is because you are reading the unix time twice and then occasionally seeing the two reads spanning a second change.

A better way is to read the unix timestamp once

Time_t time_now = Time.now();

Then

nowHour = Time.hour(time_now);
nowMin = Time.minute(time_now);

Also, for the publish this is better to avoid String() use.

char data[50];
snprintf(data, sizeof(data), "Hour: %02i", nowHour);
Particle.publish("log", data, PRIVATE);
2 Likes

Thanks @evanroode and @armor! That’s a great workaround.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.