libcproject
C static library easier to use than libc (C standard library).
date.h File Reference
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "convert.h"
#include "mathematics.h"
#include "string.h"
#include "types.h"
Include dependency graph for date.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  date
 Date object representing a single moment in time. More...
 

Macros

#define SECONDS_PER_MINUTE   60
 
#define SECONDS_PER_HOUR   (60 * SECONDS_PER_MINUTE)
 
#define SECONDS_PER_DAY   (24 * SECONDS_PER_HOUR)
 
#define MILLISECONDS_PER_SECOND   1000
 

Functions

struct datedate_copy (struct date *date_to_copy)
 Return the copy of a date. More...
 
bool date_get_is_valid_year (uint16_t year)
 Check if a year is valid, between [0, 9999] (inclusive). More...
 
bool date_get_is_valid_month (uint8_t month)
 Check if a month is valid, between [1, 12] (inclusive). More...
 
bool date_get_is_valid_day (uint8_t day)
 Check if a day is valid, between [1, 31] (inclusive). More...
 
bool date_get_is_valid_hours (uint8_t hours)
 Check if hours are valid, between [0, 23] (inclusive). More...
 
bool date_get_is_valid_minutes (uint8_t minutes)
 Check if minutes are valid, between [0, 59] (inclusive). More...
 
bool date_get_is_valid_seconds (uint8_t seconds)
 Check if seconds are valid, between [0, 59] (inclusive). More...
 
bool date_get_is_valid_milliseconds (uint16_t milliseconds)
 Check if milliseconds are valid, between [0, 999] (inclusive). More...
 
bool date_get_is_valid_timezone_utc_offset (int8_t timezone_utc_offset)
 Check if the timezone UTC offset is valid, between [-12, 14] (inclusive). More...
 
bool date_get_is_valid (struct date *date)
 Check if the date is valid (all fields are possible). More...
 
string_t date_to_iso_string (struct date *date)
 String representing the date in the date time string format, a simplified format based on ISO 8601, which is always 24 characters long (YYYY-MM-DDTHH:mm:ss.sssZ). The timezone is always UTC, as denoted by the suffix Z. More...
 
string_t date_to_iso_string_without_time (struct date *date)
 String representing the date in the ISO 8601 format, without time information (YYYY-MM-DD). More...
 
struct datedate_from_iso_string (string_t iso_string)
 Create a date from an ISO 8601 string, with the format YYYY-MM-DDTHH:mm:ss.sssZ. More...
 
uint8_t date_get_days_of_month (uint8_t month, uint16_t year)
 Get number of days in one month [1, 12]. More...
 
bool date_get_is_leap_year (uint16_t year)
 Determine if a year is a leap year. More...
 
uint64_t date_convert_milliseconds_to_seconds (uint16_t milliseconds)
 Convert milliseconds to seconds. More...
 
uint64_t date_convert_seconds_to_milliseconds (uint64_t seconds)
 Convert seconds to milliseconds. More...
 
uint64_t date_convert_days_to_seconds (uint64_t days)
 Convert days to seconds. More...
 
uint64_t date_convert_hms_to_seconds (uint8_t hours, uint8_t minutes, uint8_t seconds)
 Convert hours, minutes, and seconds to seconds. More...
 
uint64_t date_to_total_seconds (struct date *date)
 Convert a date to total seconds. More...
 
uint64_t date_duration_seconds_between_2_dates (struct date *date1, struct date *date2)
 Calculate the duration in seconds between 2 dates. More...
 
void date_add_hours (struct date *date, int64_t hours)
 Add hours to the date, managing the day, month, year changes if necessary. More...
 
void date_add_days (struct date *date, int64_t days)
 Adds days to the date, managing month and year changes as needed. More...
 
void date_to_utc (struct date *date)
 Transform the date with a Timezone UTC Offset to UTC (timezone_utc_offset = 0). More...
 

Data Structure Documentation

◆ date

struct date

