GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions
GncDateTimeImpl Class Reference

Public Member Functions

 GncDateTimeImpl (const time64 time)
 
 GncDateTimeImpl (const struct tm tm)
 
 GncDateTimeImpl (const GncDateImpl &date, DayPart part=DayPart::neutral)
 
 GncDateTimeImpl (const std::string &str)
 
 GncDateTimeImpl (const char *str)
 
 GncDateTimeImpl (PTime &&pt)
 
 GncDateTimeImpl (LDT &&ldt)
 
 operator time64 () const
 
 operator struct tm () const
 
void now ()
 
long offset () const
 
struct tm utc_tm () const
 
std::unique_ptr< GncDateImpldate () const
 
std::string format (const char *format) const
 
std::string format_zulu (const char *format) const
 
std::string format_iso8601 () const
 

Static Public Member Functions

static std::string timestamp ()
 

Detailed Description

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

Constructor & Destructor Documentation

◆ GncDateTimeImpl() [1/8]

GncDateTimeImpl::GncDateTimeImpl ( )
inline

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

311: m_time(LDT_from_unix_local(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count())) {}

◆ GncDateTimeImpl() [2/8]

GncDateTimeImpl::GncDateTimeImpl ( const time64  time)
inline

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

312: m_time(LDT_from_unix_local(time)) {}

◆ GncDateTimeImpl() [3/8]

GncDateTimeImpl::GncDateTimeImpl ( const struct tm  tm)
inline

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

313: m_time(LDT_from_struct_tm(tm)) {}

◆ GncDateTimeImpl() [4/8]

GncDateTimeImpl::GncDateTimeImpl ( const GncDateImpl date,
DayPart  part = DayPart::neutral 
)

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

366 :
367 m_time{LDT_from_date_daypart(date.m_greg, part,
368 tzp->get(date.m_greg.year()))} {}

◆ GncDateTimeImpl() [5/8]

GncDateTimeImpl::GncDateTimeImpl ( const std::string &  str)
inline

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

315: GncDateTimeImpl (str.c_str()) {};

◆ GncDateTimeImpl() [6/8]

GncDateTimeImpl::GncDateTimeImpl ( const char *  str)

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

432 :
433 m_time(unix_epoch, utc_zone)
434{
435 if (!str || !str[0]) return;
436 TZ_Ptr tzptr;
437 try
438 {
439 if (auto res = fast_iso8601_utc_parse (str))
440 {
441 m_time = LDT_from_date_time(res->date(), res->time_of_day(), utc_zone);
442 return;
443 }
444 static const boost::regex delim_iso("^(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}(?:\\.\\d{0,9})?)\\s*([+-]\\d{2}(?::?\\d{2})?)?$");
445 static const boost::regex non_delim("^(\\d{14}(?:\\.\\d{0,9})?)\\s*([+-]\\d{2}\\s*(:?\\d{2})?)?$");
446 PTime pdt;
447 boost::cmatch sm;
448 if (regex_match(str, sm, non_delim))
449 {
450 std::string time_str(sm[1]);
451 time_str.insert(8, "T");
452 pdt = boost::posix_time::from_iso_string(time_str);
453 }
454 else if (regex_match(str, sm, delim_iso))
455 {
456 pdt = boost::posix_time::time_from_string(sm[1]);
457 }
458 else
459 {
460 throw(std::invalid_argument("The date string was not formatted in a way that GncDateTime(const char*) knows how to parse."));
461 }
462 std::string tzstr("");
463 if (sm[2].matched)
464 tzstr += sm[2];
465 tzptr = tz_from_string(tzstr);
466 m_time = LDT_from_date_time(pdt.date(), pdt.time_of_day(), tzptr);
467 }
468 catch(boost::gregorian::bad_year&)
469 {
470 throw(std::invalid_argument("The date string was outside of the supported year range."));
471 }
472 catch(std::out_of_range&)
473 {
474 throw(std::invalid_argument("The date string was outside of the supported year range."));
475 }
476 /* Bug 767824: A GLib bug in parsing the UTC timezone on Windows may have
477 * created a bogus timezone of a random number of minutes. Since there are
478 * no fractional-hour timezones around the prime meridian we can safely
479 * check for this in files by resetting to UTC if there's a
480 * less-than-an-hour offset.
481 */
482 auto offset = tzptr->base_utc_offset().seconds();
483 if (offset != 0 && std::abs(offset) < 3600)
484 m_time = m_time.local_time_in(utc_zone);
485}

