Backing up an unused program in case of future need

Dependencies:   mbed

Revision:
7:024ace6d943c
Parent:
6:be97d38e0b01
Child:
8:45a0205a298f
--- a/time.cpp	Tue May 31 07:35:28 2016 +0000
+++ b/time.cpp	Thu Jun 02 09:26:28 2016 +0000
@@ -23,6 +23,91 @@
 {
     return unixTime16ths;
 }
+void TimeToTm(uint32_t time, struct tm* ptm)
+{
+    int seconds  = time % 60; time /= 60;
+    int minutes  = time % 60; time /= 60;
+    int hours    = time % 24; time /= 24;
+    int daysLeft = time;
+    
+    //Add a year at a time while there is more than a year of days left
+    int year      = 1970; //Unix epoch is 1970
+    int dayOfWeek = 4;    //1 Jan 1970 is a Thursday
+    int dayOfYear;
+    int leapYear;
+    while(1)
+    {
+        //See if it is a leap year
+        int year4   = !(year %   4);
+        int year100 = !(year % 100);
+        int year400 = !(year % 400);
+                     leapYear = false;
+        if (year4)   leapYear =  true;
+        if (year100) leapYear = false;
+        if (year400) leapYear =  true;
+        
+        //Find the number of days in this year
+        int daysInYear = leapYear ? 366 : 365;
+        
+        //Stop if this is the final year
+        if (daysLeft < daysInYear) break;
+        
+        //Calculate the current day of the week at the start of the year
+        dayOfWeek += leapYear ? 2 : 1;
+        if (dayOfWeek >= 7) dayOfWeek -= 7;
+        
+        //Move on to the next year
+        daysLeft -= daysInYear;
+        year++;
+    }
+    
+    dayOfYear = daysLeft;
+    dayOfWeek += daysLeft;
+    dayOfWeek %= 7;
+
+    //Add a month at a time while there is more than a month of days left
+    int month = 0;
+    while(1)
+    {
+        int daysInMonth;
+        switch (month + 1)
+        {
+            case  1: daysInMonth = 31; break;
+            case  2: daysInMonth = leapYear ? 29 : 28; break;
+            case  3: daysInMonth = 31; break;
+            case  4: daysInMonth = 30; break;
+            case  5: daysInMonth = 31; break;
+            case  6: daysInMonth = 30; break;
+            case  7: daysInMonth = 31; break;
+            case  8: daysInMonth = 31; break;
+            case  9: daysInMonth = 30; break;
+            case 10: daysInMonth = 31; break;
+            case 11: daysInMonth = 30; break;
+            case 12: daysInMonth = 31; break;
+        }
+        
+        //Stop if this is the last month
+        if (daysLeft < daysInMonth) break;
+        
+        //Move onto next month
+        daysLeft -= daysInMonth;
+        month++;
+    }
+
+  ptm->tm_sec  = seconds;
+  ptm->tm_min  = minutes;
+  ptm->tm_hour = hours;
+  ptm->tm_mday = daysLeft + 1;
+  ptm->tm_mon  = month;
+  ptm->tm_year = year - 1900;
+  ptm->tm_wday = dayOfWeek;
+  ptm->tm_yday = dayOfYear;
+}
+void TimeGetTm(struct tm* ptm)
+{
+    uint32_t time = TimeGet();    
+    TimeToTm(time, ptm);
+}
 static Timer timer;
 int TimeScanUs = 0;
 int TimeMain()