Guest User

Untitled

a guest
Oct 17th, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1. // .. in the pch.h
  2. #include <boost/date_time/gregorian/gregorian.hpp> //include all types plus i/o
  3. namespace bgdt= boost::gregorian;
  4.  
  5. // .   ......................................................
  6. //The HEDate.hpp
  7. #pragma once
  8.  
  9. class HEDate
  10. {
  11.     bgdt::date date;
  12.     bool bValid;
  13.  
  14. public:
  15.     HEDate( ):bValid(false){ }
  16.     HEDate( const bgdt::date& in )//assumes a valid date.
  17.         :date( in )
  18.         ,bValid(true)
  19.     { }
  20.     HEDate( const COleDateTime& dtIn )
  21.         :bValid( false )
  22.     {
  23.         try{
  24.             if( dtIn.m_status != COleDateTime::valid )
  25.                 return;
  26.  
  27.             bgdt::date inDate( dtIn.GetYear( ), dtIn.GetMonth( ), dtIn.GetDay( ) );
  28.             //didn't throw
  29.             date= inDate;
  30.             bValid= true;
  31.         }
  32.         catch( std::exception& e )  {e; }
  33.     }
  34.     void operator = (const COleDateTime& dtIn) {
  35.         bValid = false;
  36.         try {
  37.             if (dtIn.m_status != COleDateTime::valid)
  38.                 return;
  39.  
  40.             bgdt::date inDate(dtIn.GetYear(), dtIn.GetMonth(), dtIn.GetDay());
  41.             //didn't throw
  42.             date = inDate;
  43.             bValid = true;
  44.         }
  45.         catch (std::exception & e){e;}
  46.     }
  47.     //CString Format( ) const, depreciated, not using CString anymore.............
  48.     void AddYear(size_t dur = 1)
  49.     {
  50.         ASSERT(bValid);
  51.         try {
  52.             bgdt::date::ymd_type ymd = date.year_month_day();
  53.             ymd.year = ymd.year + (unsigned short)dur;
  54.             date = bgdt::date(ymd);
  55.         }
  56.         catch (std::exception & e)  {
  57.             bValid = false;
  58.             e;
  59.             //e; //no what, just throws
  60.         }
  61.     }
  62.     void AddDay(size_t dur = 1)
  63.     {
  64.         ASSERT(bValid);
  65.         try {
  66.             date += bgdt::date_duration(dur);
  67.         }
  68.         catch (std::exception & e) {
  69.             bValid = false;
  70.             e;
  71.         }
  72.     }
  73.     bgdt::date GetDate( ) const { return date; }
  74.     bool IsValid( ) const { return bValid; }
  75.     operator COleDateTime ( ) const { //check if valid....
  76.         return COleDateTime( date.year( ), date.month( ), date.day( ), 0, 0, 0 );
  77.     }
  78.     operator const bgdt::date& ( ) const { return date; }
  79.     friend bool operator < (const HEDate in1, const HEDate in2) { return in1.date < in2.date; }
  80.  
  81.     std::string format(const char* psFmt = "%D") const;
  82.     std::wstring formatW(const char* psFmt = "%D") const { return to_wide(format(psFmt).c_str()); }
  83.     int years_span(const HEDate& from) { if (!from.IsValid() || !IsValid()) return 0; return date.year() - ((bgdt::date)from).year(); }
  84.     long get_year()const { return date.year(); }
  85.     long get_month()const { return date.month(); }
  86. protected:
  87.     //return false if lookup fails ?
  88.     //update this and return a composed std::wstring, then depreciate
  89.     void FormatSub(std::string_view::iterator f, std::stringstream& val ) const;
  90. };
  91.  
  92. inline std::ostream& operator << ( std::ostream& os, HEDate& date ){
  93.     os << date.format("%N");
  94.     return os;
  95. }
  96.  
  97. //.   .................................................................
  98. //the HEDate.cpp
  99.  
  100. //official flags:
  101. //https://www.boost.org/doc/libs/1_35_0/doc/html/date_time/date_time_io.html#date_time.format_flags
  102.  
  103. namespace {
  104.     using iterator = std::string_view::const_iterator;
  105. }
  106.  
  107. void HEDate::FormatSub(iterator f, std::stringstream& hold) const
  108. {
  109.     switch (*f)
  110.     {
  111.     case 'B'://December
  112.         hold << en_long_month_names[date.month() - 1];
  113.         break;
  114.  
  115.     case 'd'://31
  116.         hold << date.day();
  117.         break;
  118.  
  119.     case 'm'://12
  120.         hold << date.month().as_number();
  121.         break;
  122.  
  123.     case 'Y'://2020
  124.         hold << date.year();
  125.         break;
  126.  
  127.     case 'y'://20
  128.     {
  129.         auto dt = date.year() - (date.year() / 100) * 100;
  130.         hold << (dt < 10 ? "0" : "") << date.year() - (date.year() / 100) * 100;
  131.         break;
  132.     }
  133.     case 'D'://2020/12/31
  134.         hold << date.year() << "/" << date.month().as_number() << "/" << date.day();
  135.         break;
  136.  
  137.     case 'N'://1/31/2020
  138.         hold << date.month().as_number() << "/" << date.day() << "/" << date.year();
  139.         break;
  140.  
  141.     default://??
  142.         hold << "??";
  143.         break;
  144.  
  145.     }//switch
  146. }
  147.  
  148. std::string HEDate::format( const char* psFmt ) const
  149. {
  150.     if( ! bValid )
  151.         return "Invalid Date";//or we can crash/exception below
  152.  
  153.     std::string_view s( psFmt );
  154.     std::stringstream hold;
  155.     std::vector< std::pair< iterator, iterator> > tokens;
  156.     boost::split( tokens, s, boost::is_any_of("%") );
  157.  
  158.     for( auto it= tokens.begin( ); it != tokens.end( ); ++it)
  159.     {
  160.         //no leading char
  161.         if (it->first == it->second)
  162.             continue;
  163.  
  164.         //a leading char set.
  165.         if (it == tokens.begin()) {
  166.             hold << std::string(it->first, it->second);
  167.             continue;
  168.         }
  169.         FormatSub(it->first++, hold);
  170.         if(it->first != it->second)//more printables
  171.             hold << std::string(it->first, it->second);
  172.     }
  173.     return hold.str( );
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment