iPXE
time.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <time.h>
00027 
00028 /** @file
00029  *
00030  * Date and time
00031  *
00032  * POSIX:2008 section 4.15 defines "seconds since the Epoch" as an
00033  * abstract measure approximating the number of seconds that have
00034  * elapsed since the Epoch, excluding leap seconds.  The formula given
00035  * is
00036  *
00037  *    tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
00038  *    (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
00039  *    ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
00040  *
00041  * This calculation assumes that leap years occur in each year that is
00042  * either divisible by 4 but not divisible by 100, or is divisible by
00043  * 400.
00044  */
00045 
00046 /** Current system clock offset */
00047 signed long time_offset;
00048 
00049 /** Days of week (for debugging) */
00050 static const char *weekdays[] = {
00051         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
00052 };
00053 
00054 /**
00055  * Determine whether or not year is a leap year
00056  *
00057  * @v tm_year           Years since 1900
00058  * @v is_leap_year      Year is a leap year
00059  */
00060 static int is_leap_year ( int tm_year ) {
00061         int leap_year = 0;
00062 
00063         if ( ( tm_year % 4 ) == 0 )
00064                 leap_year = 1;
00065         if ( ( tm_year % 100 ) == 0 )
00066                 leap_year = 0;
00067         if ( ( tm_year % 400 ) == 100 )
00068                 leap_year = 1;
00069 
00070         return leap_year;
00071 }
00072 
00073 /**
00074  * Calculate number of leap years since 1900
00075  *
00076  * @v tm_year           Years since 1900
00077  * @v num_leap_years    Number of leap years
00078  */
00079 static int leap_years_to_end ( int tm_year ) {
00080         int leap_years = 0;
00081 
00082         leap_years += ( tm_year / 4 );
00083         leap_years -= ( tm_year / 100 );
00084         leap_years += ( ( tm_year + 300 ) / 400 );
00085 
00086         return leap_years;
00087 }
00088 
00089 /**
00090  * Calculate day of week
00091  *
00092  * @v tm_year           Years since 1900
00093  * @v tm_mon            Month of year [0,11]
00094  * @v tm_day            Day of month [1,31]
00095  */
00096 static int day_of_week ( int tm_year, int tm_mon, int tm_mday ) {
00097         static const uint8_t offset[12] =
00098                 { 1, 4, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
00099         int pseudo_year = tm_year;
00100 
00101         if ( tm_mon < 2 )
00102                 pseudo_year--;
00103         return ( ( pseudo_year + leap_years_to_end ( pseudo_year ) +
00104                    offset[tm_mon] + tm_mday ) % 7 );
00105 }
00106 
00107 /** Days from start of year until start of months (in non-leap years) */
00108 static const uint16_t days_to_month_start[] =
00109         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
00110 
00111 /**
00112  * Calculate seconds since the Epoch
00113  *
00114  * @v tm                Broken-down time
00115  * @ret time            Seconds since the Epoch
00116  */
00117 time_t mktime ( struct tm *tm ) {
00118         int days_since_epoch;
00119         int seconds_since_day;
00120         time_t seconds;
00121 
00122         /* Calculate day of year */
00123         tm->tm_yday = ( ( tm->tm_mday - 1 ) +
00124                         days_to_month_start[ tm->tm_mon ] );
00125         if ( ( tm->tm_mon >= 2 ) && is_leap_year ( tm->tm_year ) )
00126                 tm->tm_yday++;
00127 
00128         /* Calculate day of week */
00129         tm->tm_wday = day_of_week ( tm->tm_year, tm->tm_mon, tm->tm_mday );
00130 
00131         /* Calculate seconds since the Epoch */
00132         days_since_epoch = ( tm->tm_yday + ( 365 * tm->tm_year ) - 25567 +
00133                              leap_years_to_end ( tm->tm_year - 1 ) );
00134         seconds_since_day =
00135                 ( ( ( ( tm->tm_hour * 60 ) + tm->tm_min ) * 60 ) + tm->tm_sec );
00136         seconds = ( ( ( ( time_t ) days_since_epoch ) * ( ( time_t ) 86400 ) ) +
00137                     seconds_since_day );
00138 
00139         DBGC ( &weekdays, "TIME %04d-%02d-%02d %02d:%02d:%02d => %lld (%s, "
00140                "day %d)\n", ( tm->tm_year + 1900 ), ( tm->tm_mon + 1 ),
00141                tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, seconds,
00142                weekdays[ tm->tm_wday ], tm->tm_yday );
00143 
00144         return seconds;
00145 }