/* * Set the time with a human readable string */ void EAC_Time::set_to(const std::string& time_str) { if(time_str == "") { year = month = day = 0; return; } std::vector<std::string> params; std::string current; for(u32bit j = 0; j != time_str.size(); ++j) { if(Charset::is_digit(time_str[j])) current += time_str[j]; else { if(current != "") params.push_back(current); current.clear(); } } if(current != "") params.push_back(current); if(params.size() != 3) throw Invalid_Argument("Invalid time specification " + time_str); year = to_u32bit(params[0]); month = to_u32bit(params[1]); day = to_u32bit(params[2]); if(!passes_sanity_check()) throw Invalid_Argument("Invalid time specification " + time_str); }
void X509_Time::set_to(const std::string& t_spec, ASN1_Tag spec_tag) { if(spec_tag == UTC_OR_GENERALIZED_TIME) { try { set_to(t_spec, GENERALIZED_TIME); return; } catch(Invalid_Argument&) {} // Not a generalized time. Continue try { set_to(t_spec, UTC_TIME); return; } catch(Invalid_Argument&) {} // Not a UTC time. Continue throw Invalid_Argument("Time string could not be parsed as GeneralizedTime or UTCTime."); } BOTAN_ASSERT(spec_tag == UTC_TIME || spec_tag == GENERALIZED_TIME, "Invalid tag."); if(t_spec.empty()) throw Invalid_Argument("Time string must not be empty."); if(t_spec.back() != 'Z') throw Unsupported_Argument("Botan does not support times with timezones other than Z: " + t_spec); if(spec_tag == GENERALIZED_TIME) { if(t_spec.size() != 15) throw Invalid_Argument("Invalid GeneralizedTime string: '" + t_spec + "'"); } else if(spec_tag == UTC_TIME) { if(t_spec.size() != 13) throw Invalid_Argument("Invalid UTCTime string: '" + t_spec + "'"); } const size_t YEAR_SIZE = (spec_tag == UTC_TIME) ? 2 : 4; std::vector<std::string> params; std::string current; for(size_t j = 0; j != YEAR_SIZE; ++j) current += t_spec[j]; params.push_back(current); current.clear(); for(size_t j = YEAR_SIZE; j != t_spec.size() - 1; ++j) { current += t_spec[j]; if(current.size() == 2) { params.push_back(current); current.clear(); } } m_year = to_u32bit(params[0]); m_month = to_u32bit(params[1]); m_day = to_u32bit(params[2]); m_hour = to_u32bit(params[3]); m_minute = to_u32bit(params[4]); m_second = (params.size() == 6) ? to_u32bit(params[5]) : 0; m_tag = spec_tag; if(spec_tag == UTC_TIME) { if(m_year >= 50) m_year += 1900; else m_year += 2000; } if(!passes_sanity_check()) throw Invalid_Argument("Time did not pass sanity check: " + t_spec); }