I have a new Real Time Clock library for Spark available that I hope everybody will find useful until the team gives us access to the on-chip RTC. This library provides a real-time clock function by getting the current time from an NTP server and then using the millis() function to add to that initial time. The base NTP time is updated periodically so the clock does not drift much. In my testing, the clock is accurate to within one second of real time.
The library is available here: https://github.com/bkobkobko/SparkCoreLibraries
The library obeys the NTP kiss-of-death and tries to back-off when a NTP server complains you are using it too much. The NTP update rate is once per hour by default but you can set this to as low as five minutes. In my testing, once per hour or once per day are good choices. The library uses pool.ntp.org so that no single host gets over used. For those of you with your own online hosts, If you can contribute a host to be in the NTP pool, everyone would benefit!
The library provides time and date values as unsigned integers like other time libraries, but also provides a nice selection of Arduino String return values. You can get both 12- and 24-hour formats. This makes it super simple to build up your own time formats! The library also handles time zones and daylight savings time for the US and European rules. You can, of course, turn off daylight savings time and set the time zone to zero, if you need to.
For those of you looking for a time and date stamp for your sensor data, you will find two pre-formatted Strings, one with time zone taken in to account and one that returns UTC or Zulu time, which I recommend. See the example program for details.
There is a sample program in the git, but here’s a simple example using Arduino Strings to give you the flavor of it:
unsigned long currentTime = rtc.now(); String timeStr; timeStr += rtc.hour12String(currentTime); timeStr += ":"; timeStr += rtc.minuteString(currentTime); timeStr += ":"; timeStr += rtc.secondString(currentTime); timeStr += " "; timeStr += rtc.AMPMString(currentTime);
timeStr would then have a value something like “12:09:58 PM” since the strings provide nice leading-zeros.
I am sure there will be bugs! Please feel free to post me here or put in a git issue. There is not a lot of doc right now, but I am slowly writing something up.