How to convert boost::posix_time::ptime to YYMMDDHHMM? - boost

I am trying to convert boost::posix_time::ptime to YYMMDDHHMM format string. How to accomplish this?
Related: How to convert a boost::ptime to string

#include <iostream>
#include <cstdlib>
#include <string>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/format.hpp>
int main()
{
std::string submitDateString = "20190911T235959";
boost::posix_time::ptime submitPtime = boost::posix_time::from_iso_string( submitDateString );
// Use a facet to display time in a custom format (only hour and minutes).
std::stringstream sstream;
boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
facet->format("%y%m%d%H%M");
sstream.imbue(std::locale(std::locale::classic(), facet));
sstream << submitPtime;
std::cout << "submit date:" << sstream.str( ) << std::endl;
}

Related

boost filesystem making long path under windows using \\?\

it is so simple but i can not make it work and i do not know why??
i just want to make directory with long path.
i add \\\\?\\ to E:\\... to make it as win api says .
but in vain.....nothing but error.
i tried \\?\ and \?\ with no success.
this is the code :
// boost_create_directory.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <string>
#include <time.h>
#include <stdio.h>
#include <ctime>
#include <iostream>
#include <map> //Needed to use the std::map class.
//#include "symbols_array2.h"
//#include "functions.h"
#include <boost/filesystem.hpp>
//#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
////#include <filesystem>
////namespace fs = std::filesystem;
//#define DOWNLOAD_ALL true
int main()
{
using namespace std;
using namespace boost::filesystem;
//string localpath, binlocalfile, localfile;
string localpath = "\\\\?\\E:\\instruments\\symbol\\year\\month\\day\\";
//boost::filesystem::path abs_localpath;
////string localpath = "E:\\instruments\\symbol\\year\\month\\day\\";
boost::filesystem::path abs_localpath(localpath) ;
////string localpath = "\\\\?\\E:\\instruments\\symbol\\year\\month\\day\\";
////boost::filesystem::path abs_localpath("\\\\?\\E:\\instruments\\symbol\\year\\month\\day\\");
////string localpath = "E:/i/";
////boost::filesystem::path abs_localpath("\\?\E:\instruments\symbol\year\month\day\\");
////boost::filesystem::path path_argument(localpath);
////boost::filesystem::path path_native(path_argument.make_preferred());
////boost::filesystem::path abs_localpath(absolute(path_native));
//binlocalfile = localpath + "\\hourh_ticks.bin";
//localfile = localpath + "\\hourh_ticks.bi5";
//abs_localpath = boost::filesystem::absolute(localpath.c_str());
//abs_localpath = localpath;
//if (!boost::filesystem::exists(abs_localpath))
//{
//boost::filesystem::path abs_localpath;
//cout << current_path().string() << endl;
//cout << abs_localpath << endl;
//boost::filesystem::create_directory(abs_localpath);
for (int z = 0; z < 10; z++)
{
if (boost::filesystem::create_directory(abs_localpath)) {
std::cout << "Success making new directory" << "\n";
//boost::filesystem::permissions(abs_localpath, perms_mask);
}
//localpath = localpath + "\\instruments\\symbol\\year\\month\\day";
abs_localpath / "instruments\\symbol\\year\\month\\day\\";
////abs_localpath /= "\instruments\symbol\year\month\day\";
}
//mkdir($localpath, 0777, true);
//}
boost::filesystem::path path("\\?\\E:\\MyStuff\\");
boost::filesystem::create_directory(path);
return 0;
}
i hope to find answer here.thanks in advance.
it was my fault .the create_directory function make single directory if path is already created and return with error if path is missing,So i needed create_directories the plural version make all missing elements of path and voila.
i wish they change these names to create_single_directory or create_target_directory and change the plural to create_path_directories

How to Convert Custom string to ptime using boost

