iPXE
time.c File Reference

Date and time. More...

#include <time.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static int is_leap_year (int tm_year)
 Determine whether or not year is a leap year.
static int leap_years_to_end (int tm_year)
 Calculate number of leap years since 1900.
static int day_of_week (int tm_year, int tm_mon, int tm_mday)
 Calculate day of week.
time_t mktime (struct tm *tm)
 Calculate seconds since the Epoch.

Variables

signed long time_offset
 Current system clock offset.
static const char * weekdays []
 Days of week (for debugging)
static const uint16_t days_to_month_start []
 Days from start of year until start of months (in non-leap years)

Detailed Description

Date and time.

POSIX:2008 section 4.15 defines "seconds since the Epoch" as an abstract measure approximating the number of seconds that have elapsed since the Epoch, excluding leap seconds. The formula given is

tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 + (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 - ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400

This calculation assumes that leap years occur in each year that is either divisible by 4 but not divisible by 100, or is divisible by 400.

Definition in file time.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ is_leap_year()

int is_leap_year ( int tm_year)
static

Determine whether or not year is a leap year.

Parameters
tm_yearYears since 1900
is_leap_yearYear is a leap year

Definition at line 61 of file time.c.

61 {
62 int leap_year = 0;
63
64 if ( ( tm_year % 4 ) == 0 )
65 leap_year = 1;
66 if ( ( tm_year % 100 ) == 0 )
67 leap_year = 0;
68 if ( ( tm_year % 400 ) == 100 )
69 leap_year = 1;
70
71 return leap_year;
72}

Referenced by mktime().

◆ leap_years_to_end()

int leap_years_to_end ( int tm_year)
static

Calculate number of leap years since 1900.

Parameters
tm_yearYears since 1900
num_leap_yearsNumber of leap years

Definition at line 80 of file time.c.

80 {
81 int leap_years = 0;
82
83 leap_years += ( tm_year / 4 );
84 leap_years -= ( tm_year / 100 );
85 leap_years += ( ( tm_year + 300 ) / 400 );
86
87 return leap_years;
88}

Referenced by day_of_week(), and mktime().

◆ day_of_week()

int day_of_week ( int tm_year,
int tm_mon,
int tm_mday )
static

Calculate day of week.

Parameters
tm_yearYears since 1900
tm_monMonth of year [0,11]
tm_dayDay of month [1,31]

Definition at line 97 of file time.c.

97 {
98 static const uint8_t offset[12] =
99 { 1, 4, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
100 int pseudo_year = tm_year;
101
102 if ( tm_mon < 2 )
103 pseudo_year--;
104 return ( ( pseudo_year + leap_years_to_end ( pseudo_year ) +
105 offset[tm_mon] + tm_mday ) % 7 );
106}
unsigned char uint8_t
Definition stdint.h:10
uint16_t offset
Offset to command line.
Definition bzimage.h:3
static int leap_years_to_end(int tm_year)
Calculate number of leap years since 1900.
Definition time.c:80

References leap_years_to_end(), and offset.

Referenced by mktime().

◆ mktime()

time_t mktime ( struct tm * tm)

Calculate seconds since the Epoch.

Parameters
tmBroken-down time
Return values
timeSeconds since the Epoch

Definition at line 118 of file time.c.

118 {
119 int days_since_epoch;
120 int seconds_since_day;
122
123 /* Calculate day of year */
124 tm->tm_yday = ( ( tm->tm_mday - 1 ) +
126 if ( ( tm->tm_mon >= 2 ) && is_leap_year ( tm->tm_year ) )
127 tm->tm_yday++;
128
129 /* Calculate day of week */
131
132 /* Calculate seconds since the Epoch */
133 days_since_epoch = ( tm->tm_yday + ( 365 * tm->tm_year ) - 25567 +
134 leap_years_to_end ( tm->tm_year - 1 ) );
135 seconds_since_day =
136 ( ( ( ( tm->tm_hour * 60 ) + tm->tm_min ) * 60 ) + tm->tm_sec );
137 seconds = ( ( ( ( time_t ) days_since_epoch ) * ( ( time_t ) 86400 ) ) +
138 seconds_since_day );
139
140 DBGC ( &weekdays, "TIME %04d-%02d-%02d %02d:%02d:%02d => %lld (%s, "
141 "day %d)\n", ( tm->tm_year + 1900 ), ( tm->tm_mon + 1 ),
143 weekdays[ tm->tm_wday ], tm->tm_yday );
144
145 return seconds;
146}
#define DBGC(...)
Definition compiler.h:505
int64_t time_t
Seconds since the Epoch.
Definition time.h:19
UINT16_t seconds
Elapsed time.
Definition pxe_api.h:24
Broken-down time.
Definition time.h:16
int tm_mon
Month of year [0,11].
Definition time.h:26
int tm_year
Years since 1900.
Definition time.h:28
int tm_hour
Hour [0,23].
Definition time.h:22
int tm_sec
Seconds [0,60].
Definition time.h:18
int tm_yday
Day of year [0,365].
Definition time.h:32
int tm_mday
Day of month [1,31].
Definition time.h:24
int tm_min
Minutes [0,59].
Definition time.h:20
int tm_wday
Day of week [0,6] (Sunday=0)
Definition time.h:30
static const uint16_t days_to_month_start[]
Days from start of year until start of months (in non-leap years)
Definition time.c:109
static int day_of_week(int tm_year, int tm_mon, int tm_mday)
Calculate day of week.
Definition time.c:97
static int is_leap_year(int tm_year)
Determine whether or not year is a leap year.
Definition time.c:61
static const char * weekdays[]
Days of week (for debugging)
Definition time.c:51

References day_of_week(), days_to_month_start, DBGC, is_leap_year(), leap_years_to_end(), seconds, tm::tm_hour, tm::tm_mday, tm::tm_min, tm::tm_mon, tm::tm_sec, tm::tm_wday, tm::tm_yday, tm::tm_year, and weekdays.

Referenced by asn1_generalized_time(), efi_get_time(), and rtc_read_time().

Variable Documentation

◆ time_offset

signed long time_offset

Current system clock offset.

Definition at line 48 of file time.c.

Referenced by __attribute__(), and __attribute__().

◆ weekdays

const char* weekdays[]
static
Initial value:
= {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
}

Days of week (for debugging)

Definition at line 51 of file time.c.

51 {
52 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
53};

Referenced by mktime().

◆ days_to_month_start

const uint16_t days_to_month_start[]
static
Initial value:
=
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }

Days from start of year until start of months (in non-leap years)

Definition at line 109 of file time.c.

110 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };

Referenced by mktime().