# Feature Request: get current timestamp over cloud

**URL:** <https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197>\
**Category:** Firmware\
**Created:** [January 3, 2014, 1:42am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197 "2014-01-03T01:42:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Coffee](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/coffee/32/1989_2.png) [@Coffee](https://community.particle.io/u/Coffee)\
**Post date:** [January 3, 2014, 1:42am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/1 "2014-01-03T01:42:20Z")

</div>

Hi!  
I want to calculate the difference of a timestamp to the current time.

I couldn’t find an API how to get the current time. I think this could be a call to the cloud, like `Spark.currentTimestamp()`

This would be helpful. ☀

Duplicate of Topic: [https://community.spark.io/t/can-spark-get-the-time-date-from-the-cloud-server/723](https://community.spark.io/t/can-spark-get-the-time-date-from-the-cloud-server/723)

The feature has been implemented! 😄 [http://docs.spark.io/firmware/#libraries-time](http://docs.spark.io/firmware/#libraries-time)

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [January 3, 2014, 2:14am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/2 "2014-01-03T02:14:32Z")

</div>

+1

This would be a super simple way of getting the current UTC (EDIT: I mean Unix time) … much easier than setting up all of code to request the time from an NTP server.

---

<div class="post-metadata">

**Author:** ![wgbartley](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/wgbartley/32/1910_2.png) [@wgbartley](https://community.particle.io/u/wgbartley)\
**Post date:** [January 3, 2014, 4:19am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/3 "2014-01-03T04:19:05Z")

</div>

Maybe you could even pass an argument of string or signed integer indicating the time zone or offset?

---

<div class="post-metadata">

**Author:** ![timb](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/timb/32/1913_2.png) [@timb](https://community.particle.io/u/timb)\
**Post date:** [January 3, 2014, 5:38am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/4 "2014-01-03T05:38:42Z")

</div>

I’m working on a basic API to get the current time. Do you want an early version of it to test with?

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [January 3, 2014, 7:34am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/5 "2014-01-03T07:34:37Z")

</div>

I meant to say Unix time earlier… It’s easy enough to [convert Unix time to UTC](http://www.epochconverter.com/programming/functions-c.php) and add in your offset plus daylight savings… The spark server can easily summon up the Unix time and dish it out, simpleness. And if you know your offset already, might as well use your distributed 32-bit 72MHz power to add it locally and spare the spark server a few cycles x 1,000,000 users xD

I wouldn’t mind trying it @timb, it’s for a Core right not the Spark server?

---

<div class="post-metadata">

**Author:** ![Coffee](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/coffee/32/1989_2.png) [@Coffee](https://community.particle.io/u/Coffee)\
**Post date:** [January 3, 2014, 8:55am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/6 "2014-01-03T08:55:40Z")

</div>

HI @timb, yeah, i would like to test it.

But I also need a parser for a date string, any idea for that?

---

<div class="post-metadata">

**Author:** ![timb](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/timb/32/1913_2.png) [@timb](https://community.particle.io/u/timb)\
**Post date:** [January 3, 2014, 8:56am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/7 "2014-01-03T08:56:04Z")

</div>

@BDub Correct, it’s a class for the Spark Core that will return the the time, date or both in whatever timezone you specify.

_getNetTime(UTC, 24, HHMMSS, “:”);_

1. Three Letter Time Zone Code
2. 24 or 12 Hour Format
3. Return Time Format: HHMMSS, HMS, HHMM, HM, etc.
4. Separator Format (Leave Blank for None)

There will also be a _getNetEpoch();_ and _getNetDate();_ with similar style arguments for the date.

It’s been a bitch to work on this since TCPClient is so unstable. {Flashes New Code} {Factory Restore} {Register New Core} {Reset Core Several Times, Reload Spark Build} {Flashes New Code} {Rinse} {Lather} {Repeat}

---

<div class="post-metadata">

**Author:** ![timb](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/timb/32/1913_2.png) [@timb](https://community.particle.io/u/timb)\
**Post date:** [January 3, 2014, 8:59am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/8 "2014-01-03T08:59:18Z")

</div>

@Coffee What sort of parsing and/or output format would you need for a _getDate_ call?

---

<div class="post-metadata">

**Author:** ![Coffee](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/coffee/32/1989_2.png) [@Coffee](https://community.particle.io/u/Coffee)\
**Post date:** [January 3, 2014, 9:03am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/9 "2014-01-03T09:03:28Z")

</div>

From a webservice I receive the string `2014-01-03T10:02:00+0100`, and I need to convert it to unixtime, so I can compare it with the current time.

---

<div class="post-metadata">

**Author:** ![timb](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/timb/32/1913_2.png) [@timb](https://community.particle.io/u/timb)\
**Post date:** [January 3, 2014, 9:21am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/10 "2014-01-03T09:21:32Z")

</div>

How are you getting the current time? Can you give me a bit more detail on exactly what you’re trying to accomplish, I might have an easier way. 😃

---

<div class="post-metadata">

**Author:** ![Coffee](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/coffee/32/1989_2.png) [@Coffee](https://community.particle.io/u/Coffee)\
**Post date:** [January 3, 2014, 9:43am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/11 "2014-01-03T09:43:46Z")

</div>

sure. I am accessing the [webservice API of the local public transportation](http://transport.opendata.ch/), which [returns](http://transport.opendata.ch/v1/connections?from=Wabern,Gurtenbahn&to=Bern&fields%5B%5D=connections/from/departure) that the next bus will depart at

> 2014-01-03T10:02:00+0100

Then I want to calculate the number of seconds until departure. The Spark Core will make some blinking when it's time to leave the house and catch the bus.

I don't know [Timeapi.org](http://Timeapi.org)

---

<div class="post-metadata">

**Author:** ![timb](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/timb/32/1913_2.png) [@timb](https://community.particle.io/u/timb)\
**Post date:** [January 3, 2014, 10:06am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/12 "2014-01-03T10:06:16Z")

</div>

Okay, ignore the first question about [TimeAPI.org](http://TimeAPI.org), that was meant for another post and got accidentally pasted in.

So essentially, you just want a notification X minutes before Y time?

What if I had a function called _netTime.setAlarm(0, “2014-01-03T10:02:00+0100”, -15)_ [Argument #1: Alarm Number, #2: Alarm Time, #3: Offset to Trigger On]; there would be a _netTime.getAlarm(0)_ [#1: Alarm Number to Check] that would return a _true_ or _false_ boolean value depending on if the alarm had triggered. Would that suffice?

See, right now there’s no built-in timekeeping functions on the Core. There is a RTC onboard, but the Spark Team doesn’t have it quite implemented yet, though it is on their list! Anyway, since there’s no onboard timekeeping right now, you’d need to check the internet for the current time on _every_ pass of _void loop()_, which would create a ton of traffic and make things quite slow.

So, I could implement the above alarm functions by pulling time from the internet then using a “Soft RTC” to countdown in the background (I’d recheck the internet time every 10 minutes or so to manage the drift).

I should have my library done by the end of the weekend, but once I get the basics going I can work on the alarm functions and let you start testing things. 🙂

---

<div class="post-metadata">

**Author:** ![Coffee](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/coffee/32/1989_2.png) [@Coffee](https://community.particle.io/u/Coffee)\
**Post date:** [January 3, 2014, 11:40am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/13 "2014-01-03T11:40:39Z")

</div>

I am sorry, I didn’t explain the whole project, let me clarify.

1. I get the dext departure from a webservice (2014-01-03T10:02:00+0100)
2. Calculate difference to current time
3. Set the led color depending on the time difference
4. wait x seconds, then repeat at 1.

Pseudo-code:

```
loop () {
    if ( less than 30 seconds passed since last call) 
      return;
    
    connection = TransportAPI.getNextConnection("Bern");
    unixTime = parseDate(connection.departure);
    now = currentTimeUnixTS();
    diff = unixTime - now;
    if ( diff < 60 ) 
      setLedColor(red);
    else if ( diff < 120 ) 
      setLedColor(orange);
    else if ( diff < 300 ) 
      setLedColor(green);
    }
}

```

So I think `netTime.setAlarm()` might be useful somewhere else, but not to determine the led color. Is it clear now?

The current implementation which uses a php-script to parse the date-string can be found here: [https://github.com/synox/Spark-Busstop-Light](https://github.com/synox/Spark-Busstop-Light)

---

<div class="post-metadata">

**Author:** ![timb](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/timb/32/1913_2.png) [@timb](https://community.particle.io/u/timb)\
**Post date:** [January 3, 2014, 11:52am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/14 "2014-01-03T11:52:12Z")

</div>

I understand now. 🕝

I can give you the logic to do that, I’ve even got most of it written! I’ll post it this afternoon. 🙂

---

<div class="post-metadata">

**Author:** ![will](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/will/32/23374_2.png) [@will](https://community.particle.io/u/will)\
**Post date:** [January 3, 2014, 9:12pm UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/15 "2014-01-03T21:12:49Z")

</div>

Hey everyone,

Thanks for the input. Just want you all to know that at least some eyes here at Spark saw this request and know how helpful it would be to streamline this for everyone. I’ve added it to my community feedback notes and will make sure it gets brought up during our next Sprint Planning meeting.

---

<div class="post-metadata">

**Author:** ![Coffee](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/coffee/32/1989_2.png) [@Coffee](https://community.particle.io/u/Coffee)\
**Post date:** [January 11, 2014, 6:04pm UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/16 "2014-01-11T18:04:23Z")

</div>

Until the feature is ready, i use [timeapi.org](http://timeapi.org)

here is my example code: [https://github.com/synox/spark-core-examples/blob/master/http\_timeapi.cpp](https://github.com/synox/spark-core-examples/blob/master/http_timeapi.cpp)

---

<div class="post-metadata">

**Author:** ![Iv4n](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/iv4n/32/1968_2.png) [@Iv4n](https://community.particle.io/u/Iv4n)\
**Post date:** [January 12, 2014, 5:32am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/17 "2014-01-12T05:32:49Z")

</div>

@Coffee Hey there man, does that code actually works for you? I’ve tried flashing it (copy/paste) no modifications at all and it doesn’t flash it, although it says it does… The spark core just doesn’t receives it, doesn’t restart or anything at all, if I however change line #27 (“long timestamp = currentTimeMilis();”) to “long timestamp = 0;” it does get flashed. Weird.

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [January 12, 2014, 6:40am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/18 "2014-01-12T06:40:36Z")

</div>

Seems like it’s a silent failure with the compiler!

I traced it down to commenting out line 88:

```
setenv("TZ", "UTC", 1);

```

But I have no idea why it’s failing to compile.

Lol line 90:

```
// sy/11.Jan.2014: Not sure if setenv works at all...

```

EDIT: I also had to change line 115 to:  
`String response = "";//http_get("www.timeapi.org", "/utc/now");`  
To even get it to the parseDate() function before having other issues with setenv();

So I’m not sure what’s going on in there either…

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [January 12, 2014, 6:48am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/19 "2014-01-12T06:48:05Z")

</div>

Popping in to say I added this to the compiler backlog, thanks!  
Edit: by that I mean figuring out why “setenv” caused the code to not compile 😄

---

<div class="post-metadata">

**Author:** ![timb](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/timb/32/1913_2.png) [@timb](https://community.particle.io/u/timb)\
**Post date:** [January 12, 2014, 7:06am UTC](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197/20 "2014-01-12T07:06:26Z")

</div>

Nice work @Coffee! I was using [TimeAPI.org](http://TimeAPI.org) in my netTime Library as well, but I just haven’t had time to work on it. I started working on it, then decided I wanted to add some LCD support which had me debugging I2C issues, which in turn got me working on JTAG stuff.

@Dave Is anyone on the team working on adding support for the RTC? I’ve been adding some notes about it as I go through the datasheets.

[Next page](https://community.particle.io/t/feature-request-get-current-timestamp-over-cloud/1197.md?page=2)
