std::string X509_Time::to_string() const { if(time_is_set() == false) throw Invalid_State("X509_Time::as_string: No time set"); uint32_t full_year = m_year; if(m_tag == UTC_TIME) { if(m_year < 1950 || m_year >= 2050) throw Encoding_Error("X509_Time: The time " + readable_string() + " cannot be encoded as a UTCTime"); full_year = (m_year >= 2000) ? (m_year - 2000) : (m_year - 1900); } const uint64_t YEAR_FACTOR = 10000000000ULL; const uint64_t MON_FACTOR = 100000000; const uint64_t DAY_FACTOR = 1000000; const uint64_t HOUR_FACTOR = 10000; const uint64_t MIN_FACTOR = 100; const uint64_t int_repr = YEAR_FACTOR * full_year + MON_FACTOR * m_month + DAY_FACTOR * m_day + HOUR_FACTOR * m_hour + MIN_FACTOR * m_minute + m_second; std::string repr = std::to_string(int_repr) + "Z"; uint32_t desired_size = (m_tag == UTC_TIME) ? 13 : 15; while(repr.size() < desired_size) repr = "0" + repr; return repr; }
#if defined(BOTAN_HAS_CVC) #include <botan/eac_asn_obj.h> TEST_CASE("human readable time", "[EAC_Time]") { auto time1 = Botan::EAC_Time("2008-02-01"); auto time2 = Botan::EAC_Time("2008/02/28"); auto time3 = Botan::EAC_Time("2004-06-14"); CHECK(( time1.time_is_set() == true )); CHECK(( time2.time_is_set() == true )); CHECK(( time3.time_is_set() == true )); CHECK(( time1.readable_string() == "2008/02/01" )); CHECK(( time2.readable_string() == "2008/02/28" )); CHECK(( time3.readable_string() == "2004/06/14" )); } TEST_CASE("no time", "[EAC_Time]") { auto time = Botan::EAC_Time(""); CHECK(( time.time_is_set() == false )); } TEST_CASE("invalis time", "[EAC_Time]") { CHECK_THROWS( Botan::EAC_Time(" ") ); CHECK_THROWS( Botan::EAC_Time("2008`02-01") ); CHECK_THROWS( Botan::EAC_Time("9999-02-01") );