Ariel Baron Ariel Baron - 8 months ago 48
C++ Question

Declare Boost ptime globally

I am attmepting to declare boost ptime at the global level. However I am encountering the following issue:

Before the main function I declare:

static boost::posix_time::ptime start_time;

Then inside the main function I initialize:

boost::posix_time::ptime start_time(boost::posix_time::microsec_clock::universal_time());

Then in a function outside the main function, I tried to convert to a string and print and I get "not-a-date-time";

Either the global variable is not being initialized or there is a scope problem?
Thank you for your help.


You define a local variable inside the main function, one that shadows the global variable but has nothing to do with the global variable.

You should not define the variable in the main function, only assign to it:

start_time = boost::posix_time::microsec_clock::universal_time();