Date and Time Library
Modern MCUs include a RTC, or Real Time Clock, that keeps date and time. The MSP432, CC3200, TM4C123 and TM4C129 include high-level commands to manage the RTC.
RTC can be powered by a battery but unfortunately, battery line is hard-linked to main supply on the LaunchPads. With a board with WiFi capabilities like the CC3200, the library gets the exact time from Internet using NTP or Network Time Protocol. The Date and Time Library provides an object that encapsulates the most commonly used functions. |
Time Formats: Epoch and Structure
There are two commonly used formats to represent the time: epoch and structure.
|
|
The Date and Time Library provides two utilities to convert from one format to another: convertEpoch2Structure() and convertStructure2Epoch(), with self explanatory names. Both functions are based on the <time.h> library and take both formats, epoch and structure.
|
Similarly, the Date and Time Library includes functions to display the date as a string: stringDateTime() and formatStringDateTime().
The latter uses format codes which are detailed here. Both functions are based on the <time.h> library and take both formats, epoch and structure. |
Using the RTC
The clock myRTC is declared as a DateTime variable, and then initialised with begin().
Optionally, the time-zone is set with setTimeZone(), here with tz_CEST for Central Europe Savings Time. The setTime() function sets the date and time to the clock based on an epoch time. By convention, the reference time kept in the MCU's RTC is always GMT. The getTime() function gets the date and time from the clock as an epoch time, which is converted into as string with stringDateTime(). To get the local time, i.e. CEST, use getLocalTime(). |
Getting Date and Time from Internet with NTP
Additionally, the CC3200 LaunchPad and the CC3100 BoosterPack can read the official time from a time server using NTP or Network Time Protocol, thanks to a WiFi connection. One example of time sever is the National Institute of Standards and Technology, or NIST.
|
The getTimeNTP() function reads date and time from the specified NTP server. If no NTP server is specified, the function uses wwv.nist.gov mentioned earlier.
if getTimeNTP() is successful, the function updates the myEpochNTP variable and returns true. Each 10 seconds, NTP time is checked again and compared to RTC. RTC may drift up to 4 seconds a day. The library is based on the example provided with Energia 16. |
Download
The library was developed with embedXcode and has been tested on Energia 16 against MSP432 EMT, CC3200 non-EMT, TM4C123 and TM4C129.
NTP compilation was successful against CC3200 non-EMT but, for an unknown reason, failed against CC3200 EMT. |
Posted: Jul 31, 2015 — Edited: Apr 02, 2016