c++boostboost-date-time

c++ boost::posix_time::ptime default value


I have a member variable in my class:

class Foo
{
// ...
private:
  boost::posix_time::ptime t;
}

I want to initialize it in the constructor to a well known value such that I know it hasn't been set by the program yet:

Foo::Foo()
   : t(NULL) // doesnt work
{}

But setting it to NULL doesn't work because its not a pointer.

How do I initialize boost::posix_time::ptime to a well known value?


Solution

  • The default constructor initializes it to boost::posix_time::not_a_date_time. There is a member function to check for that t.is_not_a_date_time(). For more info see the docs.

    #include <iostream>
    #include <boost/date_time/posix_time/posix_time.hpp>
    
    class Foo
    {
    public:
      boost::posix_time::ptime t;
      Foo() : t() {}
    };
    
    int main()
    {
      Foo foo;
      std::cout << std::boolalpha
                << foo.t.is_not_a_date_time() << '\n';
    }
    

    Demo on Wandbox