blob: dd70bf73ce9d5e3dd5fa1686c12f1c46421b5624 [file] [log] [blame]
Alessandro Zummoc58411e2006-03-27 01:16:34 -08001/*
2 * rtc and date/time utility functions
3 *
4 * Copyright (C) 2005-06 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * based on arch/arm/common/rtctime.c and other bits
8 *
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.
12*/
13
14#include <linux/module.h>
15#include <linux/rtc.h>
16
17static const unsigned char rtc_days_in_month[] = {
18 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
19};
20
Andrew Victor82322122006-06-25 05:48:25 -070021static const unsigned short rtc_ydays[2][13] = {
22 /* Normal years */
23 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
24 /* Leap years */
25 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
26};
27
Alessandro Zummoc58411e2006-03-27 01:16:34 -080028#define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
29#define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400))
30
Andrew Victor82322122006-06-25 05:48:25 -070031/*
32 * The number of days in the month.
33 */
Alessandro Zummoc58411e2006-03-27 01:16:34 -080034int rtc_month_days(unsigned int month, unsigned int year)
35{
36 return rtc_days_in_month[month] + (LEAP_YEAR(year) && month == 1);
37}
38EXPORT_SYMBOL(rtc_month_days);
39
40/*
Andrew Victor82322122006-06-25 05:48:25 -070041 * The number of days since January 1. (0 to 365)
42 */
43int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
44{
45 return rtc_ydays[LEAP_YEAR(year)][month] + day-1;
46}
47EXPORT_SYMBOL(rtc_year_days);
48
49/*
Alessandro Zummoc58411e2006-03-27 01:16:34 -080050 * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
51 */
52void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
53{
Jan Altenberg73442da2008-09-02 14:36:05 -070054 unsigned int month, year;
55 int days;
Alessandro Zummoc58411e2006-03-27 01:16:34 -080056
57 days = time / 86400;
Jan Altenberg73442da2008-09-02 14:36:05 -070058 time -= (unsigned int) days * 86400;
Alessandro Zummoc58411e2006-03-27 01:16:34 -080059
60 /* day of the week, 1970-01-01 was a Thursday */
61 tm->tm_wday = (days + 4) % 7;
62
63 year = 1970 + days / 365;
64 days -= (year - 1970) * 365
65 + LEAPS_THRU_END_OF(year - 1)
66 - LEAPS_THRU_END_OF(1970 - 1);
67 if (days < 0) {
68 year -= 1;
69 days += 365 + LEAP_YEAR(year);
70 }
71 tm->tm_year = year - 1900;
72 tm->tm_yday = days + 1;
73
74 for (month = 0; month < 11; month++) {
75 int newdays;
76
77 newdays = days - rtc_month_days(month, year);
78 if (newdays < 0)
79 break;
80 days = newdays;
81 }
82 tm->tm_mon = month;
83 tm->tm_mday = days + 1;
84
85 tm->tm_hour = time / 3600;
86 time -= tm->tm_hour * 3600;
87 tm->tm_min = time / 60;
88 tm->tm_sec = time - tm->tm_min * 60;
89}
90EXPORT_SYMBOL(rtc_time_to_tm);
91
92/*
93 * Does the rtc_time represent a valid date/time?
94 */
95int rtc_valid_tm(struct rtc_time *tm)
96{
97 if (tm->tm_year < 70
David Brownelldb621f12006-09-30 23:28:16 -070098 || ((unsigned)tm->tm_mon) >= 12
Alessandro Zummoc58411e2006-03-27 01:16:34 -080099 || tm->tm_mday < 1
100 || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
David Brownelldb621f12006-09-30 23:28:16 -0700101 || ((unsigned)tm->tm_hour) >= 24
102 || ((unsigned)tm->tm_min) >= 60
103 || ((unsigned)tm->tm_sec) >= 60)
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800104 return -EINVAL;
105
106 return 0;
107}
108EXPORT_SYMBOL(rtc_valid_tm);
109
110/*
111 * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
112 */
113int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
114{
115 *time = mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
116 tm->tm_hour, tm->tm_min, tm->tm_sec);
117 return 0;
118}
119EXPORT_SYMBOL(rtc_tm_to_time);
120
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800121MODULE_LICENSE("GPL");