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