GnuCash  5.6-150-g038405b370+
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 (const std::string &str)
 Construct a GncDateTime. More...
 
 GncDateTime (const char *str)
 
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 70 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 767 of file gnc-datetime.cpp.

767  :
768  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 777 of file gnc-datetime.cpp.

777  :
778  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 ( const 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 771 of file gnc-datetime.cpp.

771  :
772  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 809 of file gnc-datetime.cpp.

810 {
811  return GncDate(m_impl->date());
812 }
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 815 of file gnc-datetime.cpp.

816 {
817  return m_impl->format(format);
818 }
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 827 of file gnc-datetime.cpp.

828 {
829  return m_impl->format_iso8601();
830 }

◆ 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 821 of file gnc-datetime.cpp.

822 {
823  return m_impl->format_zulu(format);
824 }
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 137 of file gnc-datetime.hpp.

137 { 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 797 of file gnc-datetime.cpp.

798 {
799  return m_impl->offset();
800 }

◆ operator struct tm()

GncDateTime::operator struct tm ( ) const
explicit

Cast the GncDateTime to a struct tm.

Timezone field isn't filled.

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

792 {
793  return m_impl->operator struct tm();
794 }

◆ operator time64()

GncDateTime::operator time64 ( ) const
explicit

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

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

787 {
788  return m_impl->operator time64();
789 }
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87

◆ 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 833 of file gnc-datetime.cpp.

834 {
835  return GncDateTimeImpl::timestamp();
836 }

◆ utc_tm()

struct tm GncDateTime::utc_tm ( ) const

Obtain a struct tm representing the time in UTC.

Returns
struct tm

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

804 {
805  return m_impl->utc_tm();
806 }

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