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"
34 bool valid_time(const struct tm
*tm
)
36 if (tm
->tm_hour
< 0 || tm
->tm_hour
> 23 ||
37 tm
->tm_sec
< 0 || tm
->tm_sec
> 59 ||
38 tm
->tm_min
< 0 || tm
->tm_min
> 59 ||
39 tm
->tm_year
< 100 || tm
->tm_year
> 199 ||
40 tm
->tm_mon
< 0 || tm
->tm_mon
> 11 ||
41 tm
->tm_wday
< 0 || tm
->tm_wday
> 6 ||
42 tm
->tm_mday
< 1 || tm
->tm_mday
> 31)
48 struct tm
*get_time(void)
52 static long timeout
= 0;
54 /* Don't read the RTC more than once per second */
55 if (current_tick
> timeout
)
57 /* Once per second, 1/10th of a second off */
58 timeout
= HZ
* (current_tick
/ HZ
+ 1) + HZ
/ 5;
59 #if CONFIG_RTC != RTC_JZ47XX
61 rtc_read_datetime(rtcbuf
);
63 tm
.tm_sec
= ((rtcbuf
[0] & 0x70) >> 4) * 10 + (rtcbuf
[0] & 0x0f);
64 tm
.tm_min
= ((rtcbuf
[1] & 0x70) >> 4) * 10 + (rtcbuf
[1] & 0x0f);
65 tm
.tm_hour
= ((rtcbuf
[2] & 0x30) >> 4) * 10 + (rtcbuf
[2] & 0x0f);
66 tm
.tm_wday
= rtcbuf
[3] & 0x07;
67 tm
.tm_mday
= ((rtcbuf
[4] & 0x30) >> 4) * 10 + (rtcbuf
[4] & 0x0f);
68 tm
.tm_mon
= ((rtcbuf
[5] & 0x10) >> 4) * 10 + (rtcbuf
[5] & 0x0f) - 1;
69 #ifdef IRIVER_H300_SERIES
70 /* Special kludge to coexist with the iriver firmware. The iriver firmware
71 stores the date as 1965+nn, and allows a range of 1980..2064. We use
72 1964+nn here to make leap years work correctly, so the date will be one
73 year off in the iriver firmware but at least won't be reset anymore. */
74 tm
.tm_year
= ((rtcbuf
[6] & 0xf0) >> 4) * 10 + (rtcbuf
[6] & 0x0f) + 64;
76 tm
.tm_year
= ((rtcbuf
[6] & 0xf0) >> 4) * 10 + (rtcbuf
[6] & 0x0f) + 100;
79 tm
.tm_yday
= 0; /* Not implemented for now */
80 tm
.tm_isdst
= -1; /* Not implemented for now */
82 rtc_read_datetime((unsigned char*)&tm
);
93 tm
.tm_yday
= 0; /* Not implemented for now */
94 tm
.tm_isdst
= -1; /* Not implemented for now */
98 time_t now
= time(NULL
);
99 return localtime(&now
);
103 int set_time(const struct tm
*tm
)
107 #if CONFIG_RTC != RTC_JZ47XX
113 #if CONFIG_RTC != RTC_JZ47XX
114 rtcbuf
[0]=((tm
->tm_sec
/10) << 4) | (tm
->tm_sec
%10);
115 rtcbuf
[1]=((tm
->tm_min
/10) << 4) | (tm
->tm_min
%10);
116 rtcbuf
[2]=((tm
->tm_hour
/10) << 4) | (tm
->tm_hour
%10);
117 rtcbuf
[3]=tm
->tm_wday
;
118 rtcbuf
[4]=((tm
->tm_mday
/10) << 4) | (tm
->tm_mday
%10);
119 rtcbuf
[5]=(((tm
->tm_mon
+1)/10) << 4) | ((tm
->tm_mon
+1)%10);
120 #ifdef IRIVER_H300_SERIES
121 /* Iriver firmware compatibility kludge, see get_time(). */
122 rtcbuf
[6]=(((tm
->tm_year
-64)/10) << 4) | ((tm
->tm_year
-64)%10);
124 rtcbuf
[6]=(((tm
->tm_year
-100)/10) << 4) | ((tm
->tm_year
-100)%10);
127 rc
= rtc_write_datetime(rtcbuf
);
129 rc
= rtc_write_datetime((unsigned char*)tm
);
148 /* mktime() code taken from lynx-2.8.5 source, written
149 by Philippe De Muyter <phdm@macqel.be> */
150 time_t mktime(struct tm
*t
)
154 static int m_to_d
[12] =
155 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
158 year
= t
->tm_year
+ month
/ 12 + 1900;
165 result
= (year
- 1970) * 365 + (year
- 1969) / 4 + m_to_d
[month
];
166 result
= (year
- 1970) * 365 + m_to_d
[month
];
169 result
+= (year
- 1968) / 4;
170 result
-= (year
- 1900) / 100;
171 result
+= (year
- 1600) / 400;
172 result
+= t
->tm_mday
;
175 result
+= t
->tm_hour
;