-
Notifications
You must be signed in to change notification settings - Fork 1
/
tz.c
401 lines (368 loc) · 10.3 KB
/
tz.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <time.h>
#include "tz.h"
#include "hash.h"
#include "main.h"
#include <util/delay.h>
extern uint_farptr_t _binary_tz_names_start;
extern uint_farptr_t _binary_tz_names_end;
extern uint_farptr_t _binary_tz_names_size;
extern uint_farptr_t _binary_tz_offset_start;
extern uint_farptr_t _binary_tz_offset_end;
extern uint_farptr_t _binary_tz_offset_size;
extern uint_farptr_t _binary_tz_zones_start;
extern uint_farptr_t _binary_tz_zones_end;
extern uint_farptr_t _binary_tz_zones_size;
extern uint_farptr_t _binary_tz_rules_start;
extern uint_farptr_t _binary_tz_rules_end;
extern uint_farptr_t _binary_tz_rules_size;
extern uint_farptr_t _binary_tz_hash_start;
extern uint_farptr_t _binary_tz_hash_end;
extern uint_farptr_t _binary_tz_hash_size;
extern long __utc_offset;
uint16_t EEMEM TZ_EEPROM = 0;
uint16_t TZ;
uint16_t LAST_DST;
time_t LAST_DST_UPDATE;
char wday[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
uint8_t week_day(int16_t year, uint8_t month, uint8_t d){
//TODO Optimize for our dates, check if this works with an 8bit type to 2100
uint8_t y = year;
uint8_t m = month+1;
return (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7;
}
void wdays_in_month(uint8_t (*days)[5], int16_t year, uint8_t month, uint8_t day){
uint8_t last_day = month_length(year, month);
uint8_t i;
uint8_t c = 0;
for(i = 1;i<=last_day; i++){
if(week_day(year, month, i) == day){
(*days)[c] = i;
c++;
}
}
}
uint8_t day_to_int(char day[3]){
uint8_t i;
for(i=0;i<=6;i++){
if(!strcmp(day, wday[i])){
break;
}
}
return i;
}
uint8_t last(uint8_t (*days)[5]){
uint8_t highest = 0;
uint8_t i;
for(i=0;i<=4;i++){
if((*days)[i] > highest){
highest = (*days)[i];
// printf("%i\n", (*days)[i]);
}
}
return highest;
}
uint8_t decode_day(tz_rule *rule, uint8_t year){
uint8_t rvalue = 0;
uint8_t days[5] = {0, 0, 0, 0};
if(strlen(rule->day) <= 2){
rvalue = atoi(rule->day);
}
if( ((uint8_t)rule->day[0] >= 65) && ((uint8_t)rule->day[0] <= 90) ){
char day[4];
strncpy(day, rule->day, 3);
day[3] = '\0';
char op[3];
strncpy(op, rule->day+3, 2);
op[2] = '\0';
uint8_t day_num = atoi(rule->day+5);
if(!strcmp(op, ">=")){
wdays_in_month(&days, year, rule->month, day_to_int(day));
int i;
for(i=0;i<=4;i++){
if(days[i] >= day_num){
rvalue = days[i];
break;
}
}
}
if(!strcmp(op, "<=")){
wdays_in_month(&days, year, rule->month, day_to_int(day));
uint8_t i;
for(i=4;i<=0;i--){
if(days[i] <= day_num){
rvalue = days[i];
break;
}
}
}
}
if(!strncmp(rule->day, "last", 4)){
char day[4];
strcpy(day, rule->day+4);
wdays_in_month(&days, year, rule->month, day_to_int(day));
// int i = 0;
// for(i=0;i<=4;i++){
// printf("days: %i\n", days[i]);
// }
rvalue = last(&days);
}
return rvalue;
}
void read_zone(uint16_t location, tz_zone *zone){
char temp[18];
char *ptr = &temp[0];
uint_farptr_t addr = &_binary_tz_zones_start;
addr = addr+(location*18);
memcpy_PF(&temp, addr, 18);
zone->offset = *(int8_t *)ptr;
ptr++;
zone->rule = *(uint16_t *)ptr;
ptr = ptr+2;
strncpy(zone->format, ptr, 9);
ptr = ptr+10;
zone->until = *(uint32_t *)ptr;
ptr = ptr+4;
zone->flags = *ptr;
}
void read_rule(uint16_t location, tz_rule *rule){
char temp[15];
char *ptr = &temp[0];
uint_farptr_t addr = &_binary_tz_rules_start;
addr = addr+(location*15);
memcpy_PF(&temp, addr, 15);
rule->from = *(uint8_t *)ptr;
ptr++;
rule->to = *(uint8_t *)ptr;
ptr++;
rule->month = *(uint8_t *)ptr;
ptr++;
strncpy(rule->day, ptr, 9);
ptr = ptr+8;
rule->at = *(uint8_t *)ptr; //15 minute resolution
ptr++;
rule->save = *(int8_t *)ptr; //15 minute resolution
ptr++;
rule->letter = *(char *)ptr;
ptr++;
rule->flags = *ptr;
rule->flags = rule->flags|0x01;
}
void print_rule(tz_rule *rule){
printf("From: %u ", rule->from);
printf("To: %u ", rule->to);
printf("Month: %u ", rule->month);
printf("Day: %s ", rule->day);
printf("At: %u ", rule->at);
printf("Save: %i ", rule->save);
printf("Letter: %c ", rule->letter);
printf("Flags: %u\n", rule->flags);
decode_day(rule, 15);
}
void print_zone(tz_zone *zone){
printf("Offset: %i ", zone->offset);
printf("Rule: %hu ", zone->rule);
printf("%s ", zone->format);
printf("Until: %4lu ", zone->until);
printf("Flags: %i\n", zone->flags);
}
const static uint8_t months[2][12] = {
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
#define secondsinyear 31556926
time_t time_subtract(time_t a, time_t b){
time_t temp = a-b;
if(temp > a){
return 0;
}
return temp;
}
int get_dst(const time_t * timer, int32_t * z){
uint16_t location = TZ;
time_t t;
t = *timer;
struct tm tm_struct;
time_t seconds;
time(&seconds);
gmtime_r(&seconds, &tm_struct);
uint8_t year = tm_struct.tm_year-100;
#ifdef DEBUG
printf("Year: %u\n", year);
#endif
tz_zone zone;
tz_rule rule;
tz_rule last_rule = (const tz_rule){ 0 };
struct tm rule_time;
time_t temp;
time_t last_rule_time = 0;
uint8_t flag = 1;
while(flag){
read_zone(location, &zone);
// print_zone(&zone);
if(zone.until >= t){
flag = 0;
}
if((zone.flags&0x80)>>7){
flag = 0;
}
location++;
}
location = zone.rule;
t = t+(*z+LAST_DST); //Make UTC into localtime
while(1){
rule_time = (const struct tm){ 0 };
read_rule(location, &rule);
if(year >= rule.from){
#ifdef DEBUG1
printf("Last Time %lu\n", last_rule_time);
print_rule(&rule);
#endif
if(rule.to <= year){
rule_time.tm_year = rule.to+100;
}
else{
rule_time.tm_year = year+100;
}
rule_time.tm_mon = rule.month;
rule_time.tm_mday = decode_day(&rule, year);
#ifdef DEBUG1
printf("%s\n", rule.day);
printf("day %i\n", rule_time.tm_mday);
#endif
rule_time.tm_hour = (rule.at*15)/60;
rule_time.tm_min = (rule.at*15)%60;
temp = mk_gmtime(&rule_time);
#ifdef DEBUG1
printf("%lu\n", t);
printf("%lu\n", temp);
printf("%lu\n", time_subtract(temp,secondsinyear));
#endif
if(last_rule_time == 0){
if(rule.from < year){
last_rule_time = time_subtract(temp,secondsinyear);
}
else{
last_rule_time = temp;
}
}
#ifdef DEBUG1
printf("%lu\n", last_rule_time);
#endif
if(t >= time_subtract(temp,secondsinyear) && time_subtract(temp,secondsinyear) >= last_rule_time && rule.from < year){
// print_rule(&rule);
last_rule_time = time_subtract(temp,secondsinyear);
last_rule = rule;
}
if(t >= temp && temp >= last_rule_time){// && temp > last_rule_time+secondsinyear){
last_rule_time = temp;
last_rule = rule;
}
if(last_rule_time > t){
read_rule(0, &last_rule);
}
// print_rule(&last_rule);
}
if((rule.flags&0x80)>>7){
break;
}
location++;
}
#ifdef DEBUG
printf("Selected Rule ");
print_rule(&last_rule);
#endif
int dst = (last_rule.save*15)*60;
if(dst != LAST_DST && LAST_DST_UPDATE+ONE_HOUR < seconds){
LAST_DST = dst;
LAST_DST_UPDATE = seconds;
}
return dst;
}
int32_t get_offset(time_t t){
uint16_t location = TZ;
tz_zone zone;
uint8_t flag = 1;
while(flag){
read_zone(TZ, &zone);
if(zone.until >= t){
flag = 0;
}
if((zone.flags&0x80)>>7){
flag = 0;
}
location++;
}
return (zone.offset*15)*60;
}
void get_name(char string[30], int count){
uint_farptr_t ptr = &_binary_tz_names_start;
uint16_t i;
for(i=0;i<=count;i++){
if(!i==0){
ptr = ptr+(strlen_PF(ptr)+1);
}
}
strncpy_PF(string, ptr, 29);
}
uint16_t zone_by_hash(char *zone_name){
uint_farptr_t ptr = &_binary_tz_offset_start;
uint_farptr_t hashptr = &_binary_tz_hash_start;
uint16_t count = (((uint16_t)&_binary_tz_offset_size)/2)-1;
uint32_t name_hash = hash(zone_name);
uint32_t test_hash;
uint16_t i;
for(i=0;i<=count;i++){
test_hash = pgm_read_dword(hashptr+(i*4));
if(name_hash == test_hash){
return pgm_read_word(ptr+(2*i));
}
}
return 0;
}
uint16_t zone_by_name(char *zone_name){
uint_farptr_t ptr = &_binary_tz_offset_start;
uint16_t count = (((uint16_t)&_binary_tz_offset_size)/2)-1;
uint16_t i;
char name[30];
for(i=0;i<=count;i++){
get_name(name, i);
if(!strcmp(zone_name, name)){
return pgm_read_word(ptr+(2*i));
}
}
return 0;
}
void list_zones(void){
uint16_t count = (((uint16_t)&_binary_tz_offset_size)/2)-1;
uint_farptr_t ptr = &_binary_tz_names_start;
uint16_t i;
char name[30];
for(i=0;i<=count;i++){
if(!i==0){
ptr = ptr+(strlen_PF(ptr)+1);
}
strncpy_PF(name, ptr, 29);
printf("%s\n", name);
}
}
uint16_t tz_update(char *zone_name){
uint16_t zone = zone_by_hash(zone_name);
eeprom_update_word(&TZ_EEPROM, zone);
tz_init();
return zone;
}
void tz_init(void){
TZ = eeprom_read_word(&TZ_EEPROM);
LAST_DST_UPDATE = 0;
time_t seconds;
time(&seconds);
set_zone(get_offset(seconds));
set_dst(get_dst);
get_dst(&seconds, &__utc_offset);
}