]> hydra-www.ietfng.org Git - xv6-public/commitdiff
Convert 12-hour clock to 24-hour time.
authorPeter H. Froehlich <peter.hans.froehlich@gmail.com>
Thu, 5 Nov 2015 22:52:05 +0000 (17:52 -0500)
committerPeter H. Froehlich <peter.hans.froehlich@gmail.com>
Thu, 5 Nov 2015 22:52:05 +0000 (17:52 -0500)
cmos.c
cmos.h

diff --git a/cmos.c b/cmos.c
index 68fcb90853b5db970889c659a06deb068076562d..4b295b0e9a169b90cb7db550e54a43c5721c1a5f 100644 (file)
--- a/cmos.c
+++ b/cmos.c
@@ -40,11 +40,12 @@ static void fill_rtcdate(struct rtcdate *r)
 void cmostime(struct rtcdate *r)
 {
   struct rtcdate t1, t2;
-  int sb, bcd;
+  int sb, bcd, tf;
 
   sb = cmosread(CMOS_STATB);
 
   bcd = (sb & CMOS_BINARY_BIT) == 0;
+  tf = (sb & CMOS_24H_BIT) != 0;
 
   // make sure CMOS doesn't modify time while we read it
   for(;;){
@@ -56,7 +57,10 @@ void cmostime(struct rtcdate *r)
       break;
   }
 
-  // convert
+  // backup raw values since BCD conversion removes PM bit from hour
+  t2 = t1;
+
+  // convert t1 from BCD
   if(bcd){
 #define    CONV(x)     (t1.x = ((t1.x >> 4) * 10) + (t1.x & 0xf))
     CONV(second);
@@ -68,6 +72,13 @@ void cmostime(struct rtcdate *r)
 #undef     CONV
   }
 
+  // convert 12 hour format to 24 hour format
+  if(!tf){
+    if(t2.hour & CMOS_PM_BIT){
+      t1.hour = (t1.hour + 12) % 24;
+    }
+  }
+
   *r = t1;
   r->year += 2000;
 }
diff --git a/cmos.h b/cmos.h
index 378f1524aed0d7a5bcf501ad34d9f8f10a3e1d4b..43cd068e3bf552cdfd8caddd33de3b98d4ca4adb 100644 (file)
--- a/cmos.h
+++ b/cmos.h
@@ -14,6 +14,7 @@
 #define CMOS_SECS    0x00
 #define CMOS_MINS    0x02
 #define CMOS_HOURS   0x04
+  #define CMOS_PM_BIT     (1 << 7)  // RTC PM
 #define CMOS_DAY     0x07
 #define CMOS_MONTH   0x08
 #define CMOS_YEAR    0x09