FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / firmware / common / timefuncs.c
blob405bce4f5f8242b43ca8854605a9acdbf0e64a2c
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 #if !defined SIMULATOR || !CONFIG_RTC
31 static struct tm tm;
32 #endif /* !defined SIMULATOR || !CONFIG_RTC */
34 #if !CONFIG_RTC
35 static void fill_default_tm(struct tm *tm)
37 tm->tm_sec = 0;
38 tm->tm_min = 0;
39 tm->tm_hour = 0;
40 tm->tm_mday = 1;
41 tm->tm_mon = 0;
42 tm->tm_year = 70;
43 tm->tm_wday = 1;
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)
58 return false;
59 else
60 return true;
63 struct tm *get_time(void)
65 #ifndef SIMULATOR
66 #if CONFIG_RTC
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
75 char rtcbuf[7];
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 */
100 #else /* No RTC */
101 fill_default_tm(&tm);
102 #endif /* RTC */
103 return &tm;
105 #else /* SIMULATOR */
106 #if CONFIG_RTC
107 time_t now = time(NULL);
108 return localtime(&now);
109 #else /* Simulator, no RTC */
110 fill_default_tm(&tm);
111 return &tm;
112 #endif
113 #endif /* SIMULATOR */
116 int set_time(const struct tm *tm)
118 #if CONFIG_RTC
119 int rc;
120 #if CONFIG_RTC != RTC_JZ47XX
121 char rtcbuf[7];
122 #endif
124 if (valid_time(tm))
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);
136 #else
137 rtcbuf[6]=(((tm->tm_year-100)/10) << 4) | ((tm->tm_year-100)%10);
138 #endif
140 rc = rtc_write_datetime(rtcbuf);
141 #else
142 rc = rtc_write_datetime((unsigned char*)tm);
143 #endif
145 if (rc < 0)
146 return -1;
147 else
148 return 0;
150 else
152 return -2;
154 #else
155 (void)tm;
156 return 0;
157 #endif
160 #if CONFIG_RTC
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)
165 short month, year;
166 time_t result;
167 static int m_to_d[12] =
168 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
170 month = t->tm_mon;
171 year = t->tm_year + month / 12 + 1900;
172 month %= 12;
173 if (month < 0)
175 year -= 1;
176 month += 12;
178 result = (year - 1970) * 365 + (year - 1969) / 4 + m_to_d[month];
179 result = (year - 1970) * 365 + m_to_d[month];
180 if (month <= 1)
181 year -= 1;
182 result += (year - 1968) / 4;
183 result -= (year - 1900) / 100;
184 result += (year - 1600) / 400;
185 result += t->tm_mday;
186 result -= 1;
187 result *= 24;
188 result += t->tm_hour;
189 result *= 60;
190 result += t->tm_min;
191 result *= 60;
192 result += t->tm_sec;
193 return(result);
195 #endif
197 void set_day_of_week(struct tm *tm)
199 int y=tm->tm_year+1900;
200 int d=tm->tm_mday;
201 int m=tm->tm_mon;
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;