Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // .. in the pch.h
- #include <boost/date_time/gregorian/gregorian.hpp> //include all types plus i/o
- namespace bgdt= boost::gregorian;
- // . ......................................................
- //The HEDate.hpp
- #pragma once
- class HEDate
- {
- bgdt::date date;
- bool bValid;
- public:
- HEDate( ):bValid(false){ }
- HEDate( const bgdt::date& in )//assumes a valid date.
- :date( in )
- ,bValid(true)
- { }
- HEDate( const COleDateTime& dtIn )
- :bValid( false )
- {
- try{
- if( dtIn.m_status != COleDateTime::valid )
- return;
- bgdt::date inDate( dtIn.GetYear( ), dtIn.GetMonth( ), dtIn.GetDay( ) );
- //didn't throw
- date= inDate;
- bValid= true;
- }
- catch( std::exception& e ) {e; }
- }
- void operator = (const COleDateTime& dtIn) {
- bValid = false;
- try {
- if (dtIn.m_status != COleDateTime::valid)
- return;
- bgdt::date inDate(dtIn.GetYear(), dtIn.GetMonth(), dtIn.GetDay());
- //didn't throw
- date = inDate;
- bValid = true;
- }
- catch (std::exception & e){e;}
- }
- //CString Format( ) const, depreciated, not using CString anymore.............
- void AddYear(size_t dur = 1)
- {
- ASSERT(bValid);
- try {
- bgdt::date::ymd_type ymd = date.year_month_day();
- ymd.year = ymd.year + (unsigned short)dur;
- date = bgdt::date(ymd);
- }
- catch (std::exception & e) {
- bValid = false;
- e;
- //e; //no what, just throws
- }
- }
- void AddDay(size_t dur = 1)
- {
- ASSERT(bValid);
- try {
- date += bgdt::date_duration(dur);
- }
- catch (std::exception & e) {
- bValid = false;
- e;
- }
- }
- bgdt::date GetDate( ) const { return date; }
- bool IsValid( ) const { return bValid; }
- operator COleDateTime ( ) const { //check if valid....
- return COleDateTime( date.year( ), date.month( ), date.day( ), 0, 0, 0 );
- }
- operator const bgdt::date& ( ) const { return date; }
- friend bool operator < (const HEDate in1, const HEDate in2) { return in1.date < in2.date; }
- std::string format(const char* psFmt = "%D") const;
- std::wstring formatW(const char* psFmt = "%D") const { return to_wide(format(psFmt).c_str()); }
- int years_span(const HEDate& from) { if (!from.IsValid() || !IsValid()) return 0; return date.year() - ((bgdt::date)from).year(); }
- long get_year()const { return date.year(); }
- long get_month()const { return date.month(); }
- protected:
- //return false if lookup fails ?
- //update this and return a composed std::wstring, then depreciate
- void FormatSub(std::string_view::iterator f, std::stringstream& val ) const;
- };
- inline std::ostream& operator << ( std::ostream& os, HEDate& date ){
- os << date.format("%N");
- return os;
- }
- //. .................................................................
- //the HEDate.cpp
- //official flags:
- //https://www.boost.org/doc/libs/1_35_0/doc/html/date_time/date_time_io.html#date_time.format_flags
- namespace {
- using iterator = std::string_view::const_iterator;
- }
- void HEDate::FormatSub(iterator f, std::stringstream& hold) const
- {
- switch (*f)
- {
- case 'B'://December
- hold << en_long_month_names[date.month() - 1];
- break;
- case 'd'://31
- hold << date.day();
- break;
- case 'm'://12
- hold << date.month().as_number();
- break;
- case 'Y'://2020
- hold << date.year();
- break;
- case 'y'://20
- {
- auto dt = date.year() - (date.year() / 100) * 100;
- hold << (dt < 10 ? "0" : "") << date.year() - (date.year() / 100) * 100;
- break;
- }
- case 'D'://2020/12/31
- hold << date.year() << "/" << date.month().as_number() << "/" << date.day();
- break;
- case 'N'://1/31/2020
- hold << date.month().as_number() << "/" << date.day() << "/" << date.year();
- break;
- default://??
- hold << "??";
- break;
- }//switch
- }
- std::string HEDate::format( const char* psFmt ) const
- {
- if( ! bValid )
- return "Invalid Date";//or we can crash/exception below
- std::string_view s( psFmt );
- std::stringstream hold;
- std::vector< std::pair< iterator, iterator> > tokens;
- boost::split( tokens, s, boost::is_any_of("%") );
- for( auto it= tokens.begin( ); it != tokens.end( ); ++it)
- {
- //no leading char
- if (it->first == it->second)
- continue;
- //a leading char set.
- if (it == tokens.begin()) {
- hold << std::string(it->first, it->second);
- continue;
- }
- FormatSub(it->first++, hold);
- if(it->first != it->second)//more printables
- hold << std::string(it->first, it->second);
- }
- return hold.str( );
- }
Advertisement
Add Comment
Please, Sign In to add comment