BayEOSArduino Library
localtime_DE.h
1 /*
2  * Header file for transformation of UTC-Time to localtime with daylight savings, holidays...
3  *
4  *
5  */
6 
7 #ifndef LOCALTIME_H
8 #define LOCALTIME_H
9 #include <BayEOSBuffer.h> /* for Datetime */
10 
11 struct HOLIDAY {
12  uint8_t day;
13  uint8_t month;
14 };
15 
16 //Returns true for all free days incl. Saturday and Sunday
17 bool isHoliday(DateTime& date){
18  //Saturday or Sunday
19  if(date.dayOfWeek()==0 || date.dayOfWeek()==6) return true;
20 
21  //Fixed day holidays
22  HOLIDAY holidays[]={{1,1},{6,1},{1,5},{3,10},{1,11},{25,12},{26,12}};
23  for(uint8_t i=0;i<sizeof(holidays)/2;i++){
24  if(holidays[i].day==date.day() && holidays[i].month==date.month()) return true;
25  }
26 
27  //Variable date holidays (easter)
28  //day shifts to easter sunday for Karfreitag, Ostermontag, Himmelfahrt, Pfingstmontag, Fronleichnam
29  int8_t easter_shifts[]={-2,1,39,50,60};
30  //calculate easter sunday
31  int a = date.year() % 19;
32  int b = date.year() /100;
33  int c = date.year() %100;
34  int d = b / 4;
35  int e = b % 4;
36  int g = (8 * b + 13) / 25;
37  int h = (19 * a + b - d - g + 15) % 30;
38  int j = c / 4;
39  int k = c % 4;
40  int m = (a + 11 * h) / 319;
41  int r = (2 * e + 2 * j - k - h + m + 32) % 7;
42  int n = (h - m + r + 90) / 25; //month
43  int p = (h - m + r + n + 19) % 32; //day
44  DateTime easter_sunday(date.year(),n,p);
45  DateTime easter_holiday;
46  for(uint8_t i=0;i<sizeof(easter_shifts);i++){
47  easter_holiday=DateTime(easter_sunday.get()+86400L*easter_shifts[i]);
48  if(date.day()==easter_holiday.day() && date.month()==easter_holiday.month()) return true;
49  }
50  return false;
51 }
52 
53 
54 //Returns true when there is daylight saving for the date
55 bool isDaylightSaving(DateTime& d){
56  DateTime temp;
57  if(d.month()<3) return false;
58  if(d.month()>10) return false;
59  if(d.month()>3 && d.month()<10) return true;
60  if(d.month()==3){
61  temp=DateTime(d.year(),3,31);
62  while(temp.dayOfWeek()){ //not Sunday
63  temp=DateTime(temp.get()-86400L);
64  }
65  if(d.day()<temp.day()) return false;
66  return true;
67  }
68  if(d.month()==10){
69  temp=DateTime(d.year(),10,31);
70  while(temp.dayOfWeek()){ //not Sunday
71  temp=DateTime(temp.get()-86400L);
72  }
73  if(d.day()<temp.day()) return true;
74  return false;
75  }
76 }
77 
78 //get the time shift for the date
79 long getShift(DateTime& d){
80  if(isDaylightSaving(d)) return 7200; //Two hours ahead from UTC
81  else return 3600; //One hour ahead from UTC
82 }
83 
84 #endif
Definition: BayEOSBuffer.h:33
Definition: localtime_DE.h:11