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"
30 #if !defined SIMULATOR || !CONFIG_RTC
32 #endif /* !defined SIMULATOR || !CONFIG_RTC */
35 static void fill_default_tm(struct tm
*tm
)
44 tm
->tm_yday
= 0; /* Not implemented for now */
45 tm
->tm_isdst
= -1; /* Not implemented for now */
47 #endif /* !CONFIG_RTC */
49 bool valid_time(const struct tm
*tm
)
51 if (tm
->tm_hour
< 0 || tm
->tm_hour
> 23 ||
52 tm
->tm_sec
< 0 || tm
->tm_sec
> 59 ||
53 tm
->tm_min
< 0 || tm
->tm_min
> 59 ||
54 tm
->tm_year
< 100 || tm
->tm_year
> 199 ||
55 tm
->tm_mon
< 0 || tm
->tm_mon
> 11 ||
56 tm
->tm_wday
< 0 || tm
->tm_wday
> 6 ||
57 tm
->tm_mday
< 1 || tm
->tm_mday
> 31)
63 struct tm
*get_time(void)
67 static long timeout
= 0;
69 /* Don't read the RTC more than once per second */
70 if (current_tick
> timeout
)
72 /* Once per second, 1/10th of a second off */
73 timeout
= HZ
* (current_tick
/ HZ
+ 1) + HZ
/ 5;
74 #if CONFIG_RTC != RTC_JZ47XX
76 rtc_read_datetime(rtcbuf
);
78 tm
.tm_sec
= ((rtcbuf
[0] & 0x70) >> 4) * 10 + (rtcbuf
[0] & 0x0f);
79 tm
.tm_min
= ((rtcbuf
[1] & 0x70) >> 4) * 10 + (rtcbuf
[1] & 0x0f);
80 tm
.tm_hour
= ((rtcbuf
[2] & 0x30) >> 4) * 10 + (rtcbuf
[2] & 0x0f);
81 tm
.tm_wday
= rtcbuf
[3] & 0x07;
82 tm
.tm_mday
= ((rtcbuf
[4] & 0x30) >> 4) * 10 + (rtcbuf
[4] & 0x0f);
83 tm
.tm_mon
= ((rtcbuf
[5] & 0x10) >> 4) * 10 + (rtcbuf
[5] & 0x0f) - 1;
84 #ifdef IRIVER_H300_SERIES
85 /* Special kludge to coexist with the iriver firmware. The iriver firmware
86 stores the date as 1965+nn, and allows a range of 1980..2064. We use
87 1964+nn here to make leap years work correctly, so the date will be one
88 year off in the iriver firmware but at least won't be reset anymore. */
89 tm
.tm_year
= ((rtcbuf
[6] & 0xf0) >> 4) * 10 + (rtcbuf
[6] & 0x0f) + 64;
90 #else /* Not IRIVER_H300_SERIES */
91 tm
.tm_year
= ((rtcbuf
[6] & 0xf0) >> 4) * 10 + (rtcbuf
[6] & 0x0f) + 100;
92 #endif /* IRIVER_H300_SERIES */
94 tm
.tm_yday
= 0; /* Not implemented for now */
95 tm
.tm_isdst
= -1; /* Not implemented for now */
96 #else /* CONFIG_RTC == RTC_JZ47XX */
97 rtc_read_datetime((unsigned char*)&tm
);
98 #endif /* CONFIG_RTC */
101 fill_default_tm(&tm
);
105 #else /* SIMULATOR */
107 time_t now
= time(NULL
);
108 return localtime(&now
);
109 #else /* Simulator, no RTC */
110 fill_default_tm(&tm
);
113 #endif /* SIMULATOR */
116 int set_time(const struct tm
*tm
)
120 #if CONFIG_RTC != RTC_JZ47XX
126 #if CONFIG_RTC != RTC_JZ47XX
127 rtcbuf
[0]=((tm
->tm_sec
/10) << 4) | (tm
->tm_sec
%10);
128 rtcbuf
[1]=((tm
->tm_min
/10) << 4) | (tm
->tm_min
%10);
129 rtcbuf
[2]=((tm
->tm_hour
/10) << 4) | (tm
->tm_hour
%10);
130 rtcbuf
[3]=tm
->tm_wday
;
131 rtcbuf
[4]=((tm
->tm_mday
/10) << 4) | (tm
->tm_mday
%10);
132 rtcbuf
[5]=(((tm
->tm_mon
+1)/10) << 4) | ((tm
->tm_mon
+1)%10);
133 #ifdef IRIVER_H300_SERIES
134 /* Iriver firmware compatibility kludge, see get_time(). */
135 rtcbuf
[6]=(((tm
->tm_year
-64)/10) << 4) | ((tm
->tm_year
-64)%10);
137 rtcbuf
[6]=(((tm
->tm_year
-100)/10) << 4) | ((tm
->tm_year
-100)%10);
140 rc
= rtc_write_datetime(rtcbuf
);
142 rc
= rtc_write_datetime((unsigned char*)tm
);
161 /* mktime() code taken from lynx-2.8.5 source, written
162 by Philippe De Muyter <phdm@macqel.be> */
163 time_t mktime(struct tm
*t
)
167 static int m_to_d
[12] =
168 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
171 year
= t
->tm_year
+ month
/ 12 + 1900;
178 result
= (year
- 1970) * 365 + (year
- 1969) / 4 + m_to_d
[month
];
179 result
= (year
- 1970) * 365 + m_to_d
[month
];
182 result
+= (year
- 1968) / 4;
183 result
-= (year
- 1900) / 100;
184 result
+= (year
- 1600) / 400;
185 result
+= t
->tm_mday
;
188 result
+= t
->tm_hour
;
197 void set_day_of_week(struct tm
*tm
)
199 int y
=tm
->tm_year
+1900;
202 static const char mo
[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
204 if(m
== 0 || m
== 1) y
--;
205 tm
->tm_wday
= (d
+ mo
[m
] + y
+ y
/4 - y
/100 + y
/400) % 7;