#include "location.h" #include #define LOCATION_FILE "HCTRACKR" static const struct location_data default_loc = { .latitude = 29.7601338, .longitude = -95.3694728, .time_zone = -6, .use_dst = true, .dst_start_month = 2, /* March */ .dst_start_day = { .is_exact_day = false, .weekday = 0, /* Sunday */ .weeknum = 2, }, .dst_end_month = 10, /* November */ .dst_end_day = { .is_exact_day = false, .weekday = 0, /* Sunday */ .weeknum = 1, }, }; const struct location_data *get_location_data(void) { ti_var_t invar = ti_Open(LOCATION_FILE, "w"); if(!invar) goto error; if(ti_GetSize(invar) == sizeof(struct location_data)) { void *ptr = ti_GetDataPtr(invar); ti_Close(invar); return ptr; } error: ti_Close(invar); ti_var_t outvar = ti_Open(LOCATION_FILE, "w"); if(outvar) { ti_Write(&default_loc, sizeof default_loc, 1, outvar); ti_SetArchiveStatus(true, outvar); ti_Close(outvar); } return &default_loc; } bool is_on_dst(const struct location_data *loc, const struct tm *tm) { if(!loc->use_dst) return false; if(tm->tm_mon < loc->dst_start_month) return false; if(tm->tm_mon > loc->dst_end_month) return false; if(tm->tm_mon == loc->dst_start_month) { uint8_t start_day = get_exact_day(&loc->dst_start_day, tm->tm_mon, tm->tm_year); return tm->tm_mday >= start_day; } if(tm->tm_mon == loc->dst_end_month) { uint8_t end_day = get_exact_day(&loc->dst_end_day, tm->tm_mon, tm->tm_year); return tm->tm_mday < end_day; } return true; }