2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
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
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
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
42 /** Days of week (for debugging) */
43 static const char *weekdays[] = {
44 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
48 * Determine whether or not year is a leap year
50 * @v tm_year Years since 1900
51 * @v is_leap_year Year is a leap year
53 static int is_leap_year ( int tm_year ) {
56 if ( ( tm_year % 4 ) == 0 )
58 if ( ( tm_year % 100 ) == 0 )
60 if ( ( tm_year % 400 ) == 100 )
67 * Calculate number of leap years since 1900
69 * @v tm_year Years since 1900
70 * @v num_leap_years Number of leap years
72 static int leap_years_to_end ( int tm_year ) {
75 leap_years += ( tm_year / 4 );
76 leap_years -= ( tm_year / 100 );
77 leap_years += ( ( tm_year + 300 ) / 400 );
83 * Calculate day of week
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]
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;
96 return ( ( pseudo_year + leap_years_to_end ( pseudo_year ) +
97 offset[tm_mon] + tm_mday ) % 7 );
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 };
105 * Calculate seconds since the Epoch
107 * @v tm Broken-down time
108 * @ret time Seconds since the Epoch
110 time_t mktime ( struct tm *tm ) {
111 int days_since_epoch;
112 int seconds_since_day;
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 ) )
121 /* Calculate day of week */
122 tm->tm_wday = day_of_week ( tm->tm_year, tm->tm_mon, tm->tm_mday );
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 ) );
128 ( ( ( ( tm->tm_hour * 60 ) + tm->tm_min ) * 60 ) + tm->tm_sec );
129 seconds = ( ( ( ( time_t ) days_since_epoch ) * ( ( time_t ) 86400 ) ) +
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 );