iPXE
time.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <assert.h>
28#include <time.h>
29
30/** @file
31 *
32 * Date and time
33 *
34 * POSIX:2008 section 4.15 defines "seconds since the Epoch" as an
35 * abstract measure approximating the number of seconds that have
36 * elapsed since the Epoch, excluding leap seconds. The formula given
37 * is
38 *
39 * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
40 * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
41 * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
42 *
43 * This calculation assumes that leap years occur in each year that is
44 * either divisible by 4 but not divisible by 100, or is divisible by
45 * 400.
46 */
47
48/** Current system clock offset */
49signed long time_offset;
50
51/** Days of week (for debugging) */
52static const char *weekdays[] = {
53 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
54};
55
56/**
57 * Determine whether or not year is a leap year
58 *
59 * @v tm_year Years since 1900
60 * @v is_leap_year Year is a leap year
61 */
62static int is_leap_year ( int tm_year ) {
63 int leap_year = 0;
64
65 if ( ( tm_year % 4 ) == 0 )
66 leap_year = 1;
67 if ( ( tm_year % 100 ) == 0 )
68 leap_year = 0;
69 if ( ( tm_year % 400 ) == 100 )
70 leap_year = 1;
71
72 return leap_year;
73}
74
75/**
76 * Calculate number of leap years since 1900
77 *
78 * @v tm_year Years since 1900
79 * @v num_leap_years Number of leap years
80 */
81static int leap_years_to_end ( int tm_year ) {
82 int leap_years = 0;
83
84 leap_years += ( tm_year / 4 );
85 leap_years -= ( tm_year / 100 );
86 leap_years += ( ( tm_year + 300 ) / 400 );
87
88 return leap_years;
89}
90
91/**
92 * Calculate day of week
93 *
94 * @v tm_year Years since 1900
95 * @v tm_mon Month of year [0,11]
96 * @v tm_day Day of month [1,31]
97 */
98static int day_of_week ( int tm_year, int tm_mon, int tm_mday ) {
99 static const uint8_t offset[12] =
100 { 1, 4, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
101 int pseudo_year = tm_year;
102
103 if ( tm_mon < 2 )
104 pseudo_year--;
105 return ( ( pseudo_year + leap_years_to_end ( pseudo_year ) +
106 offset[tm_mon] + tm_mday ) % 7 );
107}
108
109/** Days from start of year until start of months (in non-leap years) */
111 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
112
113/**
114 * Calculate seconds since the Epoch
115 *
116 * @v tm Broken-down time
117 * @ret time Seconds since the Epoch
118 */
119time_t mktime ( struct tm *tm ) {
120 int tm_year = tm->tm_year;
121 int tm_mon = tm->tm_mon;
122 int tm_mday = tm->tm_mday;
123 int tm_hour = tm->tm_hour;
124 int tm_min = tm->tm_min;
125 int tm_sec = tm->tm_sec;
126 int tm_yday;
127 int tm_wday;
128 int days_since_epoch;
129 int seconds_since_day;
131
132 /* Normalize month for use as an array index */
133 tm_year += ( tm_mon / 12 );
134 tm_mon = ( tm_mon % 12 );
135 if ( tm_mon < 0 ) {
136 tm_year--;
137 tm_mon += 12;
138 }
139 assert ( tm_mon >= 0 );
140 assert ( tm_mon < 12 );
141
142 /* Calculate day of year */
143 tm_yday = ( ( tm_mday - 1 ) + days_to_month_start[tm_mon] );
144 if ( ( tm_mon >= 2 ) && is_leap_year ( tm_year ) )
145 tm_yday++;
146 tm->tm_yday = tm_yday;
147
148 /* Calculate day of week */
149 tm_wday = day_of_week ( tm_year, tm_mon, tm_mday );
150 tm->tm_wday = tm_wday;
151
152 /* Calculate seconds since the Epoch */
153 days_since_epoch = ( tm_yday + ( 365 * tm_year ) - 25567 +
154 leap_years_to_end ( tm_year - 1 ) );
155 seconds_since_day =
156 ( ( ( ( tm_hour * 60 ) + tm_min ) * 60 ) + tm_sec );
157 seconds = ( ( ( ( time_t ) days_since_epoch ) * ( ( time_t ) 86400 ) )
158 + seconds_since_day );
159
160 DBGC ( &weekdays, "TIME %04d-%02d-%02d %02d:%02d:%02d => %lld (%s, "
161 "day %d)\n", ( tm->tm_year + 1900 ), ( tm->tm_mon + 1 ),
163 weekdays[ tm->tm_wday ], tm->tm_yday );
164
165 return seconds;
166}
unsigned short uint16_t
Definition stdint.h:11
unsigned char uint8_t
Definition stdint.h:10
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
uint16_t offset
Offset to command line.
Definition bzimage.h:3
#define DBGC(...)
Definition compiler.h:530
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
int64_t time_t
Seconds since the Epoch.
Definition time.h:19
Date and time.
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:110
static int day_of_week(int tm_year, int tm_mon, int tm_mday)
Calculate day of week.
Definition time.c:98
static int leap_years_to_end(int tm_year)
Calculate number of leap years since 1900.
Definition time.c:81
signed long time_offset
Current system clock offset.
Definition time.c:49
static int is_leap_year(int tm_year)
Determine whether or not year is a leap year.
Definition time.c:62
static const char * weekdays[]
Days of week (for debugging).
Definition time.c:52
time_t mktime(struct tm *tm)
Calculate seconds since the Epoch.
Definition time.c:119