Setting time and date
The first step to be able to use the RTC is setting time and date. Some functions have been created for managing these operations.

Setting time and date

It sets time and date on the RTC.
Time and date are specified by inputs which correspond to the different variables: year, month, date, day, hour, minute and second.
day is the day of the week and the range for this value is 1 to 7. By default, Sunday is equal to 1 and Saturday is equal to 7.
The setTime() function extracts each part of date and time, storing each part in the corresponding variable. After this, the registersRTC array is loaded with these variables and data is sent to the RTC.
This function returns '0' if was successfully executed. In the case of any error '1' is returned.
Example of use:
1
{
2
// Setting date and time [yy:mm:dd:dow:hh:mm:ss]
3
RTC.setTime(“09:06:29:02:10:00:00”);
4
}
Copied!
Related variables:
year, month, hour, minute, second → stores the time and date set day → stores the day of the week date → stores the day of the month
An example of how to set RTC via USB port: https://development.libelium.com/rtc-07-set-waspmote-date/

Getting time and date

The getTime() function gets time and date, storing them in the registersRTC array and the corresponding variables. This function updates all API variables related to time and date:
  • RTC.year
  • RTC.month
  • RTC.date
  • RTC.day
  • RTC.hour
  • RTC.minute
  • RTC.second
It returns a string containing time and date in the following format: "Day of week, yy/mm/dd, hh:mm:ss". In the case an error happened, the"error" message is returned.
Example of use:
1
{
2
USB.println(RTC.getTime());
3
}
Copied!
Related variables:
year, month, hour, minute, second → stores the time and date set day → stores the day of the week date → stores the day of the month

Getting day of week

This function calculates the day of the week based on the year, month and day. Sakamoto's algorithm is used in this function. Valid for any date in the range [September 14, 1.752] - [December 31, 9.999].
The inputs for dow() function are: year, month and day of month.
Example of use:
1
{
2
uint8_t day_of_week = 0;
3
day_of_week = RTC.dow(2012,12,4);
4
}
Copied!
The function returns:
  1. 1.
    Sunday
  2. 2.
    Monday
  3. 3.
    Tuesday
  4. 4.
    Wednesday
  5. 5.
    Thursday
  6. 6.
    Friday
  7. 7.
    Saturday