These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / core / time.c
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
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <time.h>
27
28 /** @file
29  *
30  * Date and time
31  *
32  * POSIX:2008 section 4.15 defines "seconds since the Epoch" as an
33  * abstract measure approximating the number of seconds that have
34  * elapsed since the Epoch, excluding leap seconds.  The formula given
35  * is
36  *
37  *    tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
38  *    (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
39  *    ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
40  *
41  * This calculation assumes that leap years occur in each year that is
42  * either divisible by 4 but not divisible by 100, or is divisible by
43  * 400.
44  */
45
46 /** Days of week (for debugging) */
47 static const char *weekdays[] = {
48         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
49 };
50
51 /**
52  * Determine whether or not year is a leap year
53  *
54  * @v tm_year           Years since 1900
55  * @v is_leap_year      Year is a leap year
56  */
57 static int is_leap_year ( int tm_year ) {
58         int leap_year = 0;
59
60         if ( ( tm_year % 4 ) == 0 )
61                 leap_year = 1;
62         if ( ( tm_year % 100 ) == 0 )
63                 leap_year = 0;
64         if ( ( tm_year % 400 ) == 100 )
65                 leap_year = 1;
66
67         return leap_year;
68 }
69
70 /**
71  * Calculate number of leap years since 1900
72  *
73  * @v tm_year           Years since 1900
74  * @v num_leap_years    Number of leap years
75  */
76 static int leap_years_to_end ( int tm_year ) {
77         int leap_years = 0;
78
79         leap_years += ( tm_year / 4 );
80         leap_years -= ( tm_year / 100 );
81         leap_years += ( ( tm_year + 300 ) / 400 );
82
83         return leap_years;
84 }
85
86 /**
87  * Calculate day of week
88  *
89  * @v tm_year           Years since 1900
90  * @v tm_mon            Month of year [0,11]
91  * @v tm_day            Day of month [1,31]
92  */
93 static int day_of_week ( int tm_year, int tm_mon, int tm_mday ) {
94         static const uint8_t offset[12] =
95                 { 1, 4, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
96         int pseudo_year = tm_year;
97
98         if ( tm_mon < 2 )
99                 pseudo_year--;
100         return ( ( pseudo_year + leap_years_to_end ( pseudo_year ) +
101                    offset[tm_mon] + tm_mday ) % 7 );
102 }
103
104 /** Days from start of year until start of months (in non-leap years) */
105 static const uint16_t days_to_month_start[] =
106         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
107
108 /**
109  * Calculate seconds since the Epoch
110  *
111  * @v tm                Broken-down time
112  * @ret time            Seconds since the Epoch
113  */
114 time_t mktime ( struct tm *tm ) {
115         int days_since_epoch;
116         int seconds_since_day;
117         time_t seconds;
118
119         /* Calculate day of year */
120         tm->tm_yday = ( ( tm->tm_mday - 1 ) +
121                         days_to_month_start[ tm->tm_mon ] );
122         if ( ( tm->tm_mon >= 2 ) && is_leap_year ( tm->tm_year ) )
123                 tm->tm_yday++;
124
125         /* Calculate day of week */
126         tm->tm_wday = day_of_week ( tm->tm_year, tm->tm_mon, tm->tm_mday );
127
128         /* Calculate seconds since the Epoch */
129         days_since_epoch = ( tm->tm_yday + ( 365 * tm->tm_year ) - 25567 +
130                              leap_years_to_end ( tm->tm_year - 1 ) );
131         seconds_since_day =
132                 ( ( ( ( tm->tm_hour * 60 ) + tm->tm_min ) * 60 ) + tm->tm_sec );
133         seconds = ( ( ( ( time_t ) days_since_epoch ) * ( ( time_t ) 86400 ) ) +
134                     seconds_since_day );
135
136         DBGC ( &weekdays, "TIME %04d-%02d-%02d %02d:%02d:%02d => %lld (%s, "
137                "day %d)\n", ( tm->tm_year + 1900 ), ( tm->tm_mon + 1 ),
138                tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, seconds,
139                weekdays[ tm->tm_wday ], tm->tm_yday );
140
141         return seconds;
142 }