Date object representing a single moment in time.

Since
v4.3.0

Definition at line 24 of file date.h.

Data Fields
uint16_t year

Year. Between [0, 9999] (inclusive). As per ISO 8601, a four-digit year [YYYY] and represents years from 0000 to 9999, year 0000 being equal to 1 BC and all others AD.

uint8_t month

Month. Between [1, 12] (inclusive).

uint8_t day

Day. Between [1, 31] (inclusive).

uint8_t hours

Hours. Between [0, 23] (inclusive).

uint8_t minutes

Minutes. Between [0, 59] (inclusive).

uint8_t seconds

Seconds. Between [0, 59] (inclusive).

uint16_t milliseconds

Milliseconds. Between [0, 999] (inclusive).

int8_t timezone_utc_offset

Timezone UTC offset. Between [-12, 14]

Macro Definition Documentation

◆ SECONDS_PER_MINUTE

#define SECONDS_PER_MINUTE   60

Definition at line 15 of file date.h.

◆ SECONDS_PER_HOUR

#define SECONDS_PER_HOUR   (60 * SECONDS_PER_MINUTE)

Definition at line 16 of file date.h.

◆ SECONDS_PER_DAY

#define SECONDS_PER_DAY   (24 * SECONDS_PER_HOUR)

Definition at line 17 of file date.h.

◆ MILLISECONDS_PER_SECOND

#define MILLISECONDS_PER_SECOND   1000

Definition at line 18 of file date.h.

Function Documentation

◆ date_copy()

struct date* date_copy ( struct date date_to_copy)

Return the copy of a date.

Parameters
date
Returns
struct date*
Since
v4.3.0

◆ date_get_is_valid_year()

bool date_get_is_valid_year ( uint16_t  year)

Check if a year is valid, between [0, 9999] (inclusive).

Parameters
year
Returns
bool
Since
v4.3.0

◆ date_get_is_valid_month()

bool date_get_is_valid_month ( uint8_t  month)

Check if a month is valid, between [1, 12] (inclusive).

Parameters
month
Returns
bool
Since
v4.3.0

◆ date_get_is_valid_day()

bool date_get_is_valid_day ( uint8_t  day)

Check if a day is valid, between [1, 31] (inclusive).

Parameters
day
Returns
bool
Since
v4.3.0

◆ date_get_is_valid_hours()

bool date_get_is_valid_hours ( uint8_t  hours)

Check if hours are valid, between [0, 23] (inclusive).

Parameters
hours
Returns
bool
Since
v4.3.0

◆ date_get_is_valid_minutes()

bool date_get_is_valid_minutes ( uint8_t  minutes)

Check if minutes are valid, between [0, 59] (inclusive).

Parameters
minutes
Returns
bool
Since
v4.3.0

◆ date_get_is_valid_seconds()

bool date_get_is_valid_seconds ( uint8_t  seconds)

Check if seconds are valid, between [0, 59] (inclusive).

Parameters
seconds
Returns
bool
Since
v4.3.0

◆ date_get_is_valid_milliseconds()

bool date_get_is_valid_milliseconds ( uint16_t  milliseconds)

Check if milliseconds are valid, between [0, 999] (inclusive).

Parameters
milliseconds
Returns
bool
Since
v4.3.0

◆ date_get_is_valid_timezone_utc_offset()

bool date_get_is_valid_timezone_utc_offset ( int8_t  timezone_utc_offset)

Check if the timezone UTC offset is valid, between [-12, 14] (inclusive).

Parameters
timezone_utc_offset
Returns
bool
Since
v4.3.0

◆ date_get_is_valid()

bool date_get_is_valid ( struct date date)

Check if the date is valid (all fields are possible).

Parameters
date
Returns
bool
Since
v4.3.0

◆ date_to_iso_string()

string_t date_to_iso_string ( struct date date)

