Exchange some references to certain players or pads with a feature in some parts...
[kugel-rb.git] / firmware / common / timefuncs.c
blob53ca5f3c162710b106f4f6fe5c5ec315532dcdd3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 */
23 #include "config.h"
25 #include "kernel.h"
26 #include "rtc.h"
27 #include "timefuncs.h"
28 #include "debug.h"
30 #ifndef SIMULATOR
31 static struct tm tm;
32 #endif
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)
43 return false;
44 else
45 return true;
48 struct tm *get_time(void)
50 #ifndef SIMULATOR
51 #if CONFIG_RTC
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
60 char rtcbuf[7];
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;
75 #else
76 tm.tm_year = ((rtcbuf[6] & 0xf0) >> 4) * 10 + (rtcbuf[6] & 0x0f) + 100;
77 #endif
79 tm.tm_yday = 0; /* Not implemented for now */
80 tm.tm_isdst = -1; /* Not implemented for now */
81 #else
82 rtc_read_datetime((unsigned char*)&tm);
83 #endif
85 #else
86 tm.tm_sec = 0;
87 tm.tm_min = 0;
88 tm.tm_hour = 0;
89 tm.tm_mday = 1;
90 tm.tm_mon = 0;
91 tm.tm_year = 70;
92 tm.tm_wday = 1;
93 tm.tm_yday = 0; /* Not implemented for now */
94 tm.tm_isdst = -1; /* Not implemented for now */
95 #endif
96 return &tm;
97 #else
98 time_t now = time(NULL);
99 return localtime(&now);
100 #endif
103 int set_time(const struct tm *tm)
105 #if CONFIG_RTC
106 int rc;
107 #if CONFIG_RTC != RTC_JZ47XX
108 char rtcbuf[7];
109 #endif
111 if (valid_time(tm))
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);
123 #else
124 rtcbuf[6]=(((tm->tm_year-100)/10) << 4) | ((tm->tm_year-100)%10);
125 #endif
127 rc = rtc_write_datetime(rtcbuf);
128 #else
129 rc = rtc_write_datetime((unsigned char*)tm);
130 #endif
132 if (rc < 0)
133 return -1;
134 else
135 return 0;
137 else
139 return -2;
141 #else
142 (void)tm;
143 return 0;
144 #endif
147 #if CONFIG_RTC
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)
152 short month, year;
153 time_t result;
154 static int m_to_d[12] =
155 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
157 month = t->tm_mon;
158 year = t->tm_year + month / 12 + 1900;
159 month %= 12;
160 if (month < 0)
162 year -= 1;
163 month += 12;
165 result = (year - 1970) * 365 + (year - 1969) / 4 + m_to_d[month];
166 result = (year - 1970) * 365 + m_to_d[month];
167 if (month <= 1)
168 year -= 1;
169 result += (year - 1968) / 4;
170 result -= (year - 1900) / 100;
171 result += (year - 1600) / 400;
172 result += t->tm_mday;
173 result -= 1;
174 result *= 24;
175 result += t->tm_hour;
176 result *= 60;
177 result += t->tm_min;
178 result *= 60;
179 result += t->tm_sec;
180 return(result);
182 #endif