IB/ipath: kbuild infrastructure
[linux-2.6/x86.git] / drivers / rtc / rtc-lib.c
blobcfedc1d28ee1ab80529a86afff78c3a171d3a357
1 /*
2 * rtc and date/time utility functions
4 * Copyright (C) 2005-06 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 * based on arch/arm/common/rtctime.c and other bits
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/rtc.h>
17 static const unsigned char rtc_days_in_month[] = {
18 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
21 #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
22 #define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400))
24 int rtc_month_days(unsigned int month, unsigned int year)
26 return rtc_days_in_month[month] + (LEAP_YEAR(year) && month == 1);
28 EXPORT_SYMBOL(rtc_month_days);
31 * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
33 void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
35 register int days, month, year;
37 days = time / 86400;
38 time -= days * 86400;
40 /* day of the week, 1970-01-01 was a Thursday */
41 tm->tm_wday = (days + 4) % 7;
43 year = 1970 + days / 365;
44 days -= (year - 1970) * 365
45 + LEAPS_THRU_END_OF(year - 1)
46 - LEAPS_THRU_END_OF(1970 - 1);
47 if (days < 0) {
48 year -= 1;
49 days += 365 + LEAP_YEAR(year);
51 tm->tm_year = year - 1900;
52 tm->tm_yday = days + 1;
54 for (month = 0; month < 11; month++) {
55 int newdays;
57 newdays = days - rtc_month_days(month, year);
58 if (newdays < 0)
59 break;
60 days = newdays;
62 tm->tm_mon = month;
63 tm->tm_mday = days + 1;
65 tm->tm_hour = time / 3600;
66 time -= tm->tm_hour * 3600;
67 tm->tm_min = time / 60;
68 tm->tm_sec = time - tm->tm_min * 60;
70 EXPORT_SYMBOL(rtc_time_to_tm);
73 * Does the rtc_time represent a valid date/time?
75 int rtc_valid_tm(struct rtc_time *tm)
77 if (tm->tm_year < 70
78 || tm->tm_mon >= 12
79 || tm->tm_mday < 1
80 || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
81 || tm->tm_hour >= 24
82 || tm->tm_min >= 60
83 || tm->tm_sec >= 60)
84 return -EINVAL;
86 return 0;
88 EXPORT_SYMBOL(rtc_valid_tm);
91 * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
93 int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
95 *time = mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
96 tm->tm_hour, tm->tm_min, tm->tm_sec);
97 return 0;
99 EXPORT_SYMBOL(rtc_tm_to_time);
101 MODULE_LICENSE("GPL");