Bump plugin API.
[maemo-rb.git] / firmware / common / timefuncs.c
blob7d385949b5d3289519b47facfd83a685ff0d2673
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 -1;
103 #endif /* RTC */
106 void set_day_of_week(struct tm *tm)
108 int y=tm->tm_year+1900;
109 int d=tm->tm_mday;
110 int m=tm->tm_mon;
111 static const char mo[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
113 if(m == 0 || m == 1) y--;
114 tm->tm_wday = (d + mo[m] + y + y/4 - y/100 + y/400) % 7;