wi_date_t * wi_date_init_with_string(wi_date_t *date, wi_string_t *string, wi_string_t *format) { wi_regexp_t *regexp; wi_string_t *substring; struct tm tm; time_t clock; wi_uinteger_t count, offset, hours, minutes; memset(&tm, 0, sizeof(tm)); if(!strptime(wi_string_utf8_string(string), wi_string_utf8_string(format), &tm)) { wi_release(date); return NULL; } offset = 0; if(wi_string_contains_string(format, WI_STR("%z"), WI_STRING_CASE_INSENSITIVE)) { regexp = wi_regexp_with_pattern(WI_STR("((\\+|\\-)[0-9]{4})"), 0); substring = wi_regexp_string_of_first_match_in_string(regexp, string); if(substring) { hours = wi_string_uinteger(wi_string_substring_with_range(substring, wi_make_range(1, 2))); minutes = wi_string_uinteger(wi_string_substring_with_range(substring, wi_make_range(3, 2))); offset = (hours * 3600) + (minutes * 60); if(wi_string_has_prefix(substring, WI_STR("-"))) offset = -offset; } } clock = wi_timegm(&tm) - offset; return wi_date_init_with_time(date, clock); }
wi_url_t * wi_url_init_with_string(wi_url_t *url, wi_string_t *string) { wi_string_t *userpassword; wi_range_t range; range = wi_string_range_of_string(string, WI_STR("://"), 0); if(range.location != WI_NOT_FOUND) { url->scheme = wi_retain(wi_string_substring_to_index(string, range.location)); if(range.location + range.length >= wi_string_length(string)) goto end; else string = wi_string_substring_from_index(string, range.location + 3); } range = wi_string_range_of_string(string, WI_STR("/"), 0); if(range.location != WI_NOT_FOUND) { url->path = wi_retain(wi_string_substring_from_index(string, range.location)); string = wi_string_substring_to_index(string, range.location); } range = wi_string_range_of_string(string, WI_STR("@"), 0); if(range.location != WI_NOT_FOUND) { userpassword = wi_string_substring_to_index(string, range.location); string = wi_string_substring_from_index(string, range.location + 1); range = wi_string_range_of_string(userpassword, WI_STR(":"), 0); if(range.location != WI_NOT_FOUND && range.location != wi_string_length(userpassword) - 1) { url->user = wi_retain(wi_string_substring_to_index(userpassword, range.location)); url->password = wi_retain(wi_string_substring_from_index(userpassword, range.location + 1)); } else { url->user = wi_retain(userpassword); } } range = wi_string_range_of_string(string, WI_STR(":"), 0); if(range.location == WI_NOT_FOUND || range.location + range.length >= wi_string_length(string) || wi_string_contains_string(wi_string_substring_from_index(string, range.location + 1), WI_STR(":"), 0)) { url->host = wi_copy(string); } else { url->host = wi_retain(wi_string_substring_to_index(string, range.location)); url->port = wi_string_uint32(wi_string_substring_from_index(string, range.location + 1)); } end: _wi_url_regenerate_string(url); return url; }