Set values ​​(year, month, day ...) in boost :: posix_time

In the class, I have a boost :: posix_time :: ptime attribute that refers to a date and time:

boost :: posix_time :: ptime p_;

In the constructor, I can pass values ​​and set them without problems.

my_class::my_class( ... )
  : p_( boost::posix_time::ptime( boost::gregorian::date(y,M,d),
                                  hours(h) + minutes(m) + seconds(s) +
                                  milliseconds(ms) + microseconds(us) +
                                  nanosec(ns));

I would like to create set methods (add and subtract) the values ​​for all the fields of this ptime (year, month, day, hours ... if possible).

If I use ptime_.date (), it returns a link minus dates, and I cannot set it directly.

I would like to do something like this:

void my_class::set_year(qint64 y) {
  // p_.date().year = y;
}

Is it possible?

I thought about creating a reset (...) method and asked what I needed, but for this purpose it sounds strange (copy all the values ​​and repeat them in the code).

Rgds.

+4
2

ptime :

boost:: posix_time:: ptime - . ptime . (...) posix_time tm-, time_t FILETIME.

, struct tm . .

#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/date.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
/*
 * 
 */

using namespace boost::posix_time;
using namespace boost::gregorian;

void set_year( uint64_t y, ptime& p) {
    tm pt_tm = to_tm( p);
    pt_tm.tm_year = y - 1900;
    p = ptime_from_tm( pt_tm);
}

int main(int argc, char** argv) {
   ptime t1( date(2002,Feb,10), hours(5)+minutes(4)+seconds(2)); 
   std::cout << to_simple_string( t1) << std::endl;
   set_year( 2001, t1);
   std::cout << to_simple_string( t1);
}

:

2002--10 05:04:02

2001--10 05:04:02

+4

, , - , .

Posix Time posix, API:

boost::posix_time::ptime . ptime , .

, : ptime, .

, , . .

ptime,

  • , ptime ,
+2

Source: https://habr.com/ru/post/1536661/


All Articles