Bump version numbers for 3.13
[maemo-rb.git] / firmware / common / timefuncs.c
blobffa6e2a0b310e303e2babbab1d615bac9e31eec7
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 #if CONFIG_RTC
48 bool valid_time(const struct tm *tm)
50 if (tm->tm_hour < 0 || tm->tm_hour > 23 ||
51 tm->tm_sec < 0 || tm->tm_sec > 59 ||
52 tm->tm_min < 0 || tm->tm_min > 59 ||
53 tm->tm_year < 100 || tm->tm_year > 199 ||
54 tm->tm_mon < 0 || tm->tm_mon > 11 ||
55 tm->tm_wday < 0 || tm->tm_wday > 6 ||
56 tm->tm_mday < 1 || tm->tm_mday > 31)
57 return false;
58 else
59 return true;
61 #endif /* CONFIG_RTC */
63 struct tm *get_time(void)
65 #if CONFIG_RTC
66 static long timeout = 0;
68 /* Don't read the RTC more than once per second */
69 if (TIME_AFTER(current_tick, timeout))
71 /* Once per second, 1/10th of a second off */
72 timeout = HZ * (current_tick / HZ + 1) + HZ / 5;
73 rtc_read_datetime(&tm);
74 set_day_of_week(&tm);
76 tm.tm_yday = 0; /* Not implemented for now */
77 tm.tm_isdst = -1; /* Not implemented for now */
79 #else /* No RTC */
80 fill_default_tm(&tm);
81 #endif /* RTC */
82 return &tm;
85 int set_time(const struct tm *tm)
87 #if CONFIG_RTC
88 int rc;
90 if (valid_time(tm))
92 rc = rtc_write_datetime(tm);
94 if (rc < 0)
95 return -1;
96 else
97 return 0;
99 else
101 return -2;
103 #else /* No RTC */
104 (void)tm;
105 return -1;
106 #endif /* RTC */
109 #if CONFIG_RTC
110 void set_day_of_week(struct tm *tm)
112 int y=tm->tm_year+1900;
113 int d=tm->tm_mday;
114 int m=tm->tm_mon;
115 static const char mo[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
117 if(m == 0 || m == 1) y--;
118 tm->tm_wday = (d + mo[m] + y + y/4 - y/100 + y/400) % 7;
120 #endif /* CONFIG_RTC */