PBell vibe 500: ROLO fix
[kugel-rb.git] / firmware / common / timefuncs.c
blobe423e016fdd2eb6d723325a905884c4aaadcc2e0
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 static struct tm tm;
32 #if !CONFIG_RTC
33 static void fill_default_tm(struct tm *tm)
35 tm->tm_sec = 0;
36 tm->tm_min = 0;
37 tm->tm_hour = 0;
38 tm->tm_mday = 1;
39 tm->tm_mon = 0;
40 tm->tm_year = 70;
41 tm->tm_wday = 1;
42 tm->tm_yday = 0; /* Not implemented for now */
43 tm->tm_isdst = -1; /* Not implemented for now */
45 #endif /* !CONFIG_RTC */
47 bool valid_time(const struct tm *tm)
49 if (tm->tm_hour < 0 || tm->tm_hour > 23 ||
50 tm->tm_sec < 0 || tm->tm_sec > 59 ||
51 tm->tm_min < 0 || tm->tm_min > 59 ||
52 tm->tm_year < 100 || tm->tm_year > 199 ||
53 tm->tm_mon < 0 || tm->tm_mon > 11 ||
54 tm->tm_wday < 0 || tm->tm_wday > 6 ||
55 tm->tm_mday < 1 || tm->tm_mday > 31)
56 return false;
57 else
58 return true;
61 struct tm *get_time(void)
63 #if CONFIG_RTC
64 static long timeout = 0;
66 /* Don't read the RTC more than once per second */
67 if (TIME_AFTER(current_tick, timeout))
69 /* Once per second, 1/10th of a second off */
70 timeout = HZ * (current_tick / HZ + 1) + HZ / 5;
71 rtc_read_datetime(&tm);
73 tm.tm_yday = 0; /* Not implemented for now */
74 tm.tm_isdst = -1; /* Not implemented for now */
76 #else /* No RTC */
77 fill_default_tm(&tm);
78 #endif /* RTC */
79 return &tm;
82 int set_time(const struct tm *tm)
84 #if CONFIG_RTC
85 int rc;
87 if (valid_time(tm))
89 rc = rtc_write_datetime(tm);
91 if (rc < 0)
92 return -1;
93 else
94 return 0;
96 else
98 return -2;
100 #else /* No RTC */
101 (void)tm;
102 return 0;
103 #endif /* RTC */
106 #if CONFIG_RTC
107 /* mktime() code taken from lynx-2.8.5 source, written
108 by Philippe De Muyter <phdm@macqel.be> */
109 time_t mktime(struct tm *t)
111 short month, year;
112 time_t result;
113 static int m_to_d[12] =
114 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
116 month = t->tm_mon;
117 year = t->tm_year + month / 12 + 1900;
118 month %= 12;
119 if (month < 0)
121 year -= 1;
122 month += 12;
124 result = (year - 1970) * 365 + (year - 1969) / 4 + m_to_d[month];
125 result = (year - 1970) * 365 + m_to_d[month];
126 if (month <= 1)
127 year -= 1;
128 result += (year - 1968) / 4;
129 result -= (year - 1900) / 100;
130 result += (year - 1600) / 400;
131 result += t->tm_mday;
132 result -= 1;
133 result *= 24;
134 result += t->tm_hour;
135 result *= 60;
136 result += t->tm_min;
137 result *= 60;
138 result += t->tm_sec;
139 return(result);
141 #endif
143 void set_day_of_week(struct tm *tm)
145 int y=tm->tm_year+1900;
146 int d=tm->tm_mday;
147 int m=tm->tm_mon;
148 static const char mo[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
150 if(m == 0 || m == 1) y--;
151 tm->tm_wday = (d + mo[m] + y + y/4 - y/100 + y/400) % 7;