aboutsummaryrefslogtreecommitdiffstats
path: root/src/asn1/asn1_tm.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2011-10-06 15:54:03 +0000
committerlloyd <[email protected]>2011-10-06 15:54:03 +0000
commit7939e91a733c8c50706de2fb090fcb60f7f229c8 (patch)
treebd9f24eefbae7a49e4832ac728ea83c80da99dc4 /src/asn1/asn1_tm.cpp
parentca5581260445e70ed4d038091acb88949b6101ce (diff)
parent2877bbc0828a92de94d3628455e92c4298a2ac7d (diff)
propagate from branch 'net.randombit.botan' (head 29dfb73a5efec220ebafcb9c1d7a32bb9d63461c)
to branch 'net.randombit.botan.cxx11' (head a842d86b2b9593318fbce5868c3d1278f8b3a037)
Diffstat (limited to 'src/asn1/asn1_tm.cpp')
-rw-r--r--src/asn1/asn1_tm.cpp52
1 files changed, 28 insertions, 24 deletions
diff --git a/src/asn1/asn1_tm.cpp b/src/asn1/asn1_tm.cpp
index a059e0528..6e6868972 100644
--- a/src/asn1/asn1_tm.cpp
+++ b/src/asn1/asn1_tm.cpp
@@ -23,11 +23,11 @@ X509_Time::X509_Time(const std::string& time_str)
}
/*
-* Create an X509_Time
+* Create a X509_Time from a time point
*/
-X509_Time::X509_Time(u64bit timer)
+X509_Time::X509_Time(const std::chrono::system_clock::time_point& time)
{
- calendar_point cal = calendar_value(timer);
+ calendar_point cal = calendar_value(time);
year = cal.year;
month = cal.month;
@@ -98,7 +98,7 @@ void X509_Time::set_to(const std::string& time_str)
void X509_Time::set_to(const std::string& t_spec, ASN1_Tag spec_tag)
{
if(spec_tag != GENERALIZED_TIME && spec_tag != UTC_TIME)
- throw Invalid_Argument("X509_Time: Invalid tag " + to_string(spec_tag));
+ throw Invalid_Argument("X509_Time: Invalid tag " + std::to_string(spec_tag));
if(spec_tag == GENERALIZED_TIME && t_spec.size() != 13 && t_spec.size() != 15)
throw Invalid_Argument("Invalid GeneralizedTime: " + t_spec);
@@ -182,24 +182,30 @@ std::string X509_Time::as_string() const
if(time_is_set() == false)
throw Invalid_State("X509_Time::as_string: No time set");
- std::string asn1rep;
- if(tag == GENERALIZED_TIME)
- asn1rep = to_string(year, 4);
- else if(tag == UTC_TIME)
+ u32bit full_year = year;
+
+ if(tag == UTC_TIME)
{
if(year < 1950 || year >= 2050)
throw Encoding_Error("X509_Time: The time " + readable_string() +
" cannot be encoded as a UTCTime");
- u32bit asn1year = (year >= 2000) ? (year - 2000) : (year - 1900);
- asn1rep = to_string(asn1year, 2);
+
+ full_year = (year >= 2000) ? (year - 2000) : (year - 1900);
}
- else
- throw Invalid_Argument("X509_Time: Invalid tag " + to_string(tag));
- asn1rep += to_string(month, 2) + to_string(day, 2);
- asn1rep += to_string(hour, 2) + to_string(minute, 2) + to_string(second, 2);
- asn1rep += "Z";
- return asn1rep;
+ std::string repr = std::to_string(full_year*10000000000 +
+ month*100000000 +
+ day*1000000 +
+ hour*10000 +
+ minute*100 +
+ second) + "Z";
+
+ u32bit desired_size = (tag == UTC_TIME) ? 13 : 15;
+
+ while(repr.size() < desired_size)
+ repr = "0" + repr;
+
+ return repr;
}
/*
@@ -218,14 +224,12 @@ std::string X509_Time::readable_string() const
if(time_is_set() == false)
throw Invalid_State("X509_Time::readable_string: No time set");
- std::string readable;
- readable += to_string(year, 4) + "/";
- readable += to_string(month ) + "/";
- readable += to_string(day ) + " ";
- readable += to_string(hour ) + ":";
- readable += to_string(minute, 2) + ":";
- readable += to_string(second, 2) + " UTC";
- return readable;
+ std::string output(24, 0);
+
+ std::sprintf(&output[0], "%04d/%02d/%02d %02d:%02d:%02d UTC",
+ year, month, day, hour, minute, second);
+
+ return output;
}
/*