Domanda

I am using the following code to get the current date time (Mountain time)

const boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();

    //In mountain time I get now = 2013-Apr-08 20:44:22

Now I am using the following method for conversion

ptime FeedConnector::MountaintToEasternConversion(ptime coloTime) 
{

      return boost::date_time::local_adjustor <ptime, -5, us_dst>::utc_to_local(coloTime);
} 

//This function is suppose to give me the time in NewYork (East standard time) and I am getting

2013-Apr-08 16:44:22

Thsi time is wrong any suggestion where I am going wrong ?

È stato utile?

Soluzione

As far as I understand wrong time means that it is has one hour difference to expected, i.e. -4 hours instead of expected -5 hours. If yes, then the problem is that the us_std type is pointed as the last parameter of the local_adjustor declaration. If to specify no_dst instead of the use_dst. The code works as expatiated and the difference is -5 hours. The following code demonstrates it (link to online compiled version)

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/local_time_adjustor.hpp>
#include <iostream>

int main(void) {
   const boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
   const boost::posix_time::ptime adjUSDST = boost::date_time::local_adjustor<boost::posix_time::ptime, -5, boost::posix_time::us_dst>::utc_to_local(now);
   const boost::posix_time::ptime adjNODST = boost::date_time::local_adjustor<boost::posix_time::ptime, -5, boost::posix_time::no_dst>::utc_to_local(now);
   std::cout << "now: " << now << std::endl;
   std::cout << "adjUSDST: " << adjUSDST << std::endl;
   std::cout << "adjNODST: " << adjNODST << std::endl;
   return 0;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top