ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
time.c
Go to the documentation of this file.
1 
25 #include "user_config.h"
26 
27 #ifdef AVR
28 #include <stdlib.h>
29 #include <string.h>
30 #endif
31 
32 #include "fatfs.h"
33 
34 #include "printf/mathio.h"
35 
36 #include "time.h"
37 #include "timer.h"
38 
39 #ifdef RTC_SUPPORT
40 #include "rtc.h"
41 #endif
42 
43 #include "posix.h"
44 
46 extern volatile ts_t __clock;
47 
50 
53 
59 static const uint16_t __days_sum[] =
60 {
61  0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
62 };
63 
68 static const uint16_t __days[] =
69 {
70  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
71 };
72 
78 const char *__WDay[] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat","BAD"};
79 
87 char *tm_wday_to_ascii(int i)
88 {
89  if(i >= 0 && i <= 6)
90  return((char *)__WDay[i]);
91  else
92  return((char *)__WDay[7]);
93 }
94 
95 
100 const char *__Month[]= \
101 { \
102  "Jan","Feb","Mar","Apr","May","Jun","Jul", \
103  "Aug","Sep","Oct","Nov","Dec","BAD"
104 };
105 
113 MEMSPACE
114 char *tm_mon_to_ascii(int i)
115 {
116  if(i >= 0 && i <= 11)
117  return((char *)__Month[i]);
118  else
119  return((char *)__Month[12]);
120 }
121 
122 
134 MEMSPACE
135 static int IS_Leap(int year)
136 {
137  if((year & 3) || year == 1900 || year == 2100)
138  return(0);
139  return(1);
140 }
141 
142 
155 MEMSPACE
156 static int Leap_Days_Since_1900(int year)
157 {
158  int sum;
159 
160  year -= 1900;
161 
162  if(year>0)
163  --year;
164 
165  sum = (year >> 2);
166 
167 
168  if(year >= 200 )
169  --sum;
170 
171  return(sum);
172 }
173 
179 MEMSPACE
180 int finddayofweek(int year, int month, int day)
181 {
182  int value;
183 
184  if ( month < 3 )
185  {
186  month += 12;
187  year -= 1;
188  }
189  value = ( year + year/4 - year/100 + year/400 );
190  value += ( 6 * (month+1) / 10 + (month * 2));
191  value += day;
192  value += 1;
193  return( value % 7 );
194 }
195 
196 
197 
210 MEMSPACE
211 static int Days_Per_Year(int year)
212 {
213  return(IS_Leap(year) ? 366 : 365);
214 }
215 
222 MEMSPACE
223 int Days_Per_Month(int month, int year)
224 {
225  int days;
226 
227  // Normalize month
228  while(month >= 12)
229  {
230  ++year;
231  month -= 12;
232  }
233  while(month < 0)
234  {
235  --year;
236  month += 12;
237  }
238  days = __days[month];
239  if( month == 1 && IS_Leap(year))
240  ++days;
241  return( days );
242 }
243 
253 
260 
261 MEMSPACE
262 time_t time_to_tm(time_t epoch, int32_t offset, tm_t *t)
263 {
264  int year,month,tmp;
265  int flag = 0;
266  int32_t days;
267  time_t save = epoch;
268 
269  memset(t,0,sizeof(tm_t));
270 
271  if(epoch >= 0xFFFD5D00UL)
272  return(-1);
273 
274  epoch -= offset;
275 
276  if(epoch >= 0xFFFEAE80UL)
277  {
278  epoch -= 0xFFFEAE80UL;
279  flag = 1;
280  }
281 
282  t->tm_sec = epoch % 60;
283  epoch /= 60;
284 
285  t->tm_min = epoch % 60;
286  epoch /= 60;
287 
288  t->tm_hour = epoch % 24;
289  epoch /= 24;
290 
291  days = epoch;
292 
293  if(flag)
294  {
295  t->tm_year = 69;
296  t->tm_mon = 11;
297  t->tm_mday = 31;
298  t->tm_wday = (EPOCH_DAY - 1) % 7;
299  }
300  else
301  {
302  t->tm_wday = (EPOCH_DAY + days) % 7;
303 
304  year=EPOCH_YEAR;
305  while (days >= (tmp=Days_Per_Year(year)) )
306  {
307  ++year;
308  days -= tmp;
309  }
310 
311  t->tm_year = year - 1900;
312  t->tm_yday = days;
313 
314  month = 0;
315 
316  while(days > 0 && month < 12)
317  {
318  tmp=__days[month];
319  if( month == 1 && IS_Leap(year))
320  ++tmp;
321  if(days < tmp)
322  break;
323  days -= tmp;
324  ++month;
325  }
326 
327  t->tm_mon = month;
328  t->tm_mday = days + 1;
329  }
330  return(save - offset);
331 }
332 
333 
346 MEMSPACE
348 {
349  time_t seconds;
350  seconds = normalize(t,0);
351  return (seconds);
352 }
353 
354 // =============================================
356 // =============================================
357 
358 
366 MEMSPACE
367 char *asctime_r(tm_t *t, char *buf)
368 {
369  // normaize tm_t before output
370  (void) normalize(t,0);
371 
372  memset(buf,0,32);
373  snprintf(buf,32,"%s %s %2d %02d:%02d:%02d %4d",
374  __WDay[t->tm_wday],
375  __Month[t->tm_mon],
376  (int)t->tm_mday,
377  (int)t->tm_hour,
378  (int)t->tm_min,
379  (int)t->tm_sec,
380  (int)t->tm_year + 1900);
381  return(buf);
382 }
383 
384 
385 
393 MEMSPACE
394 char *asctime(tm_t *t)
395 {
396  static char buf[32];
397  // acstime_r does tm_t normalization
398  return( asctime_r(t,buf) );
399 }
400 
401 
409 MEMSPACE
410 char *ctime_r(time_t *t, char *buf)
411 {
412  // acstime_r does tm_t normalization
413  return( asctime_r(localtime(t),buf) );
414 }
415 
416 
423 MEMSPACE
424 char *ctime(time_t *tp)
425 {
426  static char buf[32];
427  // acstime_r does tm_t normalization
428  return( asctime_r( localtime(tp), buf) );
429 }
430 
431 
440 MEMSPACE
441 char *ctime_gm(time_t *tp)
442 {
443  static char buf[32];
444  tm_t tm;
445  return( asctime_r( gmtime_r(tp,&tm), buf) );
446 }
447 
448 
455 MEMSPACE
456 tm_t *gmtime_r(time_t *t, tm_t *result)
457 {
458  time_t epoch = *t;
459  (void)time_to_tm(epoch, 0, result);
460  return(result);
461 }
462 
463 
470 MEMSPACE
472 {
473  static tm_t t, *p;
474  p = &t;
475  gmtime_r(tp, p);
476  return(p);
477 }
478 
479 
480 // FIXME localtime
481 /*
482  The localtime function converts the calendar time timep to broken-down time representation, expressed relative to the users specified timezone.
483  The function acts as if it called tzset(3) and sets the external variables tzname with information about the current timezone, timezone with the difference
484  between Coordinated Universal Time (UTC) and local standard time in seconds, and daylight to a nonzero value if daylight savings time rules apply during
485  some part of the year. The return value points to a statically allocated struct which might be overwritten by subsequent calls to any of the date and
486  time functions. The localtime_r() function does the same, but stores the data in a user-supplied struct. It need not set tzname, timezone, and day‐
487  light.
488 */
489 
490 
498 MEMSPACE
500 {
501  tz_t tz;
502  long int offset;
503  time_t epoch = *t;
504 
505  gettimezone(&tz);
506  offset = 60L * tz.tz_minuteswest;
507 
508  if(is_dst(epoch - offset))
509  offset -= 3600L;
510  (void) time_to_tm(epoch, offset, result);
511 
512  return(result);
513 }
514 
521 MEMSPACE
523 {
524  static struct tm t;
525  return( localtime_r(tp, &t) );
526 }
527 
533 MEMSPACE
535 {
536  return( normalize(t, 1) );
537 }
538 
539 
550 MEMSPACE
551 static
553 {
554  time_t days,seconds;
555 
556  int year = t->tm_year + 1900;
557  int mon = t->tm_mon; // 0..11
558  int mday = t->tm_mday - 1; // 1..28,29,30,31
559  int hour = t->tm_hour; // 0..23
560  int min = t->tm_min; // 0..59
561  int sec = t->tm_sec; // 0..59
562 
563 #ifdef TIME_DEBUG
564  printf("tm2epoch %4d,%2d,%2d, %02d:%02d:%02d\n",
565  (int)t->tm_year+1900, (int)t->tm_mon, (int)t->tm_mday,
566  (int)t->tm_hour, (int)t->tm_min, (int)t->tm_sec);
567 #endif
568 
569  if (year < EPOCH_YEAR || year > 2106)
570  {
571 #ifdef TIME_DEBUG
572  printf("tm2epoch year out of range: %4d\n", year);
573 #endif
574  return(-1);
575  }
576 
577  if(mon >= 12 || mon < 0)
578  {
579 #ifdef TIME_DEBUG
580  printf("tm2epoch mon out of range: %4d\n", mon);
581 #endif
582  return(-1);
583  }
584 
585  if(mday >= Days_Per_Month(mon,year) || mday < 0)
586  {
587 #ifdef TIME_DEBUG
588  printf("tm2epoch mday out of range: %4d\n", mday);
589 #endif
590  return(-1);
591  }
592 
593  if(hour >= 24 || hour < 0)
594  {
595 #ifdef TIME_DEBUG
596  printf("tm2epoch hour out of range: %4d\n", hour);
597 #endif
598  return(-1);
599  }
600 
601  if(min >= 60|| min < 0)
602  {
603 #ifdef TIME_DEBUG
604  printf("tm2epoch min out of range: %4d\n", min);
605 #endif
606  return(-1);
607  }
608 
609  if(sec >= 60 || sec < 0)
610  {
611 #ifdef TIME_DEBUG
612  printf("tm2epoch sec out of range: %4d\n", sec);
613 #endif
614  return(-1);
615  }
616 
618 
619  days = ( year - EPOCH_YEAR );
620 
621  days *= (time_t) 365L;
622 
623  days += (time_t) __days_sum[mon];
624 
625  days += (time_t) mday;
626 
627  days += (time_t) Leap_Days_Since_1900(year);
628 
629  days -= (time_t) 17;
630 
631  if(mon > 1 && IS_Leap(year))
632  ++days;
633 
634  seconds = days;
635 
636  seconds *= (time_t) 24L; // hours
637  seconds += (time_t) hour;
638  seconds *= (time_t) 60L; // Minutes
639  seconds += (time_t) min;
640  seconds *= (time_t) 60L; // Seconds
641  seconds += (time_t) sec;
642  return (seconds);
643 }
644 
645 
653 MEMSPACE
654 time_t normalize(tm_t *t, int normalize_to_timezone)
655 {
656  time_t epoch;
657  int32_t offset;
658  int isdst;
659 
660 // struct tm
661 // {
662 // int tm_sec; /*< Seconds. [0-60] (1 leap second) */
663 // int tm_min; /*< Minutes. [0-59] */
664 // int tm_hour; /*< Hours. [0-23] */
665 // int tm_mday; /*< Day. [1-31] */
666 // int tm_mon; /*< Month. [0-11] */
667 // int tm_year; /*< Year - 1900. */
668 // int tm_wday; /*< Day of week. [0-6] */
669 // int tm_yday; /*< Days in year.[0-365] */
670 // int tm_isdst; /*< DST. [-1/0/1] */
671 // };
672 
673  // Normalize t->tm_seconds
674  while(t->tm_sec >= 60)
675  {
676  ++t->tm_min;
677  t->tm_sec -= 60;
678  }
679  while(t->tm_sec < 0)
680  {
681  --t->tm_min;
682  t->tm_sec += 60;
683  }
684 
685  // Normalize t->tm_miniutes
686  while(t->tm_min >= 60)
687  {
688  ++t->tm_hour;
689  t->tm_min -= 60;
690  }
691  while(t->tm_min < 0)
692  {
693  --t->tm_hour;
694  t->tm_min += 60;
695  }
696 
697  // Normalize t->tm_hours
698  while(t->tm_hour >= 24)
699  {
700  ++t->tm_mday;
701  t->tm_hour -= 24;
702  }
703  while(t->tm_hour < 0)
704  {
705  --t->tm_mday;
706  t->tm_hour += 24;
707  }
708 
709  // Normalize t->tm_months
710  while(t->tm_mon >= 12)
711  {
712  ++t->tm_year;
713  t->tm_mon -= 12;
714  }
715  while(t->tm_mon < 0)
716  {
717  --t->tm_year;
718  t->tm_mon += 12;
719  }
720 
721  // Normalize t->tm_mday
722  // t->tm_mday is 1 based
723  while(t->tm_mday > Days_Per_Month(t->tm_mon,t->tm_year) )
724  {
725  // subtract days in current t->tm_month
726  t->tm_mday -= Days_Per_Month(t->tm_mon,t->tm_year);
727  // Keep month normalized
728  if(++t->tm_mon >= 12)
729  {
730  t->tm_mon -= 12;
731  ++t->tm_year;
732  }
733  }
734 
735  // t->tm_mday is 1 based
736  while(t->tm_mday < 1)
737  {
738  // Keep month normalized
739  if(--t->tm_mon < 0)
740  {
741  t->tm_mon += 12;
742  --t->tm_year;
743  }
744  // add days in previous mount
745  t->tm_mday += Days_Per_Month(t->tm_mon,t->tm_year);
746  }
747 
748 
749  // We can now set the remain values by converting to EPOCH and back again
750  // convert to EPOCH based seconds
751  epoch = tm2epoch( t );
752 #ifdef TIME_DEBUG
753  printf("%10lu - epoch-1\n",epoch);
754 #endif
755  // Normaize to local timezone with DST
756  offset = 0L;
757  isdst = 0;
758  if(normalize_to_timezone)
759  {
760  tz_t tz;
761 
762  gettimezone(&tz);
763  offset = tz.tz_minuteswest * 60L;
764  if(is_dst(epoch))
765  {
766  offset -= 3600L;
767  isdst = 1;
768  }
769  }
770 #ifdef TIME_DEBUG
771  printf("%10lu - epoch-2\n",epoch);
772 #endif
773 
774  // convert back to TM structure
775  epoch = time_to_tm(epoch, offset, t);
776  if(isdst)
777  t->tm_isdst = 1;
778 
779 #ifdef TIME_DEBUG
780  printf("%10lu - epoch-3\n",epoch);
781 #endif
782  return( epoch );
783 }
784 
785 
791 MEMSPACE
793 {
794  tz->tz_minuteswest = __tzone.tz_minuteswest;
795  tz->tz_dsttime = __tzone.tz_dsttime;
796  return(0);
797 }
798 
799 
805 MEMSPACE
807 {
808  __tzone.tz_minuteswest = tz->tz_minuteswest;
809  __tzone.tz_dsttime = tz->tz_dsttime;
810  return(0);
811 }
812 
813 
821 MEMSPACE
822 int gettimeofday(tv_t *tv, tz_t *tz)
823 {
824  ts_t ts;
825 
826  clock_gettime(0, (ts_t *) &ts);
827 
828  tv->tv_sec = ts.tv_sec;
829  tv->tv_usec = ts.tv_nsec / 1000L;
830 
831  gettimezone(tz);
832  return(0);
833 }
834 
835 
842 MEMSPACE
844 {
845  static ts_t ts;
846  clock_gettime(0, (ts_t *) &ts);
847  if(t != NULL)
848  *t = ts.tv_sec;
849  return(ts.tv_sec);
850 }
851 
852 
860 MEMSPACE
861 int settimeofday(tv_t *tv, tz_t *tz)
862 {
863  ts_t ts;
864 
865  ts.tv_sec = tv->tv_sec;
866  ts.tv_nsec = tv->tv_usec * 1000L;
867 
868  clock_settime(0, (ts_t *) &ts);
869 
870  settimezone(tz);
871 
872  return(0);
873 }
874 
875 
881 MEMSPACE
883 {
884  ts_t ts;
885  ts.tv_sec = seconds;
886  ts.tv_nsec = us * 1000L;
887 
888 //FIXME we may want to add hooks to the OS time functions if they exist
889  clock_settime(0, (ts_t *) &ts);
890 }
891 
892 
900 MEMSPACE
901 int setdate (void)
902 {
903  char buf[40];
904  extern MEMSPACE char *fgets ( char *str , int size , FILE *stream );
905 
906  printf("Enter date YYYY MM DD HH:MM:SS >");
907  fgets(buf,39,stdin);
908 
909  return(setdate_r(buf));
910 }
911 
918 MEMSPACE
919 int setdate_r (char *buf)
920 {
921  tm_t tm;
922  ts_t ts;
923  time_t seconds;
924 
925  tm.tm_year=tm.tm_mon=tm.tm_mday=tm.tm_hour=tm.tm_min=tm.tm_sec=0;
926 
927 #ifdef SMALL_SSCANF
928  sscanf(buf,"%d %d %d %d:%d:%d",
929  &tm.tm_year,
930  &tm.tm_mon,
931  &tm.tm_mday,
932  &tm.tm_hour,
933  &tm.tm_min,
934  &tm.tm_sec);
935 #else
936  // Year
937  while(*buf == ' ')
938  ++buf;
939  tm.tm_year = strtol(buf,&buf,10);
940 
941  // Month
942  while(*buf == ' ')
943  ++buf;
944  tm.tm_mon = strtol(buf,&buf,10);
945 
946  // Day of Month
947  while(*buf == ' ')
948  ++buf;
949  tm.tm_mday = strtol(buf,&buf,10);
950 
951  // Hour
952  while(*buf == ' ')
953  ++buf;
954  tm.tm_hour = strtol(buf,&buf,10);
955 
956  // Minute
957  if(*buf && (*buf == ' ' || *buf == ':'))
958  ++buf;
959  tm.tm_min = strtol(buf,&buf,10);
960 
961  // Second
962  if(*buf && (*buf == ' ' || *buf == ':'))
963  ++buf;
964  tm.tm_sec = strtol(buf,&buf,10);
965 #endif
966 
967  tm.tm_mon--;
968 
969  if(tm.tm_year < 1970 || tm.tm_year > 2038)
970  {
971  printf("invalid year: %d\n",tm.tm_year);
972  return(-1);
973  }
974  if(tm.tm_year >= 1900)
975  tm.tm_year -= 1900;
976  if(tm.tm_mon < 0 || tm.tm_mon > 11)
977  {
978  printf("invalid mon: %d\n",tm.tm_year);
979  return(-1);
980  }
981  if(tm.tm_mday < 1 || tm.tm_mday > 31)
982  {
983  printf("invalid day: %d\n",tm.tm_mday);
984  return(-1);
985  }
986  if(tm.tm_hour < 0 || tm.tm_hour > 23)
987  {
988  printf("invalid hour: %d\n",tm.tm_hour);
989  return(-1);
990  }
991  if(tm.tm_min < 0 || tm.tm_min > 59)
992  {
993  printf("invalid min: %d\n",tm.tm_min);
994  return(-1);
995  }
996 
997  seconds = timegm(&tm);
998 
999  ts.tv_sec = seconds;
1000  ts.tv_nsec = 0L;
1001  clock_settime(0, (ts_t *) &ts);
1002 
1003 #ifdef RTC_SUPPORT
1004  if( !rtc_init(1, (time_t) seconds ) )
1005  {
1006  printf("rtc force init failed\n");
1007  return(-1);
1008  }
1009 #endif
1010  return(0);
1011 }
1012 
1037 MEMSPACE
1038 time_t find_dst(int dst, time_t epoch, int year, int month, int weekno, int dayno, int hour)
1039 {
1040  tm_t t;
1041  tz_t tz;
1042  tv_t tv;
1043  time_t dst_epoch;
1044  int32_t offset;
1045  int days;
1046 
1047  // Get timezone and clock time
1048  gettimeofday(&tv, &tz);
1049 
1050  // Local time offset in seconds
1051  // Get local timezone offset in seconds without DST offset
1052  offset = tz.tz_minuteswest * 60UL;
1053  // Add DST offset if DST end time includes DST offset
1054  if(dst)
1055  offset -= (60UL * 60UL);
1056 
1057  if(year)
1058  {
1059  // Year is specified - use it
1060  t.tm_year = year - 1900; // 0 = 1900
1061  }
1062  else
1063  {
1064  // Otherwise, Calculate year from epoch or GMT time
1065  if(!epoch)
1066  epoch = tv.tv_sec;
1067  (void) time_to_tm(epoch, offset, &t);
1068  }
1069 
1070  // Local time of DST
1071  // We compute seconds for start of month in which DST happens
1072  // Result from timegm() is in GMT
1073  t.tm_mon = month - 1; // 0..11
1074  t.tm_mday = 1; // 1..28,29,30,31
1075  t.tm_hour = hour; // 0 .. 23
1076  t.tm_min = 0; // 0..59
1077  t.tm_sec = 0; // 0..59
1078 
1079 
1080  // Adjust tm_t to localtime - we can not use normalize as it calls us
1081 
1082  // local seconds as timegm does not add offset
1083  dst_epoch = timegm(&t);
1084 
1085  // Add offset for localtime
1086  dst_epoch += offset;
1087 
1088  // Convert to back to tm_t structure with localtime offsets applied
1089  (void) time_to_tm(dst_epoch, 0L, &t);
1090 
1091  dayno = dayno;
1092  weekno= weekno;
1093  days= 0;
1094  while( 1 )
1095  {
1096  if( ((t.tm_wday + days) % 7) == dayno)
1097  {
1098  if( (--weekno) == 0)
1099  break;
1100  }
1101  ++days;
1102  dst_epoch += 86400L;
1103  }
1104 
1105  // Return GMT
1106  return(dst_epoch);
1107 }
1108 
1112 MEMSPACE
1113 void set_dst(time_t epoch)
1114 {
1115  if(epoch == 0)
1116  {
1117  tv_t tv;
1118  tz_t tz;
1119  // Get timezone and clock time
1120  gettimeofday(&tv, &tz);
1121  epoch = tv.tv_sec;
1122  }
1123 
1124  // Cache caclulations so we do not do them more often then once a day
1125  if(dst.epoch >= epoch)
1126  {
1127  if((dst.epoch - epoch) < 86400L)
1128  return;
1129  }
1130  else
1131  {
1132  if((epoch - dst.epoch) < 86400L)
1133  return;
1134  }
1135 
1136  dst.epoch = epoch;
1137  // FIXME think of ways to cache this computation
1138  dst.start = find_dst(0, epoch, 0, 3, 2, 0, 2);
1139  dst.end = find_dst(1, epoch, 0, 11, 1, 0, 2);
1140 }
1141 
1146 MEMSPACE
1147 int is_dst(time_t epoch)
1148 {
1149  set_dst(epoch);
1150 
1151  if( epoch >= dst.start && epoch <= dst.end)
1152  return(1);
1153  return(0);
1154 }
1155 
1157 MEMSPACE
1159 {
1160  printf("DST START localtime: %s\n", ctime(&dst.start));
1161  printf("DST END localtime: %s\n", ctime(&dst.end));
1162 }
1163 
1165 MEMSPACE
1167 {
1168  printf("DST START GMT: %s\n", ctime_gm(&dst.start));
1169  printf("DST END GMT: %s\n", ctime_gm(&dst.end));
1170 
1171 }
1172 
1177 MEMSPACE
1178 void initialize_clock(int minwest)
1179 {
1180  time_t seconds = 0;
1181  tm_t tc;
1182  ts_t ts;
1183  tz_t tz;
1184 
1185 #ifdef RTC_SUPPORT
1186  if(!rtc_init(0,0L))
1187  {
1188  printf("rtc uninitilized\n");
1189  printf("attempting rtc init\n");
1190  if( !rtc_init(1, (time_t) 0) )
1191  {
1192  printf("rtc force init failed\n");
1193  }
1194  }
1195 
1196  if(rtc_read(&tc))
1197  {
1198  seconds = timegm(&tc);
1199  }
1200  else
1201  {
1202  seconds = 0;
1203  printf("rtc read errorafter init\n");
1204  }
1205 #else
1206  printf("NO RTC\n");
1207  seconds = 0;
1208 #endif // RTC_SUPPORT
1209  if(!seconds)
1210  printf("use setdate command to change time\n");
1211  tz.tz_minuteswest = minwest;
1212  tz.tz_dsttime = 0;
1213  settimezone( &tz );
1214 
1215  ts.tv_sec = seconds;
1216  ts.tv_nsec = 0L;
1217  clock_settime(0, (ts_t *) &ts);
1218 }
1219 
1226 MEMSPACE
1228 {
1229  time_t seconds;
1230  tm_t tc;
1231  ts_t ts;
1232 
1233 #ifdef RTC_SUPPORT
1234  if(rtc_read(&tc))
1235  {
1236  seconds = timegm(&tc);
1237  printf("rtc seconds: %lu\n",seconds);
1238  printf("rtc time: %s\n",asctime(&tc));
1239 #if RTC_DEBUG
1240  printf("rtc_read:%d, day:%d,mon:%d,hour:%d,min:%d,sec:%d, wday:%d\n",
1241  (int) tc.tm_year + 1900,
1242  (int) tc.tm_mday,
1243  (int) tc.tm_mon,
1244  (int) tc.tm_hour,
1245  (int) tc.tm_min,
1246  (int) tc.tm_sec,
1247  (int) tc.tm_wday);
1248 #endif
1249  }
1250  else
1251  {
1252  printf("RTC read failed\n");
1253  }
1254 #endif // RTC_SUPPORT
1255 
1256  clock_gettime(0, (ts_t *) &ts);
1257  seconds = ts.tv_sec;
1258  printf("clk seconds: %lu\n",seconds);
1259  printf("clk time: %s\n", asctime(gmtime(&seconds)));
1260 }
1261 
int int32_t
int tz_dsttime
Definition: time.h:81
MEMSPACE void display_clock()
Display system time and optionally RTC time.
Definition: time.c:1227
tz_t __tzone
System Time Zone.
Definition: time.c:49
int tm_min
Definition: time.h:44
dst_t dst
DST start and stop in GMT epoch.
Definition: time.c:52
MEMSPACE char * asctime_r(tm_t *t, char *buf)
Convert tm_t *t structure into POSIX asctime() ASCII string *buf.
Definition: time.c:367
MEMSPACE int is_dst(time_t epoch)
Test GMT epoch time to see if DST applies in a local timezone.
Definition: time.c:1147
MEMSPACE void initialize_clock(int minwest)
initialize system time - if we have an RTC use it
Definition: time.c:1178
unsigned short uint16_t
Definition: send.c:18
MEMSPACE time_t mktime(tm_t *t)
convert tm_t structure to time_t local time epoch
Definition: time.c:534
MEMSPACE time_t normalize(tm_t *t, int normalize_to_timezone)
Normalize POSIX tm_t *t struct and convert to epoch time Note: does not deal with DST - by design...
Definition: time.c:654
Master include file for project Includes all project includes and defines here.
Common Linux/POSIX time functions.
MEMSPACE uint8_t rtc_read(tm_t *t)
Read DS1307 RTC into POSIX struct tm * structure.
Definition: rtc.c:238
MEMSPACE void set_dst(time_t epoch)
Set DST start and end time for the given epoch year.
Definition: time.c:1113
int tm_mday
Definition: time.h:46
static MEMSPACE int Days_Per_Year(int year)
Find number of days in a given year.
Definition: time.c:211
static MEMSPACE int IS_Leap(int year)
Check if a year is a leap year.
Definition: time.c:135
MEMSPACE char * fgets(char *str, int size, FILE *stream)
get a string from stdin See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:420
MEMSPACE int finddayofweek(int year, int month, int day)
return day of week for givenn day, month, year
Definition: time.c:180
MEMSPACE int setdate_r(char *buf)
Set date and time from string in this format "YYYY MM DD HH:MM:SS".
Definition: time.c:919
int tm_year
Definition: time.h:48
unsigned int uint32_t
Definition: send.c:19
FILE type structure.
Definition: posix.h:156
MEMSPACE char * tm_mon_to_ascii(int i)
Get string Short name of Month from month number.
Definition: time.c:114
MEMSPACE void print_dst_gmt()
print start/stop for DST as GMT for this year
Definition: time.c:1166
time_t tv_sec
Definition: time.h:70
time_t end
Start of local DST in GMT.
Definition: time.h:107
MEMSPACE time_t timegm(tm_t *t)
Convert tm_t structure as GMT time into GMT seconds since 1900. All calculactions are in GMT regardle...
Definition: time.c:347
time_t tv_sec
Definition: time.h:90
timer functions
#define snprintf(s, size, format, args...)
Definition: cpu.h:81
MEMSPACE time_t time(time_t *t)
Return second from epoch - POSIX function.
Definition: time.c:843
volatile ts_t __clock
System Clock Time.
Definition: timer.c:38
const char * __WDay[]
Short Name of each Day in a week.
Definition: time.c:78
MEMSPACE int Days_Per_Month(int month, int year)
days in a month
Definition: time.c:223
uint32_t time_t
type of EPOCH result.
Definition: time.h:35
MEMSPACE int clock_settime(clockid_t clk_id, const struct timespec *ts)
Set system clock using seconds and nonoseconds - POSIX function.
Definition: timer.c:345
int tm_mon
Definition: time.h:47
MEMSPACE uint16_t sum(char *name)
sum of a file with 16bit hex and integer results
Definition: posix_tests.c:626
long tv_nsec
Definition: time.h:91
#define NULL
Definition: cpu.h:55
MEMSPACE char * ctime_gm(time_t *tp)
GMT version of POSIX ctime().
Definition: time.c:441
MEMSPACE tm_t * localtime(time_t *tp)
Convert POSIX epoch time_t *tp into POSIX tm_t *result.
Definition: time.c:522
time_t start
Definition: time.h:106
MEMSPACE uint8_t rtc_init(int force, time_t seconds)
Initialize DS1307 rtc if not initialied - or if forced.
Definition: rtc.c:121
POSIX timeval.
Definition: time.h:68
time_t epoch
End of local DST in GMT.
Definition: time.h:108
MEMSPACE int gettimezone(tz_t *tz)
Get current timezone in struct timezone *tz - POSIX function.
Definition: time.c:792
MEMSPACE char * asctime(tm_t *t)
Convert tm_t *t structure into POSIX asctime() ASCII string.
Definition: time.c:394
Math IO functions, and verious conversion code with floating point support.
MEMSPACE tm_t * gmtime(time_t *tp)
Convert epoch GMT time_t *tp into POSIX static tm_t *t.
Definition: time.c:471
uint32_t tv_usec
Definition: time.h:71
MEMSPACE void print_dst()
print start/stop for DST as localtime for this year
Definition: time.c:1158
MEMSPACE long strtol(const char *nptr, char **endptr, int base)
Convert ASCII string to number in a given base.
Definition: mathio.c:136
static MEMSPACE time_t tm2epoch(tm_t *t)
Converts tm_t structure as GMT time into GMT epoch since 1900.
Definition: time.c:552
#define stdin
define stdin, stdout and stderr
Definition: posix.h:269
MEMSPACE int settimezone(tz_t *tz)
Set current timezone with struct timezone *tz - POSIX function.
Definition: time.c:806
MEMSPACE int setdate(void)
Set date and time by prompting user.
Definition: time.c:901
int sscanf(const char *strp, const char *fmt,...)
#define EPOCH_YEAR
Definition: time.h:29
MEMSPACE char * ctime_r(time_t *t, char *buf)
Convert local time_t *t epoch time into POSIX asctime() ASCII string *buf.
Definition: time.c:410
DST structure.
Definition: time.h:105
POSIX struct tm.
Definition: time.h:41
MEMSPACE tm_t * gmtime_r(time_t *t, tm_t *result)
Convert epoch GMT time_t *tp into POSIX tm_t *result.
Definition: time.c:456
int tm_wday
Definition: time.h:49
#define MEMSPACE
Definition: cpu.h:25
MEMSPACE int gettimeofday(tv_t *tv, tz_t *tz)
Get current time struct timeval *tv and struct timezone *tz - POSIX function. We assume a GMT hardwar...
Definition: time.c:822
int tm_yday
Definition: time.h:50
MEMSPACE time_t find_dst(int dst, time_t epoch, int year, int month, int weekno, int dayno, int hour)
Calculate GMT seconds of DST transition given LOCAL time start / end time and DST flag...
Definition: time.c:1038
MEMSPACE int printf(const char *format,...)
const char * __Month[]
Short Name or each Month in a year.
Definition: time.c:100
time_t sec
Definition: user_main.c:308
MEMSPACE tm_t * localtime_r(time_t *t, tm_t *result)
Convert POSIX epoch time_t *tp into POSIX tm_t *result expressed as local time using timezone and DST...
Definition: time.c:499
POSIX timespec.
Definition: time.h:88
#define L(x)
Definition: cal_dex.c:54
MEMSPACE int settimeofday(tv_t *tv, tz_t *tz)
Set current time struct timeval *tv and struct timezone *tz - POSIX function. We assume a GMT hardwar...
Definition: time.c:861
int tm_sec
Definition: time.h:43
#define EPOCH_DAY
Definition: time.h:30
MEMSPACE char * ctime(time_t *tp)
Convert local time_t *t epoch time into POSIX asctime() string buf[].
Definition: time.c:424
POSIX wrapper for FatFS.
int tm_hour
Definition: time.h:45
MEMSPACE time_t time_to_tm(time_t epoch, int32_t offset, tm_t *t)
Converts epoch ( seconds from 1 Jan EPOCH_YEAR UTC), offset seconds, to UNIX tm *t.
Definition: time.c:262
static const uint16_t __days[]
days in each month.
Definition: time.c:68
time_t seconds
Definition: user_main.c:293
static const uint16_t __days_sum[]
accumulated days to the start of a month in a year.
Definition: time.c:59
MEMSPACE void clock_set(uint32_t seconds, uint32_t us)
Set system clock with seconds and microseconds.
Definition: time.c:882
MEMSPACE char * tm_wday_to_ascii(int i)
Get string Short name of day from day number.
Definition: time.c:87
POSIX timezone.
Definition: time.h:78
int tm_isdst
Definition: time.h:51
MEMSPACE int clock_gettime(clockid_t clk_id, struct timespec *ts)
Generic clock_gettime() function WITHOUT high resolution.
Definition: timer.c:377
static MEMSPACE int Leap_Days_Since_1900(int year)
Number of leap days since 1900 to the BEGINNING of the year.
Definition: time.c:156
int tz_minuteswest
Definition: time.h:80