Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Date time conversion and adding seconds

Tags:

c++

boost

I need to load in a string of format 16/11/2012 11:00:00 i.e %d/%m/%Y %H:%M and then repeatedly add 900 seconds to it. The code below is an example, could you help me get it working?

    #include "stdafx.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <sstream>

#include "boost/date_time/local_time/local_date_time.hpp"

int _tmain(int argc, _TCHAR* argv[])
{
  unsigned int seconds=900;
  using namespace boost::local_time;
  std::stringstream ss;

  // Set up the input datetime format.
  local_time_input_facet *input_facet = new local_time_input_facet("%d/%m/%Y %H:%M");
  ss.imbue(std::locale(ss.getloc(), input_facet));

  local_date_time date(not_a_date_time);
  ss.str("16/11/2012 11:00:00");
  ss >>date;

  local_date_time now=date+seconds;
  std::cout << now << std::endl;
  return 0;
}

Any help would be greatly appreciated, James

like image 651
James Avatar asked Mar 21 '23 14:03

James


1 Answers

I don't have boost in hands right now, please try something like:

date + boost::posix_time::seconds(900)

like image 153
Andriy Tylychko Avatar answered Apr 01 '23 10:04

Andriy Tylychko