Feature Request: get current timestamp over cloud

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:


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.


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.

Hi @timb,

We have a task to add a time library, which I think would either grab the time from the server, or easily wrap NTP requests to a user-specified server. I don’t think we’ve had a chance to work on it yet, so it’s still just in the backlog. I think having some code to make syncing the clock easier would be awesome.

@Coffee Thanks for sharing this! @BDub, I actually had to comment line number #89!


Thanks for the help guys!

Okay, cool. I’ll play around with it a bit once I get these I2C changes done. (Which, by the way, is going great! I’m looking into using DMA for I2C now, to increase the stability.)

1 Like

Wow you guys are fast with testing. :blush: I had the solution ready, but forget to push it last night :stuck_out_tongue:

The Problem was, that tzset() used 60KB of memory. So the total of 110k was too much for the core. It was a dirty notworking hack anyway.

The new version gets the unixTimestamp from the server, instead of parsing it offline.

I added the example for parsing a date here:

1 Like

Hi, did you have any success using DMA yet? I’d like to use DMA (and an IRQ routine to handle the Transfer Complete Interrupt) for SPI. So if you already got something working for I2C, that would be a great starting point.

@luz Yeah, there should be some specific registers for DMA SPI transfer you need to use. Let me go through my notes and pull out the relevant info for you. I’ll also share my I2C code so you can get an idea of the steps you’ll need to take. :smile:

Thanks! The reference manual for the STM32F10xxx is pretty clear about the channel setup procedure, however the things I don’t know is how to actually code that in the spark environment. I have no idea what headers the web IDE includes, and which symbols I could access to get to those registers. Same for the TCI IRQ setup - I have no pointers yet about how to install it from the web IDE C environment.

Of course, seeing a piece of code setting up a DMA channel (any channel, I can figure out SPI details then) would be awesome!

BTW: do you know which DMA channels are in use by the spark core firmware itself?

Hi @Coffee were you able to run the code? I try to push it over but for me is not working. I created a Spark variable in order to read it with a Get request but I’m getting 0.

#include "application.h"

// ------------- HTTP functions --------------


  • make http request and return body
    TCPClient client;
    char buffer[512];
    String http_get(char const
    hostname, String path) {

     if (client.connect(hostname, 80)) {
             client.print("GET ");
             client.print(" HTTP/1.0\n");
             client.print("HOST: ");
             //        client.print("Connection: close\n\n");
     } else {

// Serial.println(“connection failed”);
return NULL;

    // Block until first byte is read.
    for (unsigned int i = 0; i < sizeof(buffer) && client.available(); i++) {
            char c = client.read();
            if (c == -1) {
            buffer[i] = c;

    String response(buffer);
    int bodyPos = response.indexOf("\r\n\r\n");
    if (bodyPos == -1) {

// Serial.println(“can not find http reponse body”);
return NULL;
return response.substring(bodyPos + 4);

// ------------- DATE / TIME functions --------------


  • returns current time since epoche, from a http server.
    long currentUnixTimestamp() {
    // Serial.println(“getting current time”);

     String response = http_get("www.timeapi.org", "/utc/now?\\s");
     if (response != NULL) {

// Serial.print(“timeapi time=”);
// Serial.println(response);
return atoi(response.c_str());
} else {
return 0;

/*timeapi example :
GET /utc/now?\s HTTP/1.0
Host: www.timeapi.org
Connection: close

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 11 Jan 2014 11:59:37 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 25
Connection: keep-alive
X-Frame-Options: sameorigin
X-Xss-Protection: 1; mode=block


int currenttime;
long timestamp;
void setup() {
// Serial.begin(9600);

unsigned int nextTime = 0; // next time to contact the server
void loop() {
if (nextTime > millis()) {

    timestamp = currentUnixTimestamp();

// Serial.print("time: ");
// Serial.println(timestamp);

    nextTime = millis() + 10000;


Also, did you compile the time.h library into the core?

Thanks man!

there is an issue with http, see here: https://community.spark.io/t/tcpclient-available-does-not-return-length/1169/8

there is an example where i use time.h

You are amazing @Coffee , great work on it! I will try this ASAP :slight_smile:

1 Like

I have posted a new time library for :spark: over here:


You can get a UTC (Zulu) time and date stamp in ISO format as an Arduino String like this:


You can get an ISO format time and date stamp with timezone and daylight savings too, but the UTC version is likely what you really want.


This is awesome @bko. I was getting current time over GET from my server using Node.js…this might be a little more accurate :slight_smile:

This post was flagged by the community and is temporarily hidden.