◆ GncDateTimeImpl() [7/8]

GncDateTimeImpl::GncDateTimeImpl ( PTime &&  pt)
inline

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

317: m_time(pt, tzp->get(pt.date().year())) {}

◆ GncDateTimeImpl() [8/8]

GncDateTimeImpl::GncDateTimeImpl ( LDT &&  ldt)
inline

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

318: m_time(ldt) {}

Member Function Documentation

◆ date()

std::unique_ptr< GncDateImpl > GncDateTimeImpl::date ( ) const

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

513{
514 return std::unique_ptr<GncDateImpl>(new GncDateImpl(m_time.local_time().date()));
515}
Private implementation of GncDate.

◆ format()

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

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

591{
592#ifdef __MINGW32__
593 auto tz = m_time.zone();
594 auto tm = static_cast<struct tm>(*this);
595 auto sformat = win_format_tz_abbrev(format, tz, tm.tm_isdst);
596 sformat = win_format_tz_name(sformat, tz, tm.tm_isdst);
597 sformat = win_format_tz_posix(sformat, tz);
598 return win_date_format(sformat, tm);
599#else
600 using Facet = boost::local_time::local_time_facet;
601 auto output_facet(new Facet(normalize_format(format).c_str()));
602 std::stringstream ss;
603 ss.imbue(std::locale(gnc_get_locale(), output_facet));
604 ss << m_time;
605 return ss.str();
606#endif
607}

◆ format_iso8601()

std::string GncDateTimeImpl::format_iso8601 ( ) const

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

632{
633 auto str = boost::posix_time::to_iso_extended_string(m_time.utc_time());
634 str[10] = ' ';
635 return str.substr(0, 19);
636}

◆ format_zulu()

std::string GncDateTimeImpl::format_zulu ( const char *  format) const

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

611{
612#ifdef __MINGW32__
613 auto tz = utc_zone;
614 auto tm = static_cast<struct tm>(*this);
615 auto sformat = win_format_tz_abbrev(format, tz, tm.tm_isdst);
616 sformat = win_format_tz_name(sformat, tz, tm.tm_isdst);
617 sformat = win_format_tz_posix(sformat, tz);
618 return win_date_format(sformat, utc_tm());
619#else
620 using Facet = boost::local_time::local_time_facet;
621 auto zulu_time = LDT{m_time.utc_time(), utc_zone};
622 auto output_facet(new Facet(normalize_format(format).c_str()));
623 std::stringstream ss;
624 ss.imbue(std::locale(gnc_get_locale(), output_facet));
625 ss << zulu_time;
626 return ss.str();
627#endif
628}

◆ now()

void GncDateTimeImpl::now ( )
inline

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

322{ m_time = boost::local_time::local_sec_clock::local_time(tzp->get(boost::gregorian::day_clock::local_day().year())); }

◆ offset()

long GncDateTimeImpl::offset ( ) const

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

506{
507 auto offset = m_time.local_time() - m_time.utc_time();
508 return offset.total_seconds();
509}

◆ operator struct tm()

GncDateTimeImpl::operator struct tm ( ) const

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

496{
497 struct tm time = to_tm(m_time);
498#if HAVE_STRUCT_TM_GMTOFF
499 time.tm_gmtoff = offset();
500#endif
501 return time;
502}

◆ operator time64()

GncDateTimeImpl::operator time64 ( ) const

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

488{
489 auto duration = m_time.utc_time() - unix_epoch;
490 auto secs = duration.ticks();
491 secs /= ticks_per_second;
492 return secs;
493}

◆ timestamp()

std::string GncDateTimeImpl::timestamp ( )
static

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

640{
641 GncDateTimeImpl gdt;
642 auto str = boost::posix_time::to_iso_string(gdt.m_time.local_time());
643 return str.substr(0, 8) + str.substr(9, 15);
644}

◆ utc_tm()

struct tm GncDateTimeImpl::utc_tm ( ) const
inline

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

324{ return to_tm(m_time.utc_time()); }

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