String representing the date in the date time string format, a simplified format based on ISO 8601, which is always 24 characters long (YYYY-MM-DDTHH:mm:ss.sssZ). The timezone is always UTC, as denoted by the suffix Z.

Parameters
date
Returns
string_t
date_to_iso_string() // "2024-09-11T09:39:18.203Z"
string_t date_to_iso_string(struct date *date)
String representing the date in the date time string format, a simplified format based on ISO 8601,...
Since
v4.3.0

◆ date_to_iso_string_without_time()

string_t date_to_iso_string_without_time ( struct date date)

String representing the date in the ISO 8601 format, without time information (YYYY-MM-DD).

Parameters
date
Returns
string_t
string_t date_to_iso_string_without_time(struct date *date)
String representing the date in the ISO 8601 format, without time information (YYYY-MM-DD).
Since
v4.3.0

◆ date_from_iso_string()

struct date* date_from_iso_string ( string_t  iso_string)

Create a date from an ISO 8601 string, with the format YYYY-MM-DDTHH:mm:ss.sssZ.

The timezone is always UTC, as denoted by the suffix Z.

Parameters
iso_string
Returns
struct date*
Since
v4.3.0

◆ date_get_days_of_month()

uint8_t date_get_days_of_month ( uint8_t  month,
uint16_t  year 
)

Get number of days in one month [1, 12].

Parameters
month
Returns
uint8_t
Since
v4.3.0

◆ date_get_is_leap_year()

bool date_get_is_leap_year ( uint16_t  year)

Determine if a year is a leap year.

Parameters
year
Returns
bool
date_is_leap_year(2020) // true
date_is_leap_year(2021) // false
date_is_leap_year(2022) // false
date_is_leap_year(2023) // false
date_is_leap_year(2024) // true
Since
v4.3.0

◆ date_convert_milliseconds_to_seconds()

uint64_t date_convert_milliseconds_to_seconds ( uint16_t  milliseconds)

Convert milliseconds to seconds.

Parameters
milliseconds
Returns
uint64_t
Since
v4.3.0

◆ date_convert_seconds_to_milliseconds()

uint64_t date_convert_seconds_to_milliseconds ( uint64_t  seconds)

Convert seconds to milliseconds.

Parameters
seconds
Returns
uint64_t
Since
v4.3.0

◆ date_convert_days_to_seconds()

uint64_t date_convert_days_to_seconds ( uint64_t  days)

Convert days to seconds.

Parameters
days
Returns
uint64_t
Since
v4.3.0

◆ date_convert_hms_to_seconds()

uint64_t date_convert_hms_to_seconds ( uint8_t  hours,
uint8_t  minutes,
uint8_t  seconds 
)

Convert hours, minutes, and seconds to seconds.

Parameters
hours
minutes
seconds
Returns
uint64_t
Since
v4.3.0

◆ date_to_total_seconds()

uint64_t date_to_total_seconds ( struct date date)

Convert a date to total seconds.

Parameters
date
Returns
uint64_t
Since
v4.3.0

◆ date_duration_seconds_between_2_dates()

uint64_t date_duration_seconds_between_2_dates ( struct date date1,
struct date date2 
)

Calculate the duration in seconds between 2 dates.

Parameters
date1
date2
Returns
uint64_t
Since
v4.3.0

◆ date_add_hours()

void date_add_hours ( struct date date,
int64_t  hours 
)

Add hours to the date, managing the day, month, year changes if necessary.

NOTE: Mutates the date.

Parameters
date
hours
Since
v4.3.0

◆ date_add_days()

void date_add_days ( struct date date,
int64_t  days 
)

Adds days to the date, managing month and year changes as needed.

NOTE: Mutates the date.

Parameters
dateThe date to which days are being added.
daysThe number of days to add.
Since
v4.3.0

◆ date_to_utc()

void date_to_utc ( struct date date)

Transform the date with a Timezone UTC Offset to UTC (timezone_utc_offset = 0).

NOTE: Mutates the date.

Parameters
date
Since
v4.3.0