# Set time via publish message

**URL:** <https://community.particle.io/t/set-time-via-publish-message/56935>\
**Category:** Cloud\
**Created:** [July 9, 2020, 9:02pm UTC](https://community.particle.io/t/set-time-via-publish-message/56935 "2020-07-09T21:02:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daneboomer](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/daneboomer/32/3653_2.png) [@daneboomer](https://community.particle.io/u/daneboomer)\
**Post date:** [July 9, 2020, 9:02pm UTC](https://community.particle.io/t/set-time-via-publish-message/56935/1 "2020-07-09T21:02:49Z")

</div>

Despite revisiting this issue time and time again and receiving some fantastic help from members I’ve never managed to get my Particle Core to maintain accurate time. It’s off by a minutes and obviously the drift only gets worse rather than better!

Just wondering - could anyone point me at a code snippet where I can tell my Core with a Particle Publish:  
“right, it’s 22:00 NOW” !!  
the Core then sets the time to 22:00.  
I’m aware that there will be a couple of seconds inaccuracy with this method (at best) but it’s a lot better than the couple of minutes issue I have at the moment!

thank you!!

---

<div class="post-metadata">

**Author:** ![picsil](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/picsil/32/21737_2.png) [@picsil](https://community.particle.io/u/picsil)\
**Post date:** [July 9, 2020, 9:29pm UTC](https://community.particle.io/t/set-time-via-publish-message/56935/2 "2020-07-09T21:29:52Z")

</div>

There are a couple of libraries available to sync time via NTP. If you need accurate time that’s the best route rather than using a cloud function or other means. You’ll get way more accurate time.

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [July 9, 2020, 9:56pm UTC](https://community.particle.io/t/set-time-via-publish-message/56935/3 "2020-07-09T21:56:23Z")

</div>

Using [Time.setTime()](https://docs.particle.io/reference/device-os/firmware/photon/#settime-) could work.

```auto
// Expect data to be unix timestamp
void timeHandler(const char *event, const char *data)
{
int timeFromData = atoi(data);
Time.setTime(timeFromData);
}

// In setup()
Particle.subscribe("setTime", timeHandler, MY_DEVICES);

```

Alternatively, you could make use of [Particle.syncTime()](https://docs.particle.io/reference/device-os/firmware/photon/#particle-synctime-) and call it routinely. This would be a more straightforward remedy and you wouldn’t need to manually sync the time with a publish.

---

<div class="post-metadata">

**Author:** ![daneboomer](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/daneboomer/32/3653_2.png) [@daneboomer](https://community.particle.io/u/daneboomer)\
**Post date:** [July 9, 2020, 10:10pm UTC](https://community.particle.io/t/set-time-via-publish-message/56935/4 "2020-07-09T22:10:44Z")

</div>

> [@nrobinson2000](#):
>
> Alternatively, you could make use of [Particle.syncTime()](https://docs.particle.io/reference/device-os/firmware/photon/#particle-synctime-) and call it routinely. This would be a more straightforward remedy and you wouldn’t need to manually sync the time with a publish.

Thanks, I've tried this, but it doesn't seem to work. I've had help from all sorts of forum superstars over the years but it's never worked. It might work on a Photon but I've not got it to work on a Core. It works once, but then never again.

Thanks for the code snippet, that sounds like it might do the job! I just send a unix timestamp with topic "setTime" ?

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [July 10, 2020, 12:21am UTC](https://community.particle.io/t/set-time-via-publish-message/56935/5 "2020-07-10T00:21:41Z")

</div>

> [@daneboomer](#):
>
> Thanks for the code snippet, that sounds like it might do the job! I just send a unix timestamp with topic “setTime” ?

Yup, that should do it.

---

<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:** [January 8, 2021, 12:21pm UTC](https://community.particle.io/t/set-time-via-publish-message/56935/6 "2021-01-08T12:21:51Z")

</div>

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