GnuCash  4.8a-134-g214de30c7+
Public Member Functions | Static Public Member Functions
GncDateTime Class Reference

GnuCash DateTime class. More...

#include <gnc-datetime.hpp>

Public Member Functions

 GncDateTime ()
 Construct a GncDateTime representing the current time in the current timezone.
 
 GncDateTime (const time64 time)
 Construct a GncDateTime in the current timezone representing the timestamp as seconds from the POSIX epoch (1970-01-01T00:00:00UTC). More...
 
 GncDateTime (const struct tm tm)
 Construct a GncDateTime in the current timezone representing the standard struct tm provided. More...
 
 GncDateTime (const GncDate &date, DayPart part=DayPart::neutral)
 Construct a GncDateTime from a GncDate. More...
 
 GncDateTime (std::string str)
 Construct a GncDateTime. More...
 
void now ()
 Set the GncDateTime to the date and time indicated in the computer's clock.
 
 operator time64 () const
 Cast the GncDateTime to a time64, seconds from the POSIX epoch. More...
 
 operator struct tm () const
 Cast the GncDateTime to a struct tm. More...
 
long offset () const
 Obtain the UTC offset in seconds. More...
 
struct tm utc_tm () const
 Obtain a struct tm representing the time in UTC. More...
 
GncDate date () const
 Obtain the date from the time, as a GncDate, in the current timezone. More...
 
bool isnull (void)
 Test if the GncDateTime has a member pointer. More...
 
std::string format (const char *format) const
 Format the GncDateTime into a std::string. More...
 
std::string format_zulu (const char *format) const
 Format the GncDateTime into a std::string in GMT. More...
 
std::string format_iso8601 () const
 Format the GncDateTime into a gnucash-style iso8601 string in UTC. More...
 

Static Public Member Functions

static std::string timestamp ()
 Get an undelimited string representing the current date and time. More...
 

Detailed Description

GnuCash DateTime class.

Represents local time in the current timezone. As with GncDate, the represented time is limited to the period between 1400 and 9999 CE.

Be careful when using times: A particular time is represented differently depending on the timezone, which can shift the displayed date. Accounting is generally not sensitive to the time of day, but is sensitive to the recorded day. Since GncDates are not timezone dependent they should be preferred for accounting entries.

Definition at line 66 of file gnc-datetime.hpp.

Constructor & Destructor Documentation

◆ GncDateTime() [1/4]

GncDateTime::GncDateTime ( const time64  time)

Construct a GncDateTime in the current timezone representing the timestamp as seconds from the POSIX epoch (1970-01-01T00:00:00UTC).

Parameters
timeSeconds from the POSIX epoch.
Exceptions
std::invalid_argumentif the year is outside the constraints.

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

638  :
639  m_impl(new GncDateTimeImpl(time)) {}

◆ GncDateTime() [2/4]

GncDateTime::GncDateTime ( const struct tm  tm)

Construct a GncDateTime in the current timezone representing the standard struct tm provided.

Parameters
tmA C-standard struct tm representing the date and time. Note that the timezone and offset are ignored on those systems which include them in struct tm.
Exceptions
std::invalid_argumentif the year is outside the constraints.

◆ GncDateTime() [3/4]

GncDateTime::GncDateTime ( const GncDate date,
DayPart  part = DayPart::neutral 
)

Construct a GncDateTime from a GncDate.

As a GncDate doesn't contain time information, the time will be set depending on the second parameter to start of day, neutral or end of day.

Parameters
dateA GncDate representing a date.
partAn optinoal DayPart indicating which time to use in the conversion. This can be "DayPart::start" for start of day (00:00 local time), "DayPart::neutral" for a neutral time (10:59 UTC, chosen to have the least chance of date changes when crossing timezone borders), "DayPart::end" for end of day (23:59 local time). If omitted part defaults to DayPart::neutral. Note the different timezone used for DayPart::neutral compared to the other two options!
Exceptions
std::invalid_argumentif the year is outside the constraints.

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

646  :
647  m_impl(new GncDateTimeImpl(*(date.m_impl), part)) {}
GncDate date() const
Obtain the date from the time, as a GncDate, in the current timezone.

◆ GncDateTime() [4/4]

GncDateTime::GncDateTime ( std::string  str)

Construct a GncDateTime.

Parameters
strA string representing the date and time in some recognizable format. Note that if a timezone is not specified the default is UTC, not the local one.
Exceptions
std::invalid_argumentif the year is outside the constraints.

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

642  :
643  m_impl(new GncDateTimeImpl(str)) {}

Member Function Documentation

◆ date()

GncDate GncDateTime::date ( ) const

Obtain the date from the time, as a GncDate, in the current timezone.

Returns
GncDate represented by the GncDateTime.

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

679 {
680  return GncDate(m_impl->date());
681 }
GnuCash Date class.

◆ format()

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

Format the GncDateTime into a std::string.

Parameters
formatA cstr describing the way the date and time are presented. Code letters preceded with % stand in for arguments; most are the same as described in strftime(3), but there are a few differences. Consult the boost::date_time documentation.
Returns
a std::string containing a representation of the date and time in the locale's time zone according to the format.

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

685 {
686  return m_impl->format(format);
687 }
std::string format(const char *format) const
Format the GncDateTime into a std::string.

◆ format_iso8601()

std::string GncDateTime::format_iso8601 ( ) const

Format the GncDateTime into a gnucash-style iso8601 string in UTC.

Returns
a std::string in the format YYYY-MM-DD HH:MM:SS.

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

697 {
698  return m_impl->format_iso8601();
699 }

◆ format_zulu()

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

Format the GncDateTime into a std::string in GMT.

Parameters
formatA cstr describing the way the date and time are presented. Code letters preceded with % stand in for arguments; most are the same as described in strftime(3), but there are a few differences. Consult the boost::date_time documentation.
Returns
a std::string containing a representation of the date and time in GMT (timezone Z) according to the format.

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

691 {
692  return m_impl->format_zulu(format);
693 }
std::string format(const char *format) const
Format the GncDateTime into a std::string.

◆ isnull()

bool GncDateTime::isnull ( void  )
inline

Test if the GncDateTime has a member pointer.

Testing only.

Definition at line 132 of file gnc-datetime.hpp.

132 { return m_impl == nullptr; }

◆ offset()

long GncDateTime::offset ( ) const

Obtain the UTC offset in seconds.

Returns
seconds difference between this local time and UTC. West is negative.

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

667 {
668  return m_impl->offset();
669 }

◆ operator struct tm()

GncDateTime::operator struct tm ( ) const
explicit

Cast the GncDateTime to a struct tm.

Timezone field isn't filled.

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

661 {
662  return m_impl->operator struct tm();
663 }

◆ operator time64()

GncDateTime::operator time64 ( ) const
explicit

Cast the GncDateTime to a time64, seconds from the POSIX epoch.

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

656 {
657  return m_impl->operator time64();
658 }
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:93

◆ timestamp()

std::string GncDateTime::timestamp ( )
static

Get an undelimited string representing the current date and time.

Returns
a std::string in the format YYYYMMDDHHMMSS.

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

703 {
704  return GncDateTimeImpl::timestamp();
705 }

◆ utc_tm()

struct tm GncDateTime::utc_tm ( ) const

Obtain a struct tm representing the time in UTC.

Returns
struct tm

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

673 {
674  return m_impl->utc_tm();
675 }

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