From 98de5e573afbda4b8346b19afdd4ff5cb47c058a Mon Sep 17 00:00:00 2001
From: "Ruslan V. Uss" <UncleRus@users.noreply.github.com>
Date: Tue, 1 Nov 2016 15:40:19 +0600
Subject: [PATCH] RTC drivers fix (#259)

---
 examples/ds1307/main.c | 27 ++++++++++++++-------------
 extras/ds1307/ds1307.c |  4 ++--
 extras/ds3231/ds3231.c |  2 +-
 3 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/examples/ds1307/main.c b/examples/ds1307/main.c
index 2f22447..a1b847e 100644
--- a/examples/ds1307/main.c
+++ b/examples/ds1307/main.c
@@ -14,32 +14,33 @@
 #define SCL_PIN 5
 #define SDA_PIN 4
 
-void user_init (void)
+void user_init(void)
 {
-    uart_set_baud (0, 115200);
-    printf ("SDK version:%s\n", sdk_system_get_sdk_version ());
+    uart_set_baud(0, 115200);
+    printf("SDK version:%s\n", sdk_system_get_sdk_version());
 
-    i2c_init (SCL_PIN, SDA_PIN);
-    ds1307_start (true);
+    i2c_init(SCL_PIN, SDA_PIN);
+    ds1307_start(true);
 
     // setup datetime: 2016-10-09 13:50:10
     struct tm time = {
         .tm_year = 2016,
-        .tm_mon = 10,
+        .tm_mon  = 9,  // 0-based
         .tm_mday = 9,
         .tm_hour = 13,
-        .tm_min = 50,
-        .tm_sec = 10
+        .tm_min  = 50,
+        .tm_sec  = 10
     };
-    ds1307_set_time (&time);
+    ds1307_set_time(&time);
 
     while (true)
     {
-        ds1307_get_time (&time);
+        ds1307_get_time(&time);
 
-        printf ("%04d-%02d-%02d %02d:%02d:%02d\n", time.tm_year, time.tm_mon, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);
+        printf("%04d-%02d-%02d %02d:%02d:%02d\n", time.tm_year, time.tm_mon + 1,
+            time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);
 
-        for (uint32_t i = 0; i < 1000; i ++)
-            sdk_os_delay_us (500);
+        for (uint32_t i = 0; i < 1000; i++)
+            sdk_os_delay_us(500);
     }
 }
diff --git a/extras/ds1307/ds1307.c b/extras/ds1307/ds1307.c
index cd4472f..90abf9d 100644
--- a/extras/ds1307/ds1307.c
+++ b/extras/ds1307/ds1307.c
@@ -78,14 +78,14 @@ void ds1307_get_time(struct tm *time)
     if (buf[2] & HOUR12_BIT)
     {
         // RTC in 12-hour mode
-        time->tm_hour = bcd2dec(buf[2] & HOUR12_MASK);
+        time->tm_hour = bcd2dec(buf[2] & HOUR12_MASK) - 1;
         if (buf[2] & PM_BIT)
             time->tm_hour += 12;
     }
     else time->tm_hour = bcd2dec(buf[2] & HOUR24_MASK);
     time->tm_wday = bcd2dec(buf[3]) - 1;
     time->tm_mday = bcd2dec(buf[4]);
-    time->tm_mon  = bcd2dec(buf[5]);
+    time->tm_mon  = bcd2dec(buf[5]) - 1;
     time->tm_year = bcd2dec(buf[6]) + 2000;
 }
 
diff --git a/extras/ds3231/ds3231.c b/extras/ds3231/ds3231.c
index 87ce805..543474e 100644
--- a/extras/ds3231/ds3231.c
+++ b/extras/ds3231/ds3231.c
@@ -270,7 +270,7 @@ bool ds3231_getTime(struct tm *time)
     time->tm_min = bcdToDec(data[1]);
     if (data[2] & DS3231_12HOUR_FLAG) {
         /* 12H */
-        time->tm_hour = bcdToDec(data[2] & DS3231_12HOUR_MASK);
+        time->tm_hour = bcdToDec(data[2] & DS3231_12HOUR_MASK) - 1;
         /* AM/PM? */
         if (data[2] & DS3231_PM_FLAG) time->tm_hour += 12;
     } else {