Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Sat Dec 01 19:13:29 2018 +0000
Parent:
36:6a8a8e1951d4
Child:
38:25b2a3c494aa
Commit message:
Modified to remove dependence on time_t (32bit) and used an int64 instead.

Changed in this revision

clock/clktime.c Show annotated file Show diff for this revision Revisions of this file
clock/clktime.h Show annotated file Show diff for this revision Revisions of this file
clock/clktm.c Show annotated file Show diff for this revision Revisions of this file
clock/clktm.h Show annotated file Show diff for this revision Revisions of this file
clock/clock.c Show annotated file Show diff for this revision Revisions of this file
clock/clock.h Show annotated file Show diff for this revision Revisions of this file
tm/tm.c Show annotated file Show diff for this revision Revisions of this file
tm/tm.h Show annotated file Show diff for this revision Revisions of this file
--- a/clock/clktime.c	Sat Dec 01 17:24:49 2018 +0000
+++ b/clock/clktime.c	Sat Dec 01 19:13:29 2018 +0000
@@ -23,17 +23,6 @@
     return tickCount + slewCount + TimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb + ClockSlew, TimerSinceCount(secondBaseCount), TIMER_COUNT_PER_SECOND);
 }
 
-void    TimeToTmUtc  (int64_t ticks, struct tm* ptm)
-{
-    time_t t = ticks >> TIME_ONE_SECOND_SHIFT;
-    TmUtcFromTimeT(t, ptm);
-}
-int64_t TimeFromTmUtc(struct tm* ptm)
-{
-    time_t t = TmUtcToTimeT(ptm);
-    return t << TIME_ONE_SECOND_SHIFT;
-}
-
 void TimeSet(int64_t extClock)
 {    
      int64_t timerCountSinceLastSecond = TimerSinceCount(secondBaseCount);
--- a/clock/clktime.h	Sat Dec 01 17:24:49 2018 +0000
+++ b/clock/clktime.h	Sat Dec 01 19:13:29 2018 +0000
@@ -1,9 +1,5 @@
 #include <stdint.h>
 #include <stdbool.h>
-#include <time.h>
-
-extern void     TimeToTmUtc  (int64_t clk, struct tm* ptm);
-extern int64_t  TimeFromTmUtc(struct tm* ptm);
 
 extern void     TimeSaveSnapshot(void);
 extern void     TimesGetFromSnapshot(int64_t* pInt, int64_t* pAbs);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clock/clktm.c	Sat Dec 01 19:13:29 2018 +0000
@@ -0,0 +1,9 @@
+#include <stdint.h>
+#include <time.h>
+
+#include "tm.h"
+#include "clktime.h"
+
+void    TimeToTmLocal(int64_t time, struct tm* ptm) {    TmLocalFromSeconds1970(time >> TIME_ONE_SECOND_SHIFT, ptm); }
+void    TimeToTmUtc  (int64_t time, struct tm* ptm) {      TmUtcFromSeconds1970(time >> TIME_ONE_SECOND_SHIFT, ptm); }
+int64_t TimeFromTmUtc(              struct tm* ptm) { return TmUtcToSeconds1970(ptm) << TIME_ONE_SECOND_SHIFT; }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clock/clktm.h	Sat Dec 01 19:13:29 2018 +0000
@@ -0,0 +1,6 @@
+#include <time.h>
+#include <stdint.h>
+
+extern void    TimeToTmLocal(int64_t time, struct tm* ptm);
+extern void    TimeToTmUtc  (int64_t time, struct tm* ptm);
+extern int64_t TimeFromTmUtc(              struct tm* ptm);
\ No newline at end of file
--- a/clock/clock.c	Sat Dec 01 17:24:49 2018 +0000
+++ b/clock/clock.c	Sat Dec 01 19:13:29 2018 +0000
@@ -5,7 +5,7 @@
 #include  "clktime.h"
 #include "clkstate.h"
 #include  "clksync.h"
-#include       "tm.h"
+#include    "clktm.h"
 #include      "rtc.h"
 #include    "timer.h"
 #include     "tick.h"
@@ -13,21 +13,21 @@
 
 #define ONE_BILLION 1000000000
 
-static int64_t nowTicks = 0;
-static time_t  nowT     = 0;
+static int64_t timeNow = 0;
 static int64_t refTicks = 0;
 
+int64_t ClockTime    () { return timeNow; }
 int64_t ClockRefTicks() { return refTicks; }
-bool    ClockIsSet() {    return TimeIsSet(); }
+bool    ClockIsSet   () { return TimeIsSet(); }
 bool    ClockIsSynced() { return SyncedRate && SyncedTime; }
 
-void    ClockTmLocal(struct tm* ptm) { TmLocalFromTimeT(nowT, ptm); }
-void    ClockTmUtc  (struct tm* ptm) {   TmUtcFromTimeT(nowT, ptm); }
+void    ClockTmLocal(struct tm* ptm) { TimeToTmLocal(timeNow, ptm); }
+void    ClockTmUtc  (struct tm* ptm) { TimeToTmUtc  (timeNow, ptm); }
 
 void ClockAscii(char* p)
 {
     struct tm tm;
-    TmUtcFromTimeT(nowT, &tm);
+    TimeToTmUtc(timeNow, &tm);
     sprintf(p, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
 }
 void ClockInit()
@@ -51,19 +51,19 @@
     if (hadSecond) TimeIncrementByOneSecond(secondsBaseCount);
 
     //Calculate ns and time_t
-    nowTicks = TimeNow();
-    nowT = nowTicks >> TIME_ONE_SECOND_SHIFT;
+    timeNow = TimeNow();
     
     //Establish the scan times
     ScanMain();
 
     //Record the time the clock started
-    if (SyncedTime && SyncedRate) refTicks = nowTicks;
+    if (SyncedTime && SyncedRate) refTicks = timeNow;
 
     //Set a one shot memory for having had a tick
-    static time_t lastT = 0;
-    ClockTicked = lastT > 0 && lastT != nowT;
-    lastT = nowT;
+    static int lastT = 0;
+    int        thisT = timeNow >> TIME_ONE_SECOND_SHIFT;
+    ClockTicked = lastT > 0 && lastT != thisT;
+    lastT = thisT;
 
     if (TimeIsSet())
     {
@@ -85,10 +85,8 @@
             RtcGetTm(&tm);
             if (lastRtcSecond > 0 && tm.tm_sec != lastRtcSecond)
             {
-                time_t t = TmUtcToTimeT(&tm);
-                TimeSet((int64_t)t << TIME_ONE_SECOND_SHIFT);
-                nowTicks = TimeNow();
-                nowT = nowTicks >> TIME_ONE_SECOND_SHIFT;
+                timeNow = TimeFromTmUtc(&tm);
+                TimeSet(timeNow);
                 LogTimeF("Clock set from RTC\r\n");
             }
             lastRtcSecond = tm.tm_sec;
--- a/clock/clock.h	Sat Dec 01 17:24:49 2018 +0000
+++ b/clock/clock.h	Sat Dec 01 19:13:29 2018 +0000
@@ -7,6 +7,7 @@
 extern int     ClockIsSynced(void);
 extern void    ClockTmLocal(struct tm* ptm);
 extern void    ClockTmUtc  (struct tm* ptm);
+extern int64_t ClockTime(void);
 extern bool    ClockTicked;
 
 extern void     ClockInit(void);
--- a/tm/tm.c	Sat Dec 01 17:24:49 2018 +0000
+++ b/tm/tm.c	Sat Dec 01 19:13:29 2018 +0000
@@ -1,12 +1,14 @@
+#include <time.h>
 #include <stdlib.h>
-#include <time.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdbool.h>
+#include <stdint.h>
 
 #define STD_OFFSET 0
 #define DST_OFFSET 1
 
+
 static bool isLeapYear(int year)
 {
     year += 1900;
@@ -81,39 +83,6 @@
     *pDayOfYear = dayOfYear;     // 0 --> 365
     *pDayOfWeek = dayOfWeek % 7; // 0 --> 6
 }
-void TmIncrement(struct tm* ptm)
-{
-    ptm->tm_sec++;
-    if (ptm->tm_sec > 59)
-    {
-        ptm->tm_sec = 0;
-        ptm->tm_min++;
-    }
-    if (ptm->tm_min > 59)
-    {
-        ptm->tm_min = 0;
-        ptm->tm_hour++;
-    }
-    if (ptm->tm_hour > 23)
-    {
-        ptm->tm_hour = 0;
-        ptm->tm_wday++;
-        if (ptm->tm_wday > 6) ptm->tm_wday = 0;
-        ptm->tm_yday++;
-        ptm->tm_mday++;
-        if (ptm->tm_mday > monthLength(ptm->tm_year, ptm->tm_mon))
-        {
-            ptm->tm_mon++;
-            if (ptm->tm_mon > 11)
-            {
-                ptm->tm_year++;
-                ptm->tm_yday = 0;
-                ptm->tm_mon = 0;
-            }
-            ptm->tm_mday = 1;
-        }
-    }
-}
 static void normalise(int* pHours, int* pDayOfWeek, int* pDayOfMonth, int* pMonth, int * pDayOfYear, int* pYear)
 {
     if (*pHours > 23)
@@ -192,7 +161,7 @@
         ++*pMonth;
     }
 }
-static void timeToTm(time_t t, struct tm* ptm, bool local)
+static void timeToTm(int64_t t, struct tm* ptm, bool local)
 {
     //Extract the seconds, minutes, hours and days from the time_t t
     div_t divres;
@@ -245,17 +214,19 @@
     ptm->tm_yday  = dayOfYear;     // 0 --> 365
     ptm->tm_isdst = dst;           // +ve if DST, 0 if not DSTime, -ve if the information is not available. Note that 'true' evaluates to +1.
 }
-void TmUtcFromTimeT(time_t time, struct tm* ptm)
+
+
+void TmUtcFromSeconds1970(int64_t time, struct tm* ptm)
 {
     timeToTm(time, ptm, false);
 }
-void TmLocalFromTimeT(time_t time, struct tm* ptm)
+void TmLocalFromSeconds1970(int64_t time, struct tm* ptm)
 {
     timeToTm(time, ptm, true);
 }
-time_t TmUtcToTimeT(struct tm* ptm)
+int64_t TmUtcToSeconds1970(struct tm* ptm)
 {
-    int days = 0;
+    int64_t days = 0;
     
     for (int y = 70; y < ptm->tm_year; y++) days += isLeapYear(y) ? 366 : 365;
     
@@ -266,6 +237,8 @@
            ptm->tm_min  *    60 + 
            ptm->tm_sec;
 }
+
+
 int TmSecondsBetween(struct tm* ptmLater, struct tm* ptmEarlier)
 {
     int days = 0;
@@ -307,3 +280,36 @@
     //Fill the day of week and the day of year part of the tm structure
     calculateDayOfYearAndWeek(ptm->tm_year, ptm->tm_mon, ptm->tm_mday, &ptm->tm_yday, &ptm->tm_wday);
 }
+void TmIncrement(struct tm* ptm)
+{
+    ptm->tm_sec++;
+    if (ptm->tm_sec > 59)
+    {
+        ptm->tm_sec = 0;
+        ptm->tm_min++;
+    }
+    if (ptm->tm_min > 59)
+    {
+        ptm->tm_min = 0;
+        ptm->tm_hour++;
+    }
+    if (ptm->tm_hour > 23)
+    {
+        ptm->tm_hour = 0;
+        ptm->tm_wday++;
+        if (ptm->tm_wday > 6) ptm->tm_wday = 0;
+        ptm->tm_yday++;
+        ptm->tm_mday++;
+        if (ptm->tm_mday > monthLength(ptm->tm_year, ptm->tm_mon))
+        {
+            ptm->tm_mon++;
+            if (ptm->tm_mon > 11)
+            {
+                ptm->tm_year++;
+                ptm->tm_yday = 0;
+                ptm->tm_mon = 0;
+            }
+            ptm->tm_mday = 1;
+        }
+    }
+}
--- a/tm/tm.h	Sat Dec 01 17:24:49 2018 +0000
+++ b/tm/tm.h	Sat Dec 01 19:13:29 2018 +0000
@@ -1,11 +1,11 @@
+#include <stdint.h>
 #include <time.h>
-#include <stdint.h>
-
-extern void   TmUtcToLocal(struct tm* ptm);
 
-extern void   TmFromAsciiDateTime(const char* pDate, const char* pTime, struct tm* ptm);
-extern void   TmLocalFromTimeT(uint32_t time, struct tm* ptm);
-extern void   TmUtcFromTimeT(uint32_t time, struct tm* ptm);
+extern void    TmUtcFromSeconds1970  (int64_t time, struct tm* ptm);
+extern void    TmLocalFromSeconds1970(int64_t time, struct tm* ptm);
+extern int64_t TmUtcToSeconds1970    (              struct tm* ptm);
 
-extern time_t TmUtcToTimeT(struct tm* ptm);
-extern int    TmSecondsBetween(struct tm* ptmLater, struct tm* ptmEarlier);
\ No newline at end of file
+extern void    TmUtcToLocal(struct tm* ptm);
+extern void    TmFromAsciiDateTime(const char* pDate, const char* pTime, struct tm* ptm);
+extern int     TmSecondsBetween(struct tm* ptmLater, struct tm* ptmEarlier);
+extern void    TmIncrement(struct tm* ptm);
\ No newline at end of file