00001
00009 #include "PKIFException.h"
00010 #include "PKIFCommonErrors.h"
00011 #include "ASN1Errors.h"
00012 #include "PKIFTime.h"
00013 #include "Duration.h"
00014 #include "ASN1Helper.h"
00015 #include "PKIX1Implicit88.h"
00016 #include "PKIX1Explicit88.h"
00017
00018 #include "boost/numeric/conversion/cast.hpp"
00019
00020 using boost::numeric_cast;
00021 using boost::bad_numeric_cast;
00022
00023 #include "boost/date_time/posix_time/posix_time.hpp"
00024
00025 _CACDayOfMonthLimitType _CACDayOfMonthLimit[] = {
00026 { 0, false },
00027 { 31, false },
00028 { 28, true },
00029 { 31, false },
00030 { 30, false },
00031 { 31, false },
00032 { 30, false },
00033 { 31, false },
00034 { 31, false },
00035 { 30, false },
00036 { 31, false },
00037 { 30, false },
00038 { 31, false }
00039 };
00040
00042 struct CPKIFTimeImpl {
00043 boost::posix_time::ptime m_time;
00044 };
00045
00047
00055 CPKIFTimePtr CPKIFTime::CurrentTime()
00056 {
00057 CPKIFTime* time = new CPKIFTime;
00058
00059 time->m_impl->m_time = boost::posix_time::second_clock::universal_time();
00060
00061 return CPKIFTimePtr(time);
00062 }
00070 int getInt(
00072 CPKIFStringPtr& str,
00074 int size)
00075 {
00076 int theInt = atoi(str->substr(0,size).c_str());
00077 CPKIFStringPtr tmp(new std::string(str->substr(size)));
00078 str = tmp;
00079 return theInt;
00080 }
00090 void fromUtcTime(
00092 const char* time_,
00094 CPKIFTime& time)
00095 {
00096 if(12 > strlen(time_))
00097 throw CPKIFException(TOOLKIT_ASN, ASN1_INVALID_TIME);
00098
00099 CPKIFStringPtr temp(new std::string(time_));
00100 int year = getInt(temp, 2);
00101 if (year < 50)
00102 {
00103 year += 2000;
00104 }
00105 else
00106 {
00107 year += 1900;
00108 }
00109 int month = getInt(temp, 2);
00110 int day = getInt(temp, 2);
00111 int hour = getInt(temp, 2);
00112 int minute = getInt(temp, 2);
00113 int second = getInt(temp, 2);
00114 time.set(year, month, day, hour, minute, second);
00115 }
00125 void fromGenTime(
00127 const char* time_,
00129 CPKIFTime& time)
00130 {
00131 if(14 > strlen(time_))
00132 throw CPKIFException(TOOLKIT_ASN, ASN1_INVALID_TIME);
00133
00134 CPKIFStringPtr temp(new std::string(time_));
00135 int year = getInt(temp, 4);
00136 int month = getInt(temp, 2);
00137 int day = getInt(temp, 2);
00138 int hour = getInt(temp, 2);
00139 int minute = getInt(temp, 2);
00140 int second = getInt(temp, 2);
00141 time.set(year, month, day, hour, minute, second);
00142 }
00143
00144
00145
00146
00155 CPKIFTime::CPKIFTime()
00156 :m_impl(new CPKIFTimeImpl)
00157 {
00158 }
00159
00160
00161
00162
00171 CPKIFTime::CPKIFTime(const CPKIFTime& date_)
00172 :m_impl(new CPKIFTimeImpl)
00173 {
00174 m_impl->m_time = date_.m_impl->m_time;
00175 }
00183 CPKIFTime::CPKIFTime(
00185 const char *time,
00188 TimeType type)
00189 :m_impl(new CPKIFTimeImpl)
00190 {
00191
00192 if (type == UTCTIME)
00193 {
00194
00195 fromUtcTime(time, *this);
00196 }
00197 else
00198 {
00199 fromGenTime(time, *this);
00200
00201 }
00202 }
00214 CPKIFTime::CPKIFTime(const char* time)
00215 :m_impl(new CPKIFTimeImpl)
00216 {
00217
00218 if(time == NULL)
00219 {
00220 CPKIFTimePtr temp = CurrentTime();
00221 fromGenTime(temp->GetTime(), *this);
00222 }
00223 else
00224 fromGenTime(time, *this);
00225 }
00226
00227
00228
00229
00237 CPKIFTime::~CPKIFTime()
00238 {
00239 if(m_impl) {
00240 delete m_impl;
00241 m_impl = 0;
00242 }
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00259 int CPKIFTime::year() const
00260 {
00261 return m_impl->m_time.date().year();
00262 }
00263
00264
00265
00266
00275 int CPKIFTime::month() const
00276 {
00277 return m_impl->m_time.date().month();
00278 }
00279
00280
00281
00282
00290 int CPKIFTime::dayOfMonth() const
00291 {
00292 return m_impl->m_time.date().day();
00293 }
00294
00302 int CPKIFTime::hours() const
00303 {
00304 return m_impl->m_time.time_of_day().hours();
00305 }
00306
00307
00308
00309
00318 int CPKIFTime::minutes() const
00319 {
00320 return m_impl->m_time.time_of_day().minutes();
00321 }
00322
00323
00324
00325
00333 int CPKIFTime::seconds() const
00334 {
00335 return m_impl->m_time.time_of_day().seconds();
00336 }
00337
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449 #include <iostream>
00450 #include <sstream>
00451 #include <bitset>
00452 #include <iomanip>
00462 const char* CPKIFTime::GetTime() const
00463 {
00464 if(m_timeStr == (std::string*)NULL)
00465 {
00466 std::ostringstream os;
00467 os << m_impl->m_time.date().year() <<
00468 std::setw(2) << std::setfill('0') << m_impl->m_time.date().month().as_number() <<
00469 std::setw(2) << std::setfill('0') << m_impl->m_time.date().day() <<
00470 std::setw(2) << std::setfill('0') << m_impl->m_time.time_of_day().hours() <<
00471 std::setw(2) << std::setfill('0') << m_impl->m_time.time_of_day().minutes() <<
00472 std::setw(2) << std::setfill('0') <<
00473 std::setw(2) << std::setfill('0') <<
00474 m_impl->m_time.time_of_day().seconds() << "Z" << std::ends;
00475
00476 CPKIFStringPtr tmpStr(new std::string(os.str()));
00477
00478 CPKIFTime* nonConstCACTime = const_cast<CPKIFTime*>(this);
00479 nonConstCACTime->m_timeStr = tmpStr;
00480 }
00481
00482 return m_timeStr->c_str();
00483 }
00484
00485
00486
00487
00497 void CPKIFTime::set(
00499 int year_,
00501 int month_,
00503 int dom_,
00505 int hours_,
00507 int minutes_,
00509 int seconds_ )
00510 {
00511 try {
00512 CPKIFStringPtr emptyStr;
00513 m_timeStr = emptyStr;
00514
00515 boost::gregorian::date date(year_,month_,dom_);
00516 boost::posix_time::time_duration td(hours_,minutes_,seconds_);
00517 m_impl->m_time = boost::posix_time::ptime(date,td);
00518 } catch( std::exception& e) {
00519 throw CPKIFException(TOOLKIT_ASN, ASN1_INVALID_TIME, e.what());
00520 }
00521
00522 }
00523
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00637 CPKIFTime& CPKIFTime::operator =(
00639 const CPKIFTime& time_)
00640 {
00641 CPKIFStringPtr emptyStr;
00642 m_timeStr = emptyStr;
00643
00644
00645 if( m_impl != time_.m_impl )
00646 {
00647 m_impl->m_time = time_.m_impl->m_time;
00648 }
00649
00650 return *this;
00651 }
00652
00653
00654
00655
00663 CPKIFTime& CPKIFTime::operator+=(
00665 const CPKIFDuration& duration_)
00666 {
00667 CPKIFStringPtr emptyStr;
00668 m_timeStr = emptyStr;
00669
00670 long lSecs = 0;
00671
00672 try
00673 {
00674 lSecs = numeric_cast<long>(duration_.asSeconds());
00675 }
00676 catch(bad_numeric_cast &)
00677 {
00678 throw CPKIFException(TOOLKIT_ASN, COMMON_INVALID_INPUT, "Double not fit for a long.");
00679 }
00680
00681 m_impl->m_time += boost::posix_time::time_duration(boost::posix_time::seconds(lSecs));
00682
00683 return *this;
00684 }
00685
00686
00687
00688
00696 CPKIFTime& CPKIFTime::operator-=(
00698 const CPKIFDuration& duration_)
00699 {
00700 CPKIFStringPtr emptyStr;
00701 m_timeStr = emptyStr;
00702
00703 long lSecs = 0;
00704
00705 try
00706 {
00707 lSecs = numeric_cast<long>(duration_.asSeconds());
00708 }
00709 catch(bad_numeric_cast &)
00710 {
00711 throw CPKIFException(TOOLKIT_ASN, COMMON_INVALID_INPUT, "Double not fit for a long.");
00712 }
00713
00714 m_impl->m_time -= boost::posix_time::time_duration(boost::posix_time::seconds(lSecs));
00715
00716 return *this;
00717 }
00718
00719
00720
00721
00730 CPKIFTime CPKIFTime::operator+(
00732 const CPKIFDuration& duration_) const
00733 {
00734 CPKIFTime time(*this);
00735 long lSecs = 0;
00736
00737 try
00738 {
00739 lSecs = numeric_cast<long>(duration_.asSeconds());
00740 }
00741 catch(bad_numeric_cast &)
00742 {
00743 throw CPKIFException(TOOLKIT_ASN, COMMON_INVALID_INPUT, "Double not fit for a long.");
00744 }
00745
00746 time.m_impl->m_time += boost::posix_time::time_duration(boost::posix_time::seconds(lSecs));
00747 return time;
00748 }
00749
00750
00751
00752
00760 CPKIFTime CPKIFTime::operator-(
00762 const CPKIFDuration& duration_) const
00763 {
00764 CPKIFTime time(*this);
00765 long lSecs = 0;
00766
00767 try
00768 {
00769 lSecs = numeric_cast<long>(duration_.asSeconds());
00770 }
00771 catch(bad_numeric_cast &)
00772 {
00773 throw CPKIFException(TOOLKIT_ASN, COMMON_INVALID_INPUT, "Double not fit for a long.");
00774 }
00775
00776 time.m_impl->m_time -= boost::posix_time::time_duration(boost::posix_time::seconds(lSecs));
00777 return time;
00778 }
00779
00780
00781
00782
00790 CPKIFDuration CPKIFTime::operator-(
00792 const CPKIFTime& time_) const
00793 {
00794 CPKIFDuration duration;
00795 duration.setSeconds(boost::posix_time::time_duration(m_impl->m_time - time_.m_impl->m_time).total_seconds());
00796
00797 return duration;
00798 }
00799
00800
00801
00802
00812 bool CPKIFTime::operator ==(
00814 const CPKIFTime& time_) const
00815 {
00816 return( m_impl->m_time == time_.m_impl->m_time );
00817 }
00818
00819
00820
00821
00831 bool CPKIFTime::operator !=(
00833 const CPKIFTime& time_) const
00834 {
00835 return( m_impl->m_time != time_.m_impl->m_time );
00836 }
00837
00838
00839
00840
00850 bool CPKIFTime::operator <(
00852 const CPKIFTime& time_) const
00853 {
00854 return( m_impl->m_time < time_.m_impl->m_time );
00855 }
00856
00857
00858
00859
00869 bool CPKIFTime::operator >(
00871 const CPKIFTime& time_) const
00872 {
00873 return( m_impl->m_time > time_.m_impl->m_time );
00874 }
00875
00876
00877
00878
00888 bool CPKIFTime::operator >=(
00890 const CPKIFTime& time_) const
00891 {
00892 return( m_impl->m_time >= time_.m_impl->m_time );
00893 }
00894
00895
00896
00897
00907 bool CPKIFTime::operator <=(
00909 const CPKIFTime& time_) const
00910 {
00911 return( m_impl->m_time <= time_.m_impl->m_time );
00912 }