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

**URL:** <https://community.particle.io/t/issue-time-minute-rolls-over-before-time-hour/56064>\
**Category:** Firmware\
**Tags:** argon\
**Created:** [May 8, 2020, 8:21pm UTC](https://community.particle.io/t/issue-time-minute-rolls-over-before-time-hour/56064 "2020-05-08T20:21:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhwarren](https://avatars.discourse-cdn.com/v4/letter/j/97f17d/32.png) [@jhwarren](https://community.particle.io/u/jhwarren)\
**Post date:** [May 8, 2020, 8:21pm UTC](https://community.particle.io/t/issue-time-minute-rolls-over-before-time-hour/56064/1 "2020-05-08T20:21:55Z")

</div>

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

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![evanroode](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/evanroode/32/19308_2.png) [@evanroode](https://community.particle.io/u/evanroode)\
**Post date:** [May 8, 2020, 11:12pm UTC](https://community.particle.io/t/issue-time-minute-rolls-over-before-time-hour/56064/2 "2020-05-08T23:12:13Z")

</div>

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-](https://docs.particle.io/reference/device-os/firmware/boron/#hour-)

---

<div class="post-metadata">

**Author:** ![armor](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/armor/32/18398_2.png) [@armor](https://community.particle.io/u/armor)\
**Post date:** [May 9, 2020, 1:04pm UTC](https://community.particle.io/t/issue-time-minute-rolls-over-before-time-hour/56064/3 "2020-05-09T13:04:51Z")

</div>

@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

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![jhwarren](https://avatars.discourse-cdn.com/v4/letter/j/97f17d/32.png) [@jhwarren](https://community.particle.io/u/jhwarren)\
**Post date:** [May 9, 2020, 8:13pm UTC](https://community.particle.io/t/issue-time-minute-rolls-over-before-time-hour/56064/4 "2020-05-09T20:13:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/6/4/64635adccc35238caa19e9b553ec3e98b163d026.png) [@system](https://community.particle.io/u/system)\
**Post date:** [November 8, 2020, 8:13am UTC](https://community.particle.io/t/issue-time-minute-rolls-over-before-time-hour/56064/5 "2020-11-08T08:13:07Z")

</div>

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