소스 검색

[libc] Add mktime() function

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 년 전
부모
커밋
bd6805a8c1
3개의 변경된 파일173개의 추가작업 그리고 25개의 파일을 삭제
  1. 137
    0
      src/core/time.c
  2. 11
    13
      src/include/sys/time.h
  3. 25
    12
      src/include/time.h

+ 137
- 0
src/core/time.c 파일 보기

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

+ 11
- 13
src/include/sys/time.h 파일 보기

@@ -1,20 +1,18 @@
1 1
 #ifndef _SYS_TIME_H
2 2
 #define _SYS_TIME_H
3 3
 
4
-#include <time.h>
4
+/** @file
5
+ *
6
+ * Date and time
7
+ */
5 8
 
6
-typedef unsigned long suseconds_t;
9
+#include <stdint.h>
7 10
 
8
-struct timeval {
9
-	time_t tv_sec;		/* seconds */
10
-	suseconds_t tv_usec;	/* microseconds */
11
-};
12
-
13
-struct timezone {
14
-	int tz_minuteswest;	/* minutes W of Greenwich */
15
-	int tz_dsttime;		/* type of dst correction */
16
-};
17
-
18
-extern int gettimeofday ( struct timeval *tv, struct timezone *tz );
11
+/** Seconds since the Epoch
12
+ *
13
+ * We use a 64-bit type to avoid Y2K38 issues, since we may have to
14
+ * handle distant future dates (e.g. X.509 certificate expiry dates).
15
+ */
16
+typedef int64_t time_t;
19 17
 
20 18
 #endif /* _SYS_TIME_H */

+ 25
- 12
src/include/time.h 파일 보기

@@ -1,22 +1,35 @@
1 1
 #ifndef _TIME_H
2 2
 #define _TIME_H
3 3
 
4
-typedef unsigned long time_t;
4
+/** @file
5
+ *
6
+ * Date and time
7
+ */
5 8
 
9
+#include <sys/time.h>
10
+
11
+/** Broken-down time */
6 12
 struct tm {
7
-	int tm_sec;	/* seconds */
8
-	int tm_min;	/* minutes */
9
-	int tm_hour;	/* hours */
10
-	int tm_mday;	/* day of the month */
11
-	int tm_mon;	/* month */
12
-	int tm_year;	/* year */
13
-	int tm_wday;	/* day of the week */
14
-	int tm_yday;	/* day in the year */
15
-	int tm_isdst;	/* daylight saving time */
13
+	/** Seconds [0,60] */
14
+	int tm_sec;
15
+	/** Minutes [0,59] */
16
+	int tm_min;
17
+	/** Hour [0,23] */
18
+	int tm_hour;
19
+	/** Day of month [1,31] */
20
+	int tm_mday;
21
+	/** Month of year [0,11] */
22
+	int tm_mon;
23
+	/** Years since 1900 */
24
+	int tm_year;
25
+	/** Day of week [0,6] (Sunday=0) */
26
+	int tm_wday;
27
+	/** Day of year [0,365] */
28
+	int tm_yday;
29
+	/** Daylight savings flag */
30
+	int tm_isdst;
16 31
 };
17 32
 
18
-extern time_t time ( time_t *t );
19
-
20 33
 extern time_t mktime ( struct tm *tm );
21 34
 
22 35
 #endif /* _TIME_H */

Loading…
취소
저장