nand_base: We have to ignore the -EUCLEAN error
[barebox-mini2440.git] / common / date.c
blob47d96afd3fce046fa7effe9484ec5ddda9dae1d7
1 /*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
6 * project.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
24 /**
25 * @file
26 * @brief Date & Time support
29 #include <common.h>
30 #include <command.h>
31 #include <rtc.h>
33 #define FEBRUARY 2
34 #define STARTOFTIME 1970
35 #define SECDAY 86400L
36 #define SECYR (SECDAY * 365)
37 #define leapyear(year) ((year) % 4 == 0)
38 #define days_in_year(a) (leapyear(a) ? 366 : 365)
39 #define days_in_month(a) (month_days[(a) - 1])
41 static int month_days[12] = {
42 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
46 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
48 void GregorianDay(struct rtc_time * tm)
50 int leapsToDate;
51 int lastYear;
52 int day;
53 int MonthOffset[] = { 0,31,59,90,120,151,181,212,243,273,304,334 };
55 lastYear=tm->tm_year-1;
58 * Number of leap corrections to apply up to end of last year
60 leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
63 * This year is a leap year if it is divisible by 4 except when it is
64 * divisible by 100 unless it is divisible by 400
66 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be
68 if((tm->tm_year%4==0) &&
69 ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
70 (tm->tm_mon>2)) {
72 * We are past Feb. 29 in a leap year
74 day=1;
75 } else {
76 day=0;
79 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] + tm->tm_mday;
81 tm->tm_wday=day%7;
84 void to_tm(int tim, struct rtc_time * tm)
86 register int i;
87 register long hms, day;
89 day = tim / SECDAY;
90 hms = tim % SECDAY;
92 /* Hours, minutes, seconds are easy */
93 tm->tm_hour = hms / 3600;
94 tm->tm_min = (hms % 3600) / 60;
95 tm->tm_sec = (hms % 3600) % 60;
97 /* Number of years in days */
98 for (i = STARTOFTIME; day >= days_in_year(i); i++) {
99 day -= days_in_year(i);
101 tm->tm_year = i;
103 /* Number of months in days left */
104 if (leapyear(tm->tm_year)) {
105 days_in_month(FEBRUARY) = 29;
107 for (i = 1; day >= days_in_month(i); i++) {
108 day -= days_in_month(i);
110 days_in_month(FEBRUARY) = 28;
111 tm->tm_mon = i;
113 /* Days are what is left over (+1) from all that. */
114 tm->tm_mday = day + 1;
117 * Determine the day of week
119 GregorianDay(tm);
121 EXPORT_SYMBOL(to_tm);
123 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
124 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
125 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
127 * [For the Julian calendar (which was used in Russia before 1917,
128 * Britain & colonies before 1752, anywhere else before 1582,
129 * and is still in use by some communities) leave out the
130 * -year/100+year/400 terms, and add 10.]
132 * This algorithm was first published by Gauss (I think).
134 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
135 * machines were long is 32-bit! (However, as time_t is signed, we
136 * will already get problems at other places on 2038-01-19 03:14:08)
138 unsigned long
139 mktime (unsigned int year, unsigned int mon,
140 unsigned int day, unsigned int hour,
141 unsigned int min, unsigned int sec)
143 if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
144 mon += 12; /* Puts Feb last since it has leap day */
145 year -= 1;
148 return (((
149 (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
150 year*365 - 719499
151 )*24 + hour /* now have hours */
152 )*60 + min /* now have minutes */
153 )*60 + sec; /* finally seconds */