void timelib_error_container_dtor(timelib_error_container *errors) { int i; for (i = 0; i < errors->warning_count; i++) { timelib_free(errors->warning_messages[i].message); } timelib_free(errors->warning_messages); for (i = 0; i < errors->error_count; i++) { timelib_free(errors->error_messages[i].message); } timelib_free(errors->error_messages); timelib_free(errors); }
static timelib_sll timelib_get_nr(char **ptr, int max_length) { char *begin, *end, *str; timelib_sll tmp_nr = TIMELIB_UNSET; int len = 0; while ((**ptr < '0') || (**ptr > '9')) { if (**ptr == '\0') { return TIMELIB_UNSET; } ++*ptr; } begin = *ptr; while ((**ptr >= '0') && (**ptr <= '9') && len < max_length) { ++*ptr; ++len; } end = *ptr; str = timelib_calloc(1, end - begin + 1); memcpy(str, begin, end - begin); tmp_nr = strtoll(str, NULL, 10); timelib_free(str); return tmp_nr; }
static timelib_sll do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi) { switch (tz->zone_type) { case TIMELIB_ZONETYPE_OFFSET: tz->is_localtime = 1; return tz->z * 60; break; case TIMELIB_ZONETYPE_ABBR: { timelib_sll tmp; tz->is_localtime = 1; tmp = tz->z; tmp -= tz->dst * 60; tmp *= 60; return tmp; } break; case TIMELIB_ZONETYPE_ID: tzi = tz->tz_info; /* Break intentionally missing */ default: /* No timezone in struct, fallback to reference if possible */ if (tzi) { timelib_time_offset *before, *after; timelib_sll tmp; int in_transistion; tz->is_localtime = 1; before = timelib_get_time_zone_info(tz->sse, tzi); after = timelib_get_time_zone_info(tz->sse - before->offset, tzi); timelib_set_timezone(tz, tzi); in_transistion = ( ((tz->sse - after->offset) >= (after->transistion_time + (before->offset - after->offset))) && ((tz->sse - after->offset) < after->transistion_time) ); if ((before->offset != after->offset) && !in_transistion) { tmp = -after->offset; } else { tmp = -tz->z; } timelib_time_offset_dtor(before); timelib_time_offset_dtor(after); { timelib_time_offset *gmt_offset; gmt_offset = timelib_get_time_zone_info(tz->sse + tmp, tzi); tz->z = gmt_offset->offset; tz->dst = gmt_offset->is_dst; if (tz->tz_abbr) { timelib_free(tz->tz_abbr); } tz->tz_abbr = timelib_strdup(gmt_offset->abbr); timelib_time_offset_dtor(gmt_offset); } return tmp; } } return 0; }
void timelib_strtointerval(char *s, size_t len, timelib_time **begin, timelib_time **end, timelib_rel_time **period, int *recurrences, timelib_error_container **errors) { Scanner in; int t; char *e = s + len - 1; memset(&in, 0, sizeof(in)); in.errors = timelib_malloc(sizeof(timelib_error_container)); in.errors->warning_count = 0; in.errors->warning_messages = NULL; in.errors->error_count = 0; in.errors->error_messages = NULL; if (len > 0) { while (isspace(*s) && s < e) { s++; } while (isspace(*e) && e > s) { e--; } } if (e - s < 0) { add_error(&in, "Empty string"); if (errors) { *errors = in.errors; } else { timelib_error_container_dtor(in.errors); } return; } e++; /* init cursor */ in.str = timelib_malloc((e - s) + YYMAXFILL); memset(in.str, 0, (e - s) + YYMAXFILL); memcpy(in.str, s, (e - s)); in.lim = in.str + (e - s) + YYMAXFILL; in.cur = in.str; /* init value containers */ in.begin = timelib_time_ctor(); in.begin->y = TIMELIB_UNSET; in.begin->d = TIMELIB_UNSET; in.begin->m = TIMELIB_UNSET; in.begin->h = TIMELIB_UNSET; in.begin->i = TIMELIB_UNSET; in.begin->s = TIMELIB_UNSET; in.begin->us = 0; in.begin->z = 0; in.begin->dst = 0; in.begin->is_localtime = 0; in.begin->zone_type = TIMELIB_ZONETYPE_OFFSET; in.end = timelib_time_ctor(); in.end->y = TIMELIB_UNSET; in.end->d = TIMELIB_UNSET; in.end->m = TIMELIB_UNSET; in.end->h = TIMELIB_UNSET; in.end->i = TIMELIB_UNSET; in.end->s = TIMELIB_UNSET; in.end->us = 0; in.end->z = 0; in.end->dst = 0; in.end->is_localtime = 0; in.end->zone_type = TIMELIB_ZONETYPE_OFFSET; in.period = timelib_rel_time_ctor(); in.period->y = 0; in.period->d = 0; in.period->m = 0; in.period->h = 0; in.period->i = 0; in.period->s = 0; in.period->weekday = 0; in.period->weekday_behavior = 0; in.period->first_last_day_of = 0; in.period->days = TIMELIB_UNSET; in.recurrences = 1; do { t = scan(&in); #ifdef DEBUG_PARSER printf("%d\n", t); #endif } while(t != EOI); timelib_free(in.str); if (errors) { *errors = in.errors; } else { timelib_error_container_dtor(in.errors); } if (in.have_begin_date) { *begin = in.begin; } else { timelib_time_dtor(in.begin); } if (in.have_end_date) { *end = in.end; } else { timelib_time_dtor(in.end); } if (in.have_period) { *period = in.period; } else { timelib_rel_time_dtor(in.period); } if (in.have_recurrences) { *recurrences = in.recurrences; } }