Time.cpp

Go to the documentation of this file.
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 }, // Offset starts at 1
00027     { 31, false }, // Jan
00028     { 28, true  }, // Feb
00029     { 31, false }, // Mar
00030     { 30, false }, // Apr
00031     { 31, false }, // May
00032     { 30, false }, // Jun
00033     { 31, false }, // Jul
00034     { 31, false }, // Aug
00035     { 30, false }, // Sep
00036     { 31, false }, // Oct
00037     { 30, false }, // Nov
00038     { 31, false }  // Dec
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     // XXX TODO: This does not appear to handle offsets
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 // CPKIFTime - Default constructor
00146 // 
00155 CPKIFTime::CPKIFTime()
00156 :m_impl(new CPKIFTimeImpl)
00157 {
00158 }
00159 
00160 //
00161 // CPKIFTime - Copy Constructor
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         //fromUtcTime(time.u.utcTime, *this);
00195         fromUtcTime(time, *this);
00196     }
00197     else
00198     {
00199         fromGenTime(time, *this);
00200         //fromGenTime(time.u.generalTime, *this);
00201     }
00202 }
00214 CPKIFTime::CPKIFTime(const char* time)
00215 :m_impl(new CPKIFTimeImpl)
00216 {
00217     //Added NULL check 3/7/2005 Armen
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 // CPKIFTime - Destructor
00229 // 
00237 CPKIFTime::~CPKIFTime()
00238 {
00239     if(m_impl) {
00240         delete m_impl;
00241         m_impl = 0;
00242     }
00243 }
00244 
00245 //---------------------------------------------------------------------------
00246 // Properties
00247 //---------------------------------------------------------------------------
00248 
00249 //
00250 // year - returns the year
00251 // 
00259 int CPKIFTime::year() const
00260 {
00261     return m_impl->m_time.date().year();
00262 }
00263 
00264 //
00265 // month - returns the month of the year (1 = January, 12=December)
00266 //
00275 int CPKIFTime::month() const
00276 {
00277     return m_impl->m_time.date().month();
00278 }
00279 
00280 //
00281 // dayOfMonth - returns the day of the month (1 = first day of month, 31= 31st day)
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 // minutes - returns the minutes into the hour (0-59)
00309 // 
00318 int CPKIFTime::minutes() const
00319 {
00320     return m_impl->m_time.time_of_day().minutes();
00321 }
00322 
00323 //
00324 // seconds - returns the seconds into the minute (0-59)
00325 // 
00333 int CPKIFTime::seconds() const
00334 {
00335     return m_impl->m_time.time_of_day().seconds();
00336 }
00337 
00343 //@b Interface: External
00344 //
00345 //This function returns an instance of CPKIFTIme containing the time value representing the beginning of the day of the 
00346 //time value held by the instance on which this function was invoked.
00347 //
00348 //@return An instance of CPKIFTIme containing the time value representing the beginning of the day of the time value held by the instance on which this function was invoked.
00349 //*/
00350 //CPKIFTime CPKIFTime::startOfDay() const
00351 //{
00352 //  // Get new instance based on the same time as the receiver
00353 //  CPKIFTime time(*this);
00354 //
00355 //  // Make implementation writable
00356 //  time.impl() = impl()->writableCopy();
00357 //
00358 //  // Move time to start of day
00359 //  time.impl()->startOfDay();
00360 //
00361 //  return time;
00362 //}
00363 
00369 //@b Interface: External
00370 //
00371 //This function returns an instance of CPKIFTIme containing the time value representing the beginning
00372 //of the week of the time value held by the instance on which this function was invoked.
00373 //
00374 //@return An instance of CPKIFTIme containing the time value representing the beginning of the week of the time value held by the instance on which this function was invoked.
00375 //*/
00376 //CPKIFTime CPKIFTime::startOfWeek() const
00377 //{
00378 //  // Get new instance based on the same time as the receiver
00379 //  CPKIFTime time(*this);
00380 //
00381 //  // Make implementation writable
00382 //  time.impl() = impl()->writableCopy();
00383 //
00384 //  // Move time to start of day
00385 //  time.impl()->startOfWeek();
00386 //
00387 //  return time;
00388 //}
00389 
00395 //@b Interface: External
00396 //
00397 //This function returns an instance of CPKIFTIme containing the time value representing the beginning of
00398 //the month of the time value held by the instance on which this function was invoked.
00399 //
00400 //@return An instance of CPKIFTIme containing the time value representing the beginning of the month of the time value held by the instance on which this function was invoked.
00401 //*/
00402 //CPKIFTime CPKIFTime::startOfMonth() const
00403 //{
00404 //  // Get new instance based on the same time as the receiver
00405 //  CPKIFTime time(*this);
00406 //
00407 //  // Make implementation writable
00408 //  time.impl() = impl()->writableCopy();
00409 //
00410 //  // Move time to start of day
00411 //  time.impl()->startOfMonth();
00412 //
00413 //  return time;
00414 //}
00415 
00421 //@b Interface: External
00422 //
00423 //This function returns an instance of CPKIFTIme containing the time value representing the beginning of 
00424 //the year of the time value held by the instance on which this function was invoked.
00425 //
00426 //@return An instance of CPKIFTIme containing the time value representing the beginning of the year of the time value held by the instance on which this function was invoked.
00427 //*/
00428 //CPKIFTime CPKIFTime::startOfYear() const
00429 //{
00430 //  // Get new instance based on the same time as the receiver
00431 //  CPKIFTime time(*this);
00432 //
00433 //  // Make implementation writable
00434 //  time.impl() = impl()->writableCopy();
00435 //
00436 //  // Move time to start of day
00437 //  time.impl()->startOfYear();
00438 //
00439 //  return time;
00440 //}
00441 
00442 //---------------------------------------------------------------------------
00443 // Methods
00444 //---------------------------------------------------------------------------
00445 
00446 //
00447 // print - returns a string representing the time held by the receiver
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; //added endl - 7/19/2004
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 // set - adjust the current instance of time according to the provided gregorian date/time
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 //@b Interface: External
00529 //
00530 //This function is used to set the value of an instance of CPKIFTime.
00531 //
00532 //@return None
00533 //
00534 //@exception CPKIFException(ASN1_INVALID_TIME)
00535 //*/
00536 //void CPKIFTime::setJulian(
00537 //  //![in] Integer containing the year value to set
00538 //  int  year_, 
00539 //  //![in] Integer containing the julian day value to set (1-366)
00540 //  int julianDay_,
00541 //  //![in] Integer containing the hours into the day value to set (0-23)
00542 //  int hours_, 
00543 //  //![in] Integer containing the minutes into the hour value to set (0-59)
00544 //  int minutes_, 
00545 //  //![in] Integer containing the seconds into the minute value to set (0-59)
00546 //  int seconds_  )
00547 //{
00548 //  CPKIFStringPtr emptyStr;
00549 //  m_timeStr = emptyStr;
00550 //
00551 //  // Validate date
00552 //  if( !_CPKIFTimeImpl::isJulianDateValid(year_, julianDay_) )
00553 //  {
00554 //      throw CPKIFException(TOOLKIT_ASN, ASN1_INVALID_TIME);
00555 //  }
00556 //
00557 //  // Validate time
00558 //  if( !_CPKIFTimeImpl::isTimeValid(hours_, minutes_, seconds_) )
00559 //  {
00560 //      throw CPKIFException(TOOLKIT_ASN, ASN1_INVALID_TIME);
00561 //  }
00562 //
00563 //  // Get writable copy
00564 //  impl() = impl()->writableCopy();
00565 //
00566 //  // Set variables
00567 //  impl()->set(year_, 1, 1, hours_, minutes_, seconds_);
00568 //  impl()->m_absoluteTime += (julianDay_ - 1);
00569 //  impl()->updateFromTime();
00570 //}
00571 
00576 //@b Interface: External
00577 //
00578 //This function is used to add a specified number of days to the time value held by an instance of CPKIFTime.
00579 //
00580 //@return None
00581 //*/
00582 //void CPKIFTime::addDays( 
00583 //  //![in] Integer containing the number of days to add to the time value
00584 //  int days_ )
00585 //{
00586 //  CPKIFStringPtr emptyStr;
00587 //  m_timeStr = emptyStr;
00588 //
00589 //  // Get writable copy
00590 //  impl() = impl()->writableCopy();
00591 //
00592 //  // Add number of days
00593 //  impl()->m_absoluteTime += days_;
00594 //  impl()->updateFromTime();
00595 //}
00596 
00601 //@b Interface: External
00602 //
00603 //This function is used to add a specified number of years to the time value held by an instance of CPKIFTime.
00604 //
00605 //@return None
00606 //*/
00607 //void CPKIFTime::addYears( 
00608 //  //![in] Integer containing the number of years to add
00609 //  int years_ )
00610 //{
00611 //  CPKIFStringPtr emptyStr;
00612 //  m_timeStr = emptyStr;
00613 //
00614 //  // Get writable copy
00615 //  impl() = impl()->writableCopy();
00616 //
00617 //  // Add a number of years
00618 //  impl()->addYears( years_ );
00619 //}
00620 
00621 
00622 //---------------------------------------------------------------------------
00623 // operators
00624 //---------------------------------------------------------------------------
00625 
00626 //
00627 // operator =(const CPKIFTime&), assignment operator
00628 //
00637 CPKIFTime& CPKIFTime::operator =(
00639     const CPKIFTime& time_)
00640 {
00641     CPKIFStringPtr emptyStr;
00642     m_timeStr = emptyStr;
00643     
00644     // Ensure this is a different implementation
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 // operator +=(const CPKIFDuration& duration_) - add a duration to receiver
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 // operator -=(const CPKIFDuration& duration_) - substract a duration from receiver
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 // operator +(const CPKIFDuration& duration_) - add receiver with duration and return result
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 // operator -(const CPKIFDuration& duration_) - substract duration from receiver and return result
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 // operator -(const CPKIFTime& time_) - get duration by substracting two times
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     //duration.setDays( impl()->m_absoluteTime - time_.impl()->m_absoluteTime );
00797     return duration;
00798 }
00799 
00800 //
00801 // operator == - true if the times are exactly equivalent
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 // operator != - true if the times are not equal
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 // operator < - true if the specified time is later (greater) than this
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 // operator > - true if the specified time is before (less) than this
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 // operator >= - return true if the specified time is the same or before this
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 // operator <= - return true if the specified time is the same or after this
00897 // 
00907 bool CPKIFTime::operator <=(
00909     const CPKIFTime& time_) const
00910 {
00911     return( m_impl->m_time <= time_.m_impl->m_time );
00912 }

Generated on Mon Nov 15 11:15:56 2010 for PublicKeyInfrastructureFramework(PKIF) by  doxygen 1.5.6