leapc
Loading...
Searching...
No Matches
Functions
leap.c File Reference

Leap year function implementations. More...

#include "leap.h"
#include "quo_mod.h"
Include dependency graph for leap.c:

Go to the source code of this file.

Functions

bool is_leap (int year)
 Determine if a year is a leap year.
 
int leap_add (int year)
 Adds one for a leap year otherwise zero.
 
int leap_thru (int year)
 Leap years completed from year 0 up to but not including the first day of the specified year.
 
int leap_day (int year)
 Counts leap-adjusted days up to some year.
 
struct leap_off leap_off (int year, int day_off)
 Offsets year and day of year.
 
int leap_mday (int year, int month)
 Day of month from year and month.
 
int leap_yday (int year, int month)
 Day of year from year and month.
 
struct leap_date leap_date (int year, int day_off)
 Date from year and day of year.
 
struct leap_off leap_from (int year, int month, int day)
 Day from year, month and day of month.
 
struct leap_date leap_abs_date (int day_off)
 Absolute date from day of year.
 
int leap_abs_from (int year, int month, int day)
 Absolute date from year, month, and day of month.
 

Detailed Description

Leap year function implementations.

Implements functions to determine if a year is a leap year, count leap years up to a given year, and calculate leap-adjusted days.

Definition in file leap.c.

Function Documentation

◆ is_leap()

bool is_leap ( int  year)

Determine if a year is a leap year.

Is a year a leap year? A year is a leap year if it is divisible by four, except for years that are divisible by 100, unless they are also divisible by 400.

Parameters
yearThe year to check.
Return values
trueif the year is a leap year.
falseif the year is not a leap year.

Definition at line 13 of file leap.c.

◆ leap_abs_date()

struct leap_date leap_abs_date ( int  day_off)

Absolute date from day of year.

Converts a day offset (from year 0) into a (year, month, day) date. The absolute day is anchored to year 0 such that day 0 corresponds to 0-01-01, making leap_day(0) == 0. Absolute days differ from Unix epoch days (which start at 1970-01-01).

Parameters
day_offThe absolute day offset, starting from 0 for the first day of year 0.
Returns
The leap_date structure (year, month, day) corresponding to the absolute day.

Definition at line 135 of file leap.c.

◆ leap_abs_from()

int leap_abs_from ( int  year,
int  month,
int  day 
)

Absolute date from year, month, and day of month.

Converts a (year, month, day) date into an absolute day offset from year 0 such that day 0 corresponds to 0-01-01, making leap_day(0) == 0. The absolute day is anchored at year 0, not Unix epoch (1970-01-01).

Parameters
yearThe year.
monthThe month, starting from 1 for January.
dayDay of the month, starting from 1 for the first day of the month.
Returns
The absolute day offset from year 0.

Definition at line 137 of file leap.c.

◆ leap_add()

int leap_add ( int  year)

Adds one for a leap year otherwise zero.

Answers 1 if the year is a leap year, otherwise answers 0. This is the same result as is_leap() except that the result is an explicit integer; 1 for leap years, 0 for non-leap years.

Compute the number of days in a given year as:

365 + leap_add(year);
int leap_add(int year)
Adds one for a leap year otherwise zero.
Definition leap.c:28
Parameters
yearThe year to check.
Return values
1if the year is a leap year.
0if the year is not a leap year.

Definition at line 28 of file leap.c.

◆ leap_date()

struct leap_date leap_date ( int  year,
int  day_off 
)

Date from year and day of year.

Adjust the day so that it sits in-between 1 and 365 (or 366) inclusively. Returns the year, month, and day of the month for the given year and day of year. The day of the month is adjusted to be 1-based, meaning it starts from 1 for the first day of the month.

Parameters
yearThe year.
day_offDay of the year offset, starting from 0 for first of January.
Return values
leap_date.yearThe year.
leap_date.monthThe month, starting from 1 for January.
leap_date.dayThe day of the month, starting from 1 for the first day of the month.

Definition at line 104 of file leap.c.

◆ leap_day()

int leap_day ( int  year)

Counts leap-adjusted days up to some year.

Counts the number of days completed up to but not including the first day of the year.

Parameters
yearThe year to check.
Returns
The number of leap-adjust days completed up to but not including the first day of the given year.

Definition at line 48 of file leap.c.

◆ leap_from()

struct leap_off leap_from ( int  year,
int  month,
int  day 
)

Day from year, month and day of month.

Adjusts the month so that it sits in-between 1 and 12 inclusively. The month offsets the year. Returns the absolute date from the given year, month, and day of month.

Parameters
yearThe year.
monthThe month, starting from 1 for January.
dayThe one-based day of the month, starting from 1 for the first day of the month.
Returns
The absolute date as the number of days since the epoch.

Definition at line 129 of file leap.c.

◆ leap_mday()

int leap_mday ( int  year,
int  month 
)

Day of month from year and month.

Returns the number of days in the month for the given year and month. Accounts for leap years in February.

Parameters
yearThe year.
monthThe month ordinal, starting from 1 for January.
Return values
Thenumber of days in the month, accounting for leap years in February.

Definition at line 80 of file leap.c.

◆ leap_off()

struct leap_off leap_off ( int  year,
int  day_off 
)

Offsets year and day of year.

Year and day of year from year and day of year. Adjust the day so that it sits in-between 0 and 365 or 366, inclusively.

The day adjusts to be within the range of 0 to 365 or 366 inclusively, and the year is adjusted accordingly.

This function ensures that the day of the year does not exceed the number of days in the year, accounting for leap years. If the day is negative or exceeds the number of days in the year, it adjusts the year and day accordingly. The day is adjusted to be 0-based, meaning it starts from 0 for the first day of the year. The year is adjusted to account for the number of days in the year, which can be either 365 or 366 depending on whether it is a leap year.

Parameters
yearThe year.
day_offThe day of the year offset, starting from 0. It can be negative or exceed the number of days in the year.
Return values
leap_off.yearThe year adjusted to account for the number of days in the year. The resulting year becomes normalised according to the day adjustment whether positive or negative.
leap_off.dayThe adjusted day of the year, starting from 0 for the first day. The day is guaranteed to be within the bounds of the year. 0 <= day < 365 or 366 depending on whether it is a leap year.

Definition at line 70 of file leap.c.

◆ leap_thru()

int leap_thru ( int  year)

Leap years completed from year 0 up to but not including the first day of the specified year.

Counts the number of leap years that have occurred from year 0 up to but not including the first day of the given year. This is calculated as the number of years divisible by 4, minus those divisible by 100, plus those divisible by 400. This accounts for the rules of leap years in the Gregorian calendar.

Parameters
yearThe target year up to which leap years should be counted.
Returns
The total number of leap years from year 0 through the specified year.

Definition at line 30 of file leap.c.

◆ leap_yday()

int leap_yday ( int  year,
int  month 
)

Day of year from year and month.

Returns the day of the year for the given year and month. The day of the year is calculated by summing the days in the months up to the given month, and adding an extra day if the month is after February in a leap year.

Parameters
yearThe year.
monthThe month ordinal, starting from 1 for January.
Return values
Theday of the year, starting from 0 for first of January.

Definition at line 86 of file leap.c.