1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdio.h> /* get NULL */
25 #include "timefuncs.h"
32 bool valid_time(const struct tm
*tm
)
34 if (tm
->tm_hour
< 0 || tm
->tm_hour
> 23 ||
35 tm
->tm_sec
< 0 || tm
->tm_sec
> 59 ||
36 tm
->tm_min
< 0 || tm
->tm_min
> 59 ||
37 tm
->tm_year
< 100 || tm
->tm_year
> 199 ||
38 tm
->tm_mon
< 0 || tm
->tm_mon
> 11 ||
39 tm
->tm_wday
< 0 || tm
->tm_wday
> 6 ||
40 tm
->tm_mday
< 1 || tm
->tm_mday
> 31)
46 struct tm
*get_time(void)
50 static long timeout
= 0;
52 /* Don't read the RTC more than once per second */
53 if (current_tick
> timeout
) {
55 /* Once per second, 1/10th of a second off */
56 timeout
= HZ
* (current_tick
/ HZ
+ 1) + HZ
/ 5;
57 rtc_read_datetime(rtcbuf
);
59 tm
.tm_sec
= ((rtcbuf
[0] & 0x70) >> 4) * 10 + (rtcbuf
[0] & 0x0f);
60 tm
.tm_min
= ((rtcbuf
[1] & 0x70) >> 4) * 10 + (rtcbuf
[1] & 0x0f);
61 tm
.tm_hour
= ((rtcbuf
[2] & 0x30) >> 4) * 10 + (rtcbuf
[2] & 0x0f);
62 tm
.tm_wday
= rtcbuf
[3] & 0x07;
63 tm
.tm_mday
= ((rtcbuf
[4] & 0x30) >> 4) * 10 + (rtcbuf
[4] & 0x0f);
64 tm
.tm_mon
= ((rtcbuf
[5] & 0x10) >> 4) * 10 + (rtcbuf
[5] & 0x0f) - 1;
65 #ifdef IRIVER_H300_SERIES
66 /* Special kludge to coexist with the iriver firmware. The iriver firmware
67 stores the date as 1965+nn, and allows a range of 1980..2064. We use
68 1964+nn here to make leap years work correctly, so the date will be one
69 year off in the iriver firmware but at least won't be reset anymore. */
70 tm
.tm_year
= ((rtcbuf
[6] & 0xf0) >> 4) * 10 + (rtcbuf
[6] & 0x0f) + 64;
72 tm
.tm_year
= ((rtcbuf
[6] & 0xf0) >> 4) * 10 + (rtcbuf
[6] & 0x0f) + 100;
75 tm
.tm_yday
= 0; /* Not implemented for now */
76 tm
.tm_isdst
= -1; /* Not implemented for now */
86 tm
.tm_yday
= 0; /* Not implemented for now */
87 tm
.tm_isdst
= -1; /* Not implemented for now */
91 time_t now
= time(NULL
);
92 return localtime(&now
);
96 int set_time(const struct tm
*tm
)
104 rtcbuf
[0]=((tm
->tm_sec
/10) << 4) | (tm
->tm_sec
%10);
105 rtcbuf
[1]=((tm
->tm_min
/10) << 4) | (tm
->tm_min
%10);
106 rtcbuf
[2]=((tm
->tm_hour
/10) << 4) | (tm
->tm_hour
%10);
107 rtcbuf
[3]=tm
->tm_wday
;
108 rtcbuf
[4]=((tm
->tm_mday
/10) << 4) | (tm
->tm_mday
%10);
109 rtcbuf
[5]=(((tm
->tm_mon
+1)/10) << 4) | ((tm
->tm_mon
+1)%10);
110 #ifdef IRIVER_H300_SERIES
111 /* Iriver firmware compatibility kludge, see get_time(). */
112 rtcbuf
[6]=(((tm
->tm_year
-64)/10) << 4) | ((tm
->tm_year
-64)%10);
114 rtcbuf
[6]=(((tm
->tm_year
-100)/10) << 4) | ((tm
->tm_year
-100)%10);
117 rc
= rtc_write_datetime(rtcbuf
);
135 /* mktime() code taken from lynx-2.8.5 source, written
136 by Philippe De Muyter <phdm@macqel.be> */
137 time_t mktime(struct tm
*t
)
141 static int m_to_d
[12] =
142 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
145 year
= t
->tm_year
+ month
/ 12 + 1900;
152 result
= (year
- 1970) * 365 + (year
- 1969) / 4 + m_to_d
[month
];
153 result
= (year
- 1970) * 365 + m_to_d
[month
];
156 result
+= (year
- 1968) / 4;
157 result
-= (year
- 1900) / 100;
158 result
+= (year
- 1600) / 400;
159 result
+= t
->tm_mday
;
162 result
+= t
->tm_hour
;