I have a string "2018Jan23T181138.65498648" which I need to convert to ptime. I have used below code but seems it is not working. Any idea what I am doing wrong here.
boost::posix_time::ptime pt;
std::istringstream is("2018Jan23T181138.65498648");
is.imbue(std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y%m%dT%H%M%S.%f")));
is >> pt;
std::cout << pt;
You need to at least match the format string to reflect the input format.
"Jan" is not a valid match for %Y%m%d (which would expect 20180123 instead). Likewise, %S.%f is a format string that might work for formatting¹, but to parse the seconds with fractions, the docs show to use %s
Live On Coliru
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <sstream>
#include <iostream>
int main() {
boost::posix_time::ptime pt;
std::istringstream is("2018Jan23T181138.65498648");
is.imbue(std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y%b%dT%H%M%s")));
if (is >> pt) {
std::cout << pt << "\n";
} else {
std::cout << "unparsed\n";
}
}
Prints
2018-Jan-23 18:11:38.654986
¹ haven't tested it for output formatting

boost locale bad cast exception

The following simple code, compiled with -lboost_locale generates a bad cast exception. It is the same code in the boost tutorial itself. Any help?
#include <boost/locale.hpp>
#include <iostream>
int main()
{
using namespace boost::locale;
date_time now;
std::cout<<as::date<<now<<std::endl;
}
You need to imbue (global) locales:
Live On Coliru
#include <boost/locale.hpp>
#include <iostream>
int main() {
using namespace boost::locale;
boost::locale::generator gen;
std::locale loc = gen.generate(""); // or "C", "en_US.UTF-8" etc.
std::locale::global(loc);
std::cout.imbue(loc);
date_time_period_set things;
date_time now;
std::cout << as::date << now << std::endl;
}
Prints, e.g. on coliru:
09/17/15

Set Container Not Outpuitting Names

I have to use a set container with 10 names. It outputs "Myset contains" but doesn't list the names and I'm not sure why.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <set>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
std::string names[] = {"Nathan", "Dereck", "Robert", "Michael", "Elliot"
"Oliva", "Sophia", "Jessica", "Alexis", "Erin"};
//std::set <std::string> set(std::begin(names), std::end(names));
std::set<string> mynames (names, names);
std::cout << "Myset contains: ";
for (std::set<string>::iterator it=mynames.begin(); it!=mynames.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
int foo;
cin >> foo;
return 0;
}
Change:
std::set<string> mynames (names, names);
into:
std::set<string> mynames (names, names+number_of_elements);

Storing a boost::spirit::qi::rule in a std::list

I have read the other thread about copy or reference semantics for boost::spirt::qi::rule. I am using Boost 1.42.
using boost::spirit::qi::phrase_parse;
typedef boost::spirit::qi::rule < std::string::const_iterator, boost::spirit::ascii::space_type > rule_type;
std::list < rule_type > ruleList;
std::string const s("abcdef");
std::string::const_iterator iter = s.begin(), end = s.end();
std::cout << typeid(char_).name() << std::endl;
ruleList.push_back(char_);
ruleList.push_back(*ruleList.back());
assert(phrase_parse(iter, s.end(), ruleList.back(), boost::spirit::ascii::space));
assert(iter == s.end());
This fails with...
Assertion `phrase_parse(iter, s.end(), ruleList.back(), traits::space())' failed.
Aborted (core dumped)
Is there a way to store rules in a STL list or deque? (References don't die until removed).
With Boost V1.45, this (essentially your code from above) works without problems (MSVC2010, g++ 4.5.1):
#include <list>
#include <string>
#include <iostream>
#include <boost/spirit/include/qi.hpp>
using namespace boost::spirit;
int main()
{
typedef qi::rule<std::string::const_iterator, ascii::space_type> rule_type;
std::list<rule_type> ruleList;
std::string const s("abcdef");
std::string::const_iterator iter = s.begin(), end = s.end();
std::cout << typeid(qi::char_).name() << std::endl;
ruleList.push_back(qi::char_);
ruleList.push_back(*ruleList.back());
assert(qi::phrase_parse(iter, s.end(), ruleList.back(), ascii::space));
assert(iter == s.end());
return 0;
}
Therefore, I assume it's a bug in the version of Spirit you're using.
I could not get your example to compile. Aside from not using the correct types from ...::qi, you added a () to the trait::space type.
This works w/o problem for me (boost 1.44)
#include <boost/spirit/include/qi.hpp>
#include <string>
#include <vector>
#include <cassert>
using boost::spirit::qi::phrase_parse;
typedef boost::spirit::qi::rule < std::string::const_iterator, boost::spirit::qi::space_type > rule_type;
int main() {
std::list < rule_type > ruleList;
std::string const s("abcdef");
std::string::const_iterator iter = s.begin(), end = s.end();
ruleList.push_back(*boost::spirit::qi::char_);
assert(phrase_parse(iter, s.end(), ruleList.back(), boost::spirit::qi::space));
assert(iter == s.end());
}
~>g++ test.cpp && ./a.out
~>
please note I use qi::space_type and `qi::space instead of the ascii namespace. I have no idea what/where the trait namespace is.

Resources