summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Brown2012-03-19 17:09:41 +0100
committerMichael Brown2012-03-19 18:35:46 +0100
commitbd6805a8c18f859a8f467965f2ee780817d8a81e (patch)
treed228abb380a1acf56ccadce6ea98de2918696631
parent[crypto] Use correct constraint for byte-addressable register (diff)
downloadipxe-bd6805a8c18f859a8f467965f2ee780817d8a81e.tar.gz
ipxe-bd6805a8c18f859a8f467965f2ee780817d8a81e.tar.xz
ipxe-bd6805a8c18f859a8f467965f2ee780817d8a81e.zip
[libc] Add mktime() function
Signed-off-by: Michael Brown <mcb30@ipxe.org>
-rw-r--r--src/core/time.c137
-rw-r--r--src/include/sys/time.h24
-rw-r--r--src/include/time.h37
3 files changed, 173 insertions, 25 deletions
diff --git a/src/core/time.c b/src/core/time.c
new file mode 100644
index 00000000..52ae3ee9
--- /dev/null
+++ b/src/core/time.c
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+#include <time.h>
+
+/** @file
+ *
+ * Date and time
+ *
+ * POSIX:2008 section 4.15 defines "seconds since the Epoch" as an
+ * abstract measure approximating the number of seconds that have
+ * elapsed since the Epoch, excluding leap seconds. The formula given
+ * is
+ *
+ * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
+ * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
+ * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
+ *
+ * This calculation assumes that leap years occur in each year that is
+ * either divisible by 4 but not divisible by 100, or is divisible by
+ * 400.
+ */
+
+/** Days of week (for debugging) */
+static const char *weekdays[] = {
+ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
+};
+
+/**
+ * Determine whether or not year is a leap year
+ *
+ * @v tm_year Years since 1900
+ * @v is_leap_year Year is a leap year
+ */
+static int is_leap_year ( int tm_year ) {
+ int leap_year = 0;
+
+ if ( ( tm_year % 4 ) == 0 )
+ leap_year = 1;
+ if ( ( tm_year % 100 ) == 0 )
+ leap_year = 0;
+ if ( ( tm_year % 400 ) == 100 )
+ leap_year = 1;
+
+ return leap_year;
+}
+
+/**
+ * Calculate number of leap years since 1900
+ *
+ * @v tm_year Years since 1900
+ * @v num_leap_years Number of leap years
+ */
+static int leap_years_to_end ( int tm_year ) {
+ int leap_years = 0;
+
+ leap_years += ( tm_year / 4 );
+ leap_years -= ( tm_year / 100 );
+ leap_years += ( ( tm_year + 300 ) / 400 );
+
+ return leap_years;
+}
+
+/**
+ * Calculate day of week
+ *
+ * @v tm_year Years since 1900
+ * @v tm_mon Month of year [0,11]
+ * @v tm_day Day of month [1,31]
+ */
+static int day_of_week ( int tm_year, int tm_mon, int tm_mday ) {
+ static const uint8_t offset[12] =
+ { 1, 4, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
+ int pseudo_year = tm_year;
+
+ if ( tm_mon < 2 )
+ pseudo_year--;
+ return ( ( pseudo_year + leap_years_to_end ( pseudo_year ) +
+ offset[tm_mon] + tm_mday ) % 7 );
+}
+
+/** Days from start of year until start of months (in non-leap years) */
+static const uint16_t days_to_month_start[] =
+ { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
+
+/**
+ * Calculate seconds since the Epoch
+ *
+ * @v tm Broken-down time
+ * @ret time Seconds since the Epoch
+ */
+time_t mktime ( struct tm *tm ) {
+ int days_since_epoch;
+ int seconds_since_day;
+ time_t seconds;
+
+ /* Calculate day of year */
+ tm->tm_yday = ( ( tm->tm_mday - 1 ) +
+ days_to_month_start[ tm->tm_mon ] );
+ if ( ( tm->tm_mon >= 2 ) && is_leap_year ( tm->tm_year ) )
+ tm->tm_yday++;
+
+ /* Calculate day of week */
+ tm->tm_wday = day_of_week ( tm->tm_year, tm->tm_mon, tm->tm_mday );
+
+ /* Calculate seconds since the Epoch */
+ days_since_epoch = ( tm->tm_yday + ( 365 * tm->tm_year ) - 25567 +
+ leap_years_to_end ( tm->tm_year - 1 ) );
+ seconds_since_day =
+ ( ( ( ( tm->tm_hour * 60 ) + tm->tm_min ) * 60 ) + tm->tm_sec );
+ seconds = ( ( ( ( time_t ) days_since_epoch ) * ( ( time_t ) 86400 ) ) +
+ seconds_since_day );
+
+ DBGC ( &weekdays, "TIME %04d-%02d-%02d %02d:%02d:%02d => %lld (%s, "
+ "day %d)\n", ( tm->tm_year + 1900 ), ( tm->tm_mon + 1 ),
+ tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, seconds,
+ weekdays[ tm->tm_wday ], tm->tm_yday );
+
+ return seconds;
+}
diff --git a/src/include/sys/time.h b/src/include/sys/time.h
index 21fb7e99..6c9b7421 100644
--- a/src/include/sys/time.h
+++ b/src/include/sys/time.h
@@ -1,20 +1,18 @@
#ifndef _SYS_TIME_H
#define _SYS_TIME_H
-#include <time.h>
+/** @file
+ *
+ * Date and time
+ */
-typedef unsigned long suseconds_t;
+#include <stdint.h>
-struct timeval {
- time_t tv_sec; /* seconds */
- suseconds_t tv_usec; /* microseconds */
-};
-
-struct timezone {
- int tz_minuteswest; /* minutes W of Greenwich */
- int tz_dsttime; /* type of dst correction */
-};
-
-extern int gettimeofday ( struct timeval *tv, struct timezone *tz );
+/** Seconds since the Epoch
+ *
+ * We use a 64-bit type to avoid Y2K38 issues, since we may have to
+ * handle distant future dates (e.g. X.509 certificate expiry dates).
+ */
+typedef int64_t time_t;
#endif /* _SYS_TIME_H */
diff --git a/src/include/time.h b/src/include/time.h
index 6ea927c3..f33300ab 100644
--- a/src/include/time.h
+++ b/src/include/time.h
@@ -1,22 +1,35 @@
#ifndef _TIME_H
#define _TIME_H
-typedef unsigned long time_t;
+/** @file
+ *
+ * Date and time
+ */
+#include <sys/time.h>
+
+/** Broken-down time */
struct tm {
- int tm_sec; /* seconds */
- int tm_min; /* minutes */
- int tm_hour; /* hours */
- int tm_mday; /* day of the month */
- int tm_mon; /* month */
- int tm_year; /* year */
- int tm_wday; /* day of the week */
- int tm_yday; /* day in the year */
- int tm_isdst; /* daylight saving time */
+ /** Seconds [0,60] */
+ int tm_sec;
+ /** Minutes [0,59] */
+ int tm_min;
+ /** Hour [0,23] */
+ int tm_hour;
+ /** Day of month [1,31] */
+ int tm_mday;
+ /** Month of year [0,11] */
+ int tm_mon;
+ /** Years since 1900 */
+ int tm_year;
+ /** Day of week [0,6] (Sunday=0) */
+ int tm_wday;
+ /** Day of year [0,365] */
+ int tm_yday;
+ /** Daylight savings flag */
+ int tm_isdst;
};
-extern time_t time ( time_t *t );
-
extern time_t mktime ( struct tm *tm );
#endif /* _TIME_H */