GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions | Friends
GncDateImpl Class Reference

Private implementation of GncDate. More...

Public Member Functions

 GncDateImpl (const int year, const int month, const int day)
 
 GncDateImpl (Date d)
 
 GncDateImpl (const std::string str, const std::string fmt)
 
void today ()
 
gnc_ymd year_month_day () const
 
std::string format (const char *format) const
 
std::string format_zulu (const char *format) const
 

Friends

bool operator< (const GncDateImpl &, const GncDateImpl &)
 
bool operator> (const GncDateImpl &, const GncDateImpl &)
 
bool operator== (const GncDateImpl &, const GncDateImpl &)
 
bool operator<= (const GncDateImpl &, const GncDateImpl &)
 
bool operator>= (const GncDateImpl &, const GncDateImpl &)
 
bool operator!= (const GncDateImpl &, const GncDateImpl &)
 

Detailed Description

Private implementation of GncDate.

See the documentation for that class.

Definition at line 336 of file gnc-datetime.cpp.

Constructor & Destructor Documentation

◆ GncDateImpl() [1/4]

GncDateImpl::GncDateImpl ( )
inline

Definition at line 339 of file gnc-datetime.cpp.

339: m_greg(boost::gregorian::day_clock::local_day()) {}

◆ GncDateImpl() [2/4]

GncDateImpl::GncDateImpl ( const int  year,
const int  month,
const int  day 
)
inline

Definition at line 340 of file gnc-datetime.cpp.

340 :
341 m_greg(year, static_cast<Month>(month), day) {}

◆ GncDateImpl() [3/4]

GncDateImpl::GncDateImpl ( Date  d)
inline

Definition at line 342 of file gnc-datetime.cpp.

342: m_greg(d) {}

◆ GncDateImpl() [4/4]

GncDateImpl::GncDateImpl ( const std::string  str,
const std::string  fmt 
)

Definition at line 707 of file gnc-datetime.cpp.

707 :
708 m_greg(boost::gregorian::day_clock::local_day()) /* Temporarily initialized to today, will be used and adjusted in the code below */
709{
710 auto iter = std::find_if(GncDate::c_formats.cbegin(), GncDate::c_formats.cend(),
711 [&fmt](const GncDateFormat& v){ return (v.m_fmt == fmt); } );
712 if (iter == GncDate::c_formats.cend())
713 throw std::invalid_argument(N_("Unknown date format specifier passed as argument."));
714
715 if (iter->m_str_to_date)
716 {
717 try
718 {
719 m_greg = (*iter->m_str_to_date)(str);
720 return;
721 }
722 catch (...) {} // with any string->date exception, try regex
723 }
724
725 if (iter->m_re.empty())
726 throw std::invalid_argument ("No regex pattern available");
727
728 boost::smatch what;
729 if(!boost::regex_search(str, what, iter->m_re)) // regex didn't find a match
730 throw std::invalid_argument (N_("Value can't be parsed into a date using the selected date format."));
731
732 // Bail out if a year was found with a yearless format specifier
733 auto fmt_has_year = (fmt.find('y') != std::string::npos);
734 if (!fmt_has_year && (what.length("YEAR") != 0))
735 throw std::invalid_argument (N_("Value appears to contain a year while the selected format forbids this."));
736
737 int year;
738 if (fmt_has_year)
739 {
740 /* The input dates have a year, so use that one */
741 year = std::stoi (what.str("YEAR"));
742
743 /* We assume two-digit years to be in the range 1969 - 2068. */
744 if (year < 69)
745 year += 2000;
746 else if (year < 100)
747 year += 1900;
748 }
749 else /* The input dates have no year, so use current year */
750 year = m_greg.year(); // Can use m_greg here as it was already initialized in the initializer list earlier
751
752 m_greg = Date(year,
753 static_cast<Month>(std::stoi (what.str("MONTH"))),
754 std::stoi (what.str("DAY")));
755}
static const std::vector< GncDateFormat > c_formats
A vector with all the date formats supported by the string constructor.

Member Function Documentation

◆ format()

std::string GncDateImpl::format ( const char *  format) const

Definition at line 765 of file gnc-datetime.cpp.

766{
767#ifdef __MINGW32__
768 return win_date_format(format, to_tm(m_greg));
769#else
770 using Facet = boost::gregorian::date_facet;
771 std::stringstream ss;
772 //The stream destructor frees the facet, so it must be heap-allocated.
773 auto output_facet(new Facet(normalize_format(format).c_str()));
774 ss.imbue(std::locale(gnc_get_locale(), output_facet));
775 ss << m_greg;
776 return ss.str();
777#endif
778}

◆ format_zulu()

std::string GncDateImpl::format_zulu ( const char *  format) const
inline

Definition at line 348 of file gnc-datetime.cpp.

348 {
349 return this->format(format);
350 }

◆ today()

void GncDateImpl::today ( )
inline

Definition at line 345 of file gnc-datetime.cpp.

345{ m_greg = boost::gregorian::day_clock::local_day(); }

◆ year_month_day()

gnc_ymd GncDateImpl::year_month_day ( ) const

Definition at line 758 of file gnc-datetime.cpp.

759{
760 auto boost_ymd = m_greg.year_month_day();
761 return {boost_ymd.year, boost_ymd.month.as_number(), boost_ymd.day};
762}

Friends And Related Symbol Documentation

◆ operator!=

bool operator!= ( const GncDateImpl a,
const GncDateImpl b 
)
friend

Definition at line 785 of file gnc-datetime.cpp.

785{ return a.m_greg != b.m_greg; }

◆ operator<

bool operator< ( const GncDateImpl a,
const GncDateImpl b 
)
friend

Definition at line 780 of file gnc-datetime.cpp.

780{ return a.m_greg < b.m_greg; }

◆ operator<=

bool operator<= ( const GncDateImpl a,
const GncDateImpl b 
)
friend

Definition at line 783 of file gnc-datetime.cpp.

783{ return a.m_greg <= b.m_greg; }

◆ operator==

bool operator== ( const GncDateImpl a,
const GncDateImpl b 
)
friend

Definition at line 782 of file gnc-datetime.cpp.

782{ return a.m_greg == b.m_greg; }

◆ operator>

bool operator> ( const GncDateImpl a,
const GncDateImpl b 
)
friend

Definition at line 781 of file gnc-datetime.cpp.

781{ return a.m_greg > b.m_greg; }

◆ operator>=

bool operator>= ( const GncDateImpl a,
const GncDateImpl b 
)
friend

Definition at line 784 of file gnc-datetime.cpp.

784{ return a.m_greg >= b.m_greg; }

The documentation for this class was generated from the following file: