Atmel Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Calendar service

See Quick start guide for Calendar service.

This is the common API for a calendar service. It provides functionality to convert UNIX timestamps to dates and back. It also provides functionality for calculating the difference between dates and converting timestamps to dates with different time zones and back.

Note
This module is a pure software module, and does not interface hardware calendar peripherals (e.g. the AST on UC3L)

Modules

 Quick Start Guide(s)
 In this section you can find a list of all Quick Start guides related to the Calendar service.
 

Data Structures

struct  calendar_date
 Calendar structure to hold a date. More...
 

Functions

void calendar_add_second_to_date (struct calendar_date *date)
 Increments a date with one second. More...
 
uint32_t calendar_date_to_timestamp (struct calendar_date *date)
 Convert a date to a UNIX timestamp. More...
 
uint32_t calendar_date_to_timestamp_tz (struct calendar_date *date, int8_t hour, uint8_t min)
 This function converts a date in a given time zone to a UNIX timestamp. More...
 
bool calendar_is_date_valid (struct calendar_date *date)
 Check if a date is valid. More...
 
void calendar_time_between_dates (struct calendar_date *date_end, struct calendar_date *date_start, struct calendar_date *date_out)
 This function calculates the time difference between to dates. More...
 
void calendar_timestamp_to_date (uint32_t timestamp, struct calendar_date *date_out)
 Convert a UNIX timestamp to a date. More...
 
void calendar_timestamp_to_date_tz (uint32_t timestamp, int8_t hour, uint8_t min, struct calendar_date *date_out)
 Convert a UNIX timestamp to a date in a given time zone. More...
 

void calendar_add_second_to_date ( struct calendar_date date)

Increments a date with one second.

This function will add one second to specified date. If second is 59, it will increment minute.

Parameters
dateThe date to add a second to

References Assert, calendar_add_minute_to_date(), calendar_is_date_valid(), and calendar_date::second.

uint32_t calendar_date_to_timestamp ( struct calendar_date date)

Convert a date to a UNIX timestamp.

Note
If date is invalid, timestamp 0 will be returned.
Parameters
dateDate
Returns
The corresponding UNIX timestamp
Return values
0if date is not valid

References calendar_is_date_valid(), calendar_leapyear(), calendar_yearsize(), calendar_date::date, EPOCH_YEAR, calendar_date::hour, calendar_date::minute, month, calendar_date::month, calendar_date::second, SECS_PER_DAY, SECS_PER_HOUR, SECS_PER_MINUTE, while(), and calendar_date::year.

Referenced by calendar_date_to_timestamp_tz(), and calendar_time_between_dates().

uint32_t calendar_date_to_timestamp_tz ( struct calendar_date date,
int8_t  hour,
uint8_t  min 
)

This function converts a date in a given time zone to a UNIX timestamp.

Note
If date is invalid, timestamp 0 will be returned.
Parameters
dateDate
hourHour offset from UTC (UTC-12 to UTC+14)
minMinute offset from UTC (0, 15, 30, 45)
Returns
The corresponding UNIX timestamp
Return values
0if date is not valid

References calendar_date_to_timestamp(), SECS_PER_HOUR, and SECS_PER_MINUTE.

bool calendar_is_date_valid ( struct calendar_date date)

Check if a date is valid.

Checks that number of seconds, minutes and hours is a valid value. Checks that number of days does not exceed number of days in current month. Checks that number of months is a valid value, and checks that year is between 1970 (epoch year) and 2106 (overflow year).

Parameters
*datethe date to check if valid

References calendar_leapyear(), calendar_date::date, EPOCH_YEAR, calendar_date::hour, calendar_date::minute, month, calendar_date::month, calendar_date::second, and calendar_date::year.

Referenced by calendar_add_second_to_date(), and calendar_date_to_timestamp().

void calendar_time_between_dates ( struct calendar_date date_end,
struct calendar_date date_start,
struct calendar_date date_out 
)

This function calculates the time difference between to dates.

The time difference is provided as number of years, months, days, hours, minutes and seconds between the dates. If end date is before start date, the dates are switched.

Parameters
date_endThe end date
date_startThe start date
date_outThe time between the dates

References calendar_date_to_timestamp(), calendar_leapyear(), calendar_date::date, calendar_date::hour, calendar_date::minute, month, calendar_date::month, calendar_date::second, temp, and calendar_date::year.

void calendar_timestamp_to_date ( uint32_t  timestamp,
struct calendar_date date_out 
)

Convert a UNIX timestamp to a date.

Finds the corresponding date and time for a UNIX timestamp.

Parameters
timestampUNIX timestamp
date_outDate to store result

References calendar_leapyear(), calendar_yearsize(), calendar_date::date, calendar_date::dayofweek, EPOCH_YEAR, calendar_date::hour, calendar_date::minute, month, calendar_date::month, calendar_date::second, SECS_PER_DAY, SECS_PER_HOUR, SECS_PER_MINUTE, and calendar_date::year.

Referenced by calendar_timestamp_to_date_tz().

void calendar_timestamp_to_date_tz ( uint32_t  timestamp,
int8_t  hour,
uint8_t  min,
struct calendar_date date_out 
)

Convert a UNIX timestamp to a date in a given time zone.

The provided UNIX timestamp is converted to the corresponding time in the provided time zone.

Parameters
timestampUNIX timestamp
hourHour offset from UTC (UTC-12 to UTC+14)
minMinute offset from UTC (0, 15, 30, 45)
date_outDate to store result

References calendar_timestamp_to_date(), SECS_PER_HOUR, and SECS_PER_MINUTE.