Microchip® Advanced Software Framework

calendar.c File Reference

Calendar service.

Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.

#include "compiler.h"
#include "calendar.h"
#include <stdio.h>

Macros

#define EPOCH_YEAR   1970
 Unix epoch year. More...
 
#define SECS_PER_DAY   86400UL
 Number of seconds in a day. More...
 
#define SECS_PER_HOUR   3600UL
 Number of seconds in an hour. More...
 
#define SECS_PER_MINUTE   60UL
 Number of seconds in a minute. More...
 

Functions

static void calendar_add_day_to_date (struct calendar_date *date)
 Add a day to a date. More...
 
static void calendar_add_hour_to_date (struct calendar_date *date)
 Add an hour to a date. More...
 
static void calendar_add_minute_to_date (struct calendar_date *date)
 Add a minute to a date. More...
 
static void calendar_add_month_to_date (struct calendar_date *date)
 Add a month to a date. More...
 
void calendar_add_second_to_date (struct calendar_date *date)
 Increments a date with one second. More...
 
static void calendar_add_year_to_date (struct calendar_date *date)
 Add a year to a date. 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...
 
static bool calendar_leapyear (uint16_t year)
 Check if a year is a leap year. 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...
 
static uint16_t calendar_yearsize (uint16_t year)
 Find number of days in a year. More...
 

Variables

const uint8_t month [2][12]
 Number of days in a specified month. Index 1 for leap year, else 0. More...
 

#define EPOCH_YEAR   1970
#define SECS_PER_DAY   86400UL

Number of seconds in a day.

Referenced by calendar_date_to_timestamp(), and calendar_timestamp_to_date().

#define SECS_PER_HOUR   3600UL
#define SECS_PER_MINUTE   60UL

static void calendar_add_day_to_date ( struct calendar_date date)
static

Add a day to a date.

Adds one day to specified date. If day is the last of the month, increment month.

Parameters
*datethe date to add a day to

References calendar_add_month_to_date(), calendar_leapyear(), calendar_date::date, calendar_date::dayofweek, month, calendar_date::month, and calendar_date::year.

Referenced by calendar_add_hour_to_date().

static void calendar_add_hour_to_date ( struct calendar_date date)
static

Add an hour to a date.

Adds one hour to specified date. If hour is 23, increment day.

Parameters
*datethe date to add an hour to

References calendar_add_day_to_date(), and calendar_date::hour.

Referenced by calendar_add_minute_to_date().

static void calendar_add_minute_to_date ( struct calendar_date date)
static

Add a minute to a date.

Adds one minute to specified date. If minute is 59, increment hour.

Parameters
*datethe date to add a minute to

References calendar_add_hour_to_date(), and calendar_date::minute.

Referenced by calendar_add_second_to_date().

static void calendar_add_month_to_date ( struct calendar_date date)
static

Add a month to a date.

Adds one month to specified date. If month is december, increment year.

Parameters
*datethe date to add a month to

References calendar_add_year_to_date(), and calendar_date::month.

Referenced by calendar_add_day_to_date().

static void calendar_add_year_to_date ( struct calendar_date date)
static

Add a year to a date.

Adds one year to specified date as long as the current year is before 2105.

Parameters
*datethe date to add a year to

References calendar_date::year.

Referenced by calendar_add_month_to_date().

static bool calendar_leapyear ( uint16_t  year)
static

Check if a year is a leap year.

Returns true or false depending if the input year is a leap year or not.

Parameters
yearthe year in format YYYY to check for leap year or not
Return values
trueif the year is a leap year
falseif the year is not a leap year

Referenced by calendar_add_day_to_date(), calendar_date_to_timestamp(), calendar_is_date_valid(), calendar_time_between_dates(), calendar_timestamp_to_date(), and calendar_yearsize().

static uint16_t calendar_yearsize ( uint16_t  year)
static

Find number of days in a year.

Returns the number of days in a year, depending if the input is leap year or not.

Parameters
yearthe year in format YYYY to check number of days
Return values
366if the year is a leap year
365if the year is not a leap year

References calendar_leapyear().

Referenced by calendar_date_to_timestamp(), and calendar_timestamp_to_date().

const uint8_t month[2][12]
Initial value:
= {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
}

Number of days in a specified month. Index 1 for leap year, else 0.

Referenced by calendar_add_day_to_date(), calendar_date_to_timestamp(), calendar_is_date_valid(), calendar_time_between_dates(), and calendar_timestamp_to_date().