1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h> /* get NULL */
27 #include "timefuncs.h"
33 static void fill_default_tm(struct tm
*tm
)
42 tm
->tm_yday
= 0; /* Not implemented for now */
43 tm
->tm_isdst
= -1; /* Not implemented for now */
45 #endif /* !CONFIG_RTC */
47 bool valid_time(const struct tm
*tm
)
49 if (tm
->tm_hour
< 0 || tm
->tm_hour
> 23 ||
50 tm
->tm_sec
< 0 || tm
->tm_sec
> 59 ||
51 tm
->tm_min
< 0 || tm
->tm_min
> 59 ||
52 tm
->tm_year
< 100 || tm
->tm_year
> 199 ||
53 tm
->tm_mon
< 0 || tm
->tm_mon
> 11 ||
54 tm
->tm_wday
< 0 || tm
->tm_wday
> 6 ||
55 tm
->tm_mday
< 1 || tm
->tm_mday
> 31)
61 struct tm
*get_time(void)
64 static long timeout
= 0;
66 /* Don't read the RTC more than once per second */
67 if (TIME_AFTER(current_tick
, timeout
))
69 /* Once per second, 1/10th of a second off */
70 timeout
= HZ
* (current_tick
/ HZ
+ 1) + HZ
/ 5;
71 rtc_read_datetime(&tm
);
73 tm
.tm_yday
= 0; /* Not implemented for now */
74 tm
.tm_isdst
= -1; /* Not implemented for now */
82 int set_time(const struct tm
*tm
)
89 rc
= rtc_write_datetime(tm
);
107 /* mktime() code taken from lynx-2.8.5 source, written
108 by Philippe De Muyter <phdm@macqel.be> */
109 time_t mktime(struct tm
*t
)
113 static int m_to_d
[12] =
114 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
117 year
= t
->tm_year
+ month
/ 12 + 1900;
124 result
= (year
- 1970) * 365 + (year
- 1969) / 4 + m_to_d
[month
];
125 result
= (year
- 1970) * 365 + m_to_d
[month
];
128 result
+= (year
- 1968) / 4;
129 result
-= (year
- 1900) / 100;
130 result
+= (year
- 1600) / 400;
131 result
+= t
->tm_mday
;
134 result
+= t
->tm_hour
;
143 void set_day_of_week(struct tm
*tm
)
145 int y
=tm
->tm_year
+1900;
148 static const char mo
[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
150 if(m
== 0 || m
== 1) y
--;
151 tm
->tm_wday
= (d
+ mo
[m
] + y
+ y
/4 - y
/100 + y
/400) % 7;