mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Antony Pavlov <antonynpavlov@gmail.com>
To: barebox@lists.infradead.org
Subject: [PATCH v4 6/9] rtc-lib: import rtc_time_to_tm() from linux-3.15
Date: Wed, 30 Jul 2014 00:10:22 +0400	[thread overview]
Message-ID: <1406664625-19653-7-git-send-email-antonynpavlov@gmail.com> (raw)
In-Reply-To: <1406664625-19653-1-git-send-email-antonynpavlov@gmail.com>

Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
---
 drivers/rtc/rtc-lib.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/rtc.h   |  1 +
 2 files changed, 46 insertions(+)

diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c
index 0d6acde..1b23458 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)
diff --git a/include/linux/rtc.h b/include/linux/rtc.h
index 2e034f6..2dacb14 100644
--- a/include/linux/rtc.h
+++ b/include/linux/rtc.h
@@ -17,6 +17,7 @@
 extern int rtc_month_days(unsigned int month, unsigned int year);
 extern int rtc_valid_tm(struct rtc_time *tm);
 extern int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time);
+extern void rtc_time_to_tm(unsigned long time, struct rtc_time *tm);
 
 struct rtc_class_ops;
 
-- 
2.0.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2014-07-29 20:11 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-29 20:10 [PATCH v4 0/9] Add a simple rtc framework Antony Pavlov
2014-07-29 20:10 ` [PATCH v4 1/9] lib: import 'bcd' from linux-3.15 Antony Pavlov
2014-07-29 20:10 ` [PATCH v4 2/9] Add a simple rtc framework Antony Pavlov
2014-07-29 20:10 ` [PATCH v4 3/9] rtc: import ds1307 driver from linux-3.15 Antony Pavlov
2014-07-29 20:10 ` [PATCH v4 4/9] commands: add hwclock Antony Pavlov
2014-07-29 20:10 ` [PATCH v4 5/9] ARM: versatilepb_defconfig: enable RTC support Antony Pavlov
2014-07-29 20:10 ` Antony Pavlov [this message]
2014-07-29 20:10 ` [PATCH v4 7/9] rtc: add jz4740 driver Antony Pavlov
2014-07-29 20:10 ` [PATCH v4 8/9] MIPS: dts: jz4755.dtsi: add jz4740-rtc Antony Pavlov
2014-07-29 20:10 ` [PATCH v4 9/9] MIPS: ritmix-rzx50_defconfig: enable RTC support Antony Pavlov
2014-07-31  5:31 ` [PATCH v4 0/9] Add a simple rtc framework Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1406664625-19653-7-git-send-email-antonynpavlov@gmail.com \
    --to=antonynpavlov@gmail.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox