Feature Request: get current timestamp over cloud

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. :sunny:

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

The feature has been implemented! :smile: http://docs.spark.io/firmware/#libraries-time

4 Likes

+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.

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

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

I meant to say Unix time earlier… It’s easy enough to convert Unix time to UTC 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?

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

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

@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}

1 Like

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

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.

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. :smiley:

sure. I am accessing the webservice API of the local public transportation, which returns 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

Okay, ignore the first question about 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. :slight_smile:

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

I understand now. :clock230:

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

3 Likes

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.

3 Likes

Until the feature is ready, i use timeapi.org

here is my example code: https://github.com/synox/spark-core-examples/blob/master/http_timeapi.cpp

1 Like

@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.

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…

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 :smile:

1 Like

Nice work @Coffee! I was using 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.