RTC Programming Guide
Development website
  • Initial page
  • Introduction
  • Initialization
  • Setting time and date
  • Setting Alarms
  • RTC Watchdog
  • Using RTC with hibernate
  • Unix / Epoch time
  • Code examples and extended information
  • API Changelog
Powered by GitBook
On this page
  • Setting time and date
  • Getting time and date
  • Getting day of week

Was this helpful?

Setting time and date

PreviousInitializationNextSetting Alarms

Last updated 4 years ago

Was this helpful?

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:

{
 // Setting date and time [yy:mm:dd:dow:hh:mm:ss]
 RTC.setTime(“09:06:29:02:10:00:00”);
}

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 the functions:

An example of how to set RTC via USB port:

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:

{
USB.println(RTC.getTime());
}

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:

{
 uint8_t day_of_week = 0;
 day_of_week = RTC.dow(2012,12,4);
}

The function returns:

  1. Sunday

  2. Monday

  3. Tuesday

  4. Wednesday

  5. Thursday

  6. Friday

  7. Saturday

An example of the functions:

https://development.libelium.com/rtc-01-setting-and-reading-time/
https://development.libelium.com/rtc-07-set-waspmote-date/
https://development.libelium.com/rtc-01-setting-and-reading-time/