summaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
authorAntony Pavlov <antonynpavlov@gmail.com>2014-07-30 00:10:22 +0400
committerSascha Hauer <s.hauer@pengutronix.de>2014-08-02 06:45:43 +0200
commit287e085fa9e7430564f9ffe9b70c9e77d420052e (patch)
tree55a268603739c9a24ff299e7568c5e4c6b13f65e /drivers/rtc
parente1af262100c45ec4e02113453c29eab3bd32ce26 (diff)
downloadbarebox-287e085fa9e7430564f9ffe9b70c9e77d420052e.tar.gz
barebox-287e085fa9e7430564f9ffe9b70c9e77d420052e.tar.xz
rtc-lib: import rtc_time_to_tm() from linux-3.15
Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/rtc-lib.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c
index 0d6acde3e0..1b23458a57 100644
--- a/drivers/rtc/rtc-lib.c
+++ b/drivers/rtc/rtc-lib.c
@@ -39,6 +39,51 @@ int rtc_month_days(unsigned int month, unsigned int year)
EXPORT_SYMBOL(rtc_month_days);
/*
+ * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
+ */
+void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
+{
+ unsigned int month, year;
+ int days;
+
+ days = time / 86400;
+ time -= (unsigned int) days * 86400;
+
+ /* day of the week, 1970-01-01 was a Thursday */
+ tm->tm_wday = (days + 4) % 7;
+
+ year = 1970 + days / 365;
+ days -= (year - 1970) * 365
+ + LEAPS_THRU_END_OF(year - 1)
+ - LEAPS_THRU_END_OF(1970 - 1);
+ if (days < 0) {
+ year -= 1;
+ days += 365 + is_leap_year(year);
+ }
+ tm->tm_year = year - 1900;
+ tm->tm_yday = days + 1;
+
+ for (month = 0; month < 11; month++) {
+ int newdays;
+
+ newdays = days - rtc_month_days(month, year);
+ if (newdays < 0)
+ break;
+ days = newdays;
+ }
+ tm->tm_mon = month;
+ tm->tm_mday = days + 1;
+
+ tm->tm_hour = time / 3600;
+ time -= tm->tm_hour * 3600;
+ tm->tm_min = time / 60;
+ tm->tm_sec = time - tm->tm_min * 60;
+
+ tm->tm_isdst = 0;
+}
+EXPORT_SYMBOL(rtc_time_to_tm);
+
+/*
* Does the rtc_time represent a valid date/time?
*/
int rtc_valid_tm(struct rtc_time *tm)