Simple time management w/o the Network interface

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Thu Apr 30 14:38:57 2020 +0000
Parent:
0:e8e62dfee5eb
Commit message:
Code cleanup

Changed in this revision

TimeInterface.cpp Show annotated file Show diff for this revision Revisions of this file
TimeInterface.h Show annotated file Show diff for this revision Revisions of this file
--- a/TimeInterface.cpp	Fri Oct 11 20:53:30 2019 +0000
+++ b/TimeInterface.cpp	Thu Apr 30 14:38:57 2020 +0000
@@ -220,8 +220,8 @@
     sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d",
             wday_name[tmp.tm_wday % 7],
             mon_name[tmp.tm_mon % 12],
-            tmp.tm_mday, tmp.tm_hour,
-            tmp.tm_min, tmp.tm_sec,
+            tmp.tm_mday % 99, tmp.tm_hour % 25,
+            tmp.tm_min % 99, tmp.tm_sec % 99,
             1900 + tmp.tm_year);
     return result;
 }
@@ -421,17 +421,17 @@
 #define asizeof(a)      (sizeof (a) / sizeof ((a)[0]))
 
 struct dtconv {
-    char    *abbrev_month_names[12];
-    char    *month_names[12];
-    char    *abbrev_weekday_names[7];
-    char    *weekday_names[7];
-    char    *time_format;
-    char    *sdate_format;
-    char    *dtime_format;
-    char    *am_string;
-    char    *pm_string;
-    char    *ldate_format;
-    char    *zone_names[10];
+    const char    *abbrev_month_names[12];
+    const char    *month_names[12];
+    const char    *abbrev_weekday_names[7];
+    const char    *weekday_names[7];
+    const char    *time_format;
+    const char    *sdate_format;
+    const char    *dtime_format;
+    const char    *am_string;
+    const char    *pm_string;
+    const char    *ldate_format;
+    const char    *zone_names[10];
     int8_t  zone_offsets[10];
 };
 
@@ -471,10 +471,12 @@
 #endif
 
 
-const char * TimeInterface::strptime(const char *buf, char *fmt, struct tm_ex *tm)
+const char * TimeInterface::strptime(const char *buf, const char *fmt, struct tm_ex *tm)
 {
-    char c, *ptr;
+    char c;
+    const char *ptr;
     int i, len;
+    size_t ndx;
     bool fSet_wday = false;     // so we can notice if the wday was set
     
     ptr = fmt;
@@ -637,23 +639,19 @@
 
             case 'A':
             case 'a':
-                for (i = 0; i < asizeof(En_US.weekday_names); i++) {
-                    len = strlen(En_US.weekday_names[i]);
-                    if (strncasecmp(buf,
-                                    En_US.weekday_names[i],
-                                    len) == 0)
+                for (ndx = 0; ndx < asizeof(En_US.weekday_names); ndx++) {
+                    len = strlen(En_US.weekday_names[ndx]);
+                    if (strncasecmp(buf, En_US.weekday_names[ndx], len) == 0)
                         break;
 
-                    len = strlen(En_US.abbrev_weekday_names[i]);
-                    if (strncasecmp(buf,
-                                    En_US.abbrev_weekday_names[i],
-                                    len) == 0)
+                    len = strlen(En_US.abbrev_weekday_names[ndx]);
+                    if (strncasecmp(buf, En_US.abbrev_weekday_names[ndx], len) == 0)
                         break;
                 }
-                if (i == asizeof(En_US.weekday_names))
+                if (ndx == asizeof(En_US.weekday_names))
                     return 0;
                 fSet_wday = true;
-                tm->tm_wday = i;
+                tm->tm_wday = ndx;
                 buf += len;
                 break;
 
@@ -679,23 +677,19 @@
             case 'B':
             case 'b':
             case 'h':
-                for (i = 0; i < asizeof(En_US.month_names); i++) {
-                    len = strlen(En_US.month_names[i]);
-                    if (strncasecmp(buf,
-                                    En_US.month_names[i],
-                                    len) == 0)
+                for (ndx = 0; ndx < asizeof(En_US.month_names); ndx++) {
+                    len = strlen(En_US.month_names[ndx]);
+                    if (strncasecmp(buf, En_US.month_names[ndx], len) == 0)
                         break;
 
-                    len = strlen(En_US.abbrev_month_names[i]);
-                    if (strncasecmp(buf,
-                                    En_US.abbrev_month_names[i],
-                                    len) == 0)
+                    len = strlen(En_US.abbrev_month_names[ndx]);
+                    if (strncasecmp(buf, En_US.abbrev_month_names[ndx], len) == 0)
                         break;
                 }
-                if (i == asizeof(En_US.month_names))
+                if (ndx == asizeof(En_US.month_names))
                     return 0;
 
-                tm->tm_mon = i;
+                tm->tm_mon = ndx;
                 buf += len;
                 break;
 
@@ -741,16 +735,14 @@
                         ptr++;
                 break;
             case 'Z':
-                for (i = 0; i < asizeof(En_US.zone_names); i++) {
-                    len = strlen(En_US.zone_names[i]);
-                    if (strncasecmp(buf,
-                                    En_US.zone_names[i],
-                                    len) == 0)
+                for (ndx = 0; ndx < asizeof(En_US.zone_names); ndx++) {
+                    len = strlen(En_US.zone_names[ndx]);
+                    if (strncasecmp(buf, En_US.zone_names[ndx], len) == 0)
                         break;
                 }
-                if (i == asizeof(En_US.zone_names))
+                if (ndx == asizeof(En_US.zone_names))
                     return 0;
-                tm->tm_tzo_min = En_US.zone_offsets[i] * 60;
+                tm->tm_tzo_min = En_US.zone_offsets[ndx] * 60;
                 buf += len;
                 break;
         }
--- a/TimeInterface.h	Fri Oct 11 20:53:30 2019 +0000
+++ b/TimeInterface.h	Thu Apr 30 14:38:57 2020 +0000
@@ -417,7 +417,7 @@
     ///         have been defined.
     ///     - %% Replaced by %.
     ///
-    const char * strptime(const char *buf, char *fmt, struct tm_ex *tm);
+    const char * strptime(const char *buf, const char *fmt, struct tm_ex *tm);
 
 
     // time zone functions
@@ -586,7 +586,7 @@
 
     dst_event_pair_t dst_pair;
     bool dst;           // true in dst mode
-    char result[30];    // holds the converted to text time string
+    char result[33];    // holds the converted to text time string
     time_t tresult;     // holds the converted time structure.
     struct tm_ex tm_ext;
     };