GnuCash  5.6-150-g038405b370+
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables

Date and Time handling routines. More...

#include <glib-object.h>
#include <time.h>

Go to the source code of this file.

Data Structures

struct  Time64
 

Macros

#define MAX_DATE_LENGTH   34
 The maximum length of a string created by the date printers.
 
#define QOF_UTC_DATE_FORMAT   "%Y-%m-%dT%H:%M:%SZ"
 Constants. More...
 
#define DATE_FORMAT_FIRST   QOF_DATE_FORMAT_US
 
#define DATE_FORMAT_LAST   QOF_DATE_FORMAT_UTC
 

Typedefs

typedef gint64 time64
 Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux, support 64-bit time_t even on 32-bit architectures. More...
 

Enumerations

enum  QofDateFormat {
  QOF_DATE_FORMAT_US, QOF_DATE_FORMAT_UK, QOF_DATE_FORMAT_CE, QOF_DATE_FORMAT_ISO,
  QOF_DATE_FORMAT_LOCALE, QOF_DATE_FORMAT_UTC, QOF_DATE_FORMAT_CUSTOM, QOF_DATE_FORMAT_UNSET
}
 Enum for determining a date format. More...
 
enum  QofDateCompletion { QOF_DATE_COMPLETION_THISYEAR, QOF_DATE_COMPLETION_SLIDING }
 Enum for date completion modes (for dates entered without year) More...
 
enum  GNCDateMonthFormat { GNCDATE_MONTH_NUMBER, GNCDATE_MONTH_ABBREV, GNCDATE_MONTH_NAME }
 This is how to format the month, as a number, an abbreviated string, or the full name.
 

Functions

struct tm * gnc_localtime (const time64 *secs)
 fill out a time struct from a 64-bit time value. More...
 
struct tm * gnc_localtime_r (const time64 *secs, struct tm *time)
 fill out a time struct from a 64-bit time value adjusted for the current time zone. More...
 
struct tm * gnc_gmtime (const time64 *secs)
 fill out a time struct from a 64-bit time value More...
 
gint gnc_start_of_week (void)
 returns an integer corresponding to locale start of week More...
 
time64 gnc_mktime (struct tm *time)
 calculate seconds from the epoch given a time struct More...
 
time64 gnc_timegm (struct tm *time)
 calculate seconds from the epoch given a time struct More...
 
gchar * gnc_ctime (const time64 *secs)
 Return a string representation of a date from a 64-bit time value. More...
 
time64 gnc_time (time64 *tbuf)
 get the current time More...
 
gdouble gnc_difftime (const time64 secs1, const time64 secs2)
 Find the difference in seconds between two time values (deprecated) More...
 
void gnc_tm_free (struct tm *time)
 free a struct tm* created with gnc_localtime() or gnc_gmtime() More...
 
time64 time64CanonicalDayTime (time64 t)
 convert a time64 on a certain day (localtime) to the time64 representing midday on that day. More...
 
time64 gdate_to_time64 (GDate d)
 Turns a GDate into a time64, returning the first second of the day.
 
time64 gnc_dmy2time64 (gint day, gint month, gint year)
 Convert a day, month, and year to a time64, returning the first second of the day.
 
time64 gnc_dmy2time64_neutral (gint day, gint month, gint year)
 Converts a day, month, and year to a time64 representing 11:00:00 UTC 11:00:00 UTC falls on the same time in almost all timezones, the exceptions being the +13, +14, and -12 timezones used by countries along the International Date Line. More...
 
time64 gnc_dmy2time64_end (gint day, gint month, gint year)
 Same as gnc_dmy2time64, but last second of the day.
 
time64 gnc_iso8601_to_time64_gmt (const gchar *)
 The gnc_iso8601_to_time64_gmt() routine converts an ISO-8601 style date/time string to time64. More...
 
gchar * gnc_time64_to_iso8601_buff (time64, char *buff)
 The gnc_time64_to_iso8601_buff() routine takes the input UTC time64 value and prints it as an ISO-8601 style string. More...
 
void qof_date_completion_set (QofDateCompletion dc, int backmonths)
 The qof_date_completion_set() routing sets the date completion method to one of QOF_DATE_COMPLETION_THISYEAR (for completing the year to the current calendar year) or QOF_DATE_COMPLETION_SLIDING (for using a sliding 12-month window). More...
 
gchar dateSeparator (void)
 dateSeparator Return the field separator for the current date format More...
 
String / DateFormat conversion.
const gchar * gnc_date_dateformat_to_string (QofDateFormat format)
 The string->value versions return FALSE on success and TRUE on failure.
 
gboolean gnc_date_string_to_dateformat (const gchar *format_string, QofDateFormat *format)
 Converts the date format to a printable string. More...
 
const gchar * gnc_date_monthformat_to_string (GNCDateMonthFormat format)
 
gboolean gnc_date_string_to_monthformat (const gchar *format_string, GNCDateMonthFormat *format)
 Converts the month format to a printable string. More...
 
char * gnc_print_time64 (time64 time, const char *format)
 print a time64 as a date string per format More...
 
GDate time64 setters
GDate * gnc_g_date_new_today (void)
 Returns a newly allocated date of the current clock time, taken from time(2). More...
 
void gnc_gdate_set_today (GDate *gd)
 Set a GDate to the current day. More...
 
void gnc_gdate_set_time64 (GDate *gd, time64 time)
 Set a GDate to a time64. More...
 
QofDateFormat functions
QofDateFormat qof_date_format_get (void)
 The qof_date_format_get routine returns the date format that the date printing will use when printing a date, and the scanning routines will assume when parsing a date. More...
 
void qof_date_format_set (QofDateFormat df)
 The qof_date_format_set() routine sets date format to one of US, UK, CE, OR ISO. More...
 
const gchar * qof_date_format_get_string (QofDateFormat df)
 This function returns a strftime formatting string for printing an all numeric date (e.g. More...
 
const gchar * qof_date_text_format_get_string (QofDateFormat df)
 This function returns a strftime formatting string for printing a date using words and numbers (e.g. More...
 
Date Printing/Scanning functions
gsize qof_strftime (gchar *buf, gsize max, const gchar *format, const struct tm *tm)
 qof_strftime calls qof_format_time to print a given time and afterwards tries to put the result into a buffer of fixed size. More...
 
size_t qof_print_date_dmy_buff (gchar *buff, size_t buflen, int day, int month, int year)
 qof_print_date_dmy_buff Convert a date as day / month / year integers into a localized string representation More...
 
size_t qof_print_date_buff (char *buff, size_t buflen, time64 secs)
 Convenience: calls through to qof_print_date_dmy_buff(). More...
 
size_t qof_print_gdate (char *buf, size_t bufflen, const GDate *gd)
 Convenience; calls through to qof_print_date_dmy_buff(). More...
 
char * qof_print_date (time64 secs)
 Convenience; calls through to qof_print_date_dmy_buff(). More...
 
size_t qof_print_date_time_buff (char *buff, size_t len, time64 secs)
 Returns the number of bytes printed.
 
gboolean qof_scan_date (const char *buff, int *day, int *month, int *year)
 qof_scan_date Convert a string into day / month / year integers according to the current dateFormat value. More...
 
Date Start/End Adjustment routines

Given a time value, adjust it to be the beginning or end of that day.

void gnc_tm_set_day_neutral (struct tm *tm)
 The gnc_tm_set_day_neutral() inline routine will set the appropriate fields in the struct tm to indicate 10:59am of that day.
 
time64 gnc_time64_get_day_start (time64 time_val)
 The gnc_time64_get_day_start() routine will take the given time in seconds and adjust it to the first second of that day. More...
 
time64 gnc_time64_get_day_neutral (time64 time_val)
 The gnc_time64_get_day_neutral() routine will take the given time in seconds and adjust it to 10:59:00Z of that day. More...
 
time64 gnc_time64_get_day_end (time64 time_val)
 The gnc_time64_get_day_end() routine will take the given time in seconds and adjust it to the last second of that day. More...
 
int gnc_date_get_last_mday (int month, int year)
 Get the numerical last date of the month. More...
 
GDate hash table support
gint gnc_gdate_equal (gconstpointer gda, gconstpointer gdb)
 Compares two GDate*'s for equality; useful for using GDate*'s as GHashTable keys. More...
 
guint gnc_gdate_hash (gconstpointer gd)
 Provides a "hash" of a GDate* value; useful for using GDate*'s as GHashTable keys. More...
 
GDate to time64 conversions
GDate time64_to_gdate (time64 t)
 Returns the GDate in which the time64 occurs. More...
 
time64 gnc_time64_get_day_start_gdate (const GDate *date)
 The gnc_time64_get_day_start() routine will take the given time in GLib GDate format and adjust it to the first second of that day.
 
time64 gnc_time64_get_day_end_gdate (const GDate *date)
 The gnc_time64_get_day_end() routine will take the given time in GLib GDate format and adjust it to the last second of that day.
 
Date Manipulation
void gnc_gdate_set_month_start (GDate *date)
 This function modifies a GDate to set it to the first day of the month in which it falls. More...
 
void gnc_gdate_set_month_end (GDate *date)
 This function modifies a GDate to set it to the last day of the month in which it falls. More...
 
void gnc_gdate_set_prev_month_start (GDate *date)
 This function modifies a GDate to set it to the first day of the month prior to the one in which it falls. More...
 
void gnc_gdate_set_prev_month_end (GDate *date)
 This function modifies a GDate to set it to the last day of the month prior to the one in which it falls. More...
 
void gnc_gdate_set_quarter_start (GDate *date)
 This function modifies a GDate to set it to the first day of the quarter in which it falls. More...
 
void gnc_gdate_set_quarter_end (GDate *date)
 This function modifies a GDate to set it to the last day of the quarter in which it falls. More...
 
void gnc_gdate_set_prev_quarter_start (GDate *date)
 This function modifies a GDate to set it to the first day of the quarter prior to the one in which it falls. More...
 
void gnc_gdate_set_prev_quarter_end (GDate *date)
 This function modifies a GDate to set it to the last day of the quarter prior to the one in which it falls. More...
 
void gnc_gdate_set_year_start (GDate *date)
 This function modifies a GDate to set it to the first day of the year in which it falls. More...
 
void gnc_gdate_set_year_end (GDate *date)
 This function modifies a GDate to set it to the last day of the year in which it falls. More...
 
void gnc_gdate_set_prev_year_start (GDate *date)
 This function modifies a GDate to set it to the first day of the year prior to the one in which it falls. More...
 
void gnc_gdate_set_prev_year_end (GDate *date)
 This function modifies a GDate to set it to the last day of the year prior to the one in which it falls. More...
 
void gnc_gdate_set_fiscal_year_start (GDate *date, const GDate *year_end)
 This function modifies a GDate to set it to the first day of the fiscal year in which it falls. More...
 
void gnc_gdate_set_fiscal_year_end (GDate *date, const GDate *year_end)
 This function modifies a GDate to set it to the last day of the fiscal year in which it falls. More...
 
void gnc_gdate_set_prev_fiscal_year_start (GDate *date, const GDate *year_end)
 This function modifies a GDate to set it to the first day of the fiscal year prior to the one in which it falls. More...
 
void gnc_gdate_set_prev_fiscal_year_end (GDate *date, const GDate *year_end)
 This function modifies a GDate to set it to the last day of the fiscal year prior to the one in which it falls. More...
 

Variables

const char * gnc_default_strftime_date_format
 The default date format for use with strftime. More...
 

GValue

#define GNC_TYPE_TIME64   (time64_get_type ())
 
GType time64_get_type (void)
 

Today's Date

#define MIN_BUF_LEN   10
 
void gnc_tm_get_today_start (struct tm *tm)
 The gnc_tm_get_today_start() routine takes a pointer to a struct tm and fills it in with the first second of the today. More...
 
void gnc_tm_get_today_neutral (struct tm *tm)
 The gnc_tm_get_today_start() routine takes a pointer to a struct tm and fills it in with the timezone neutral time (10:59:00Z). More...
 
void gnc_tm_get_today_end (struct tm *tm)
 The gnc_tm_get_today_end() routine takes a pointer to a struct tm and fills it in with the last second of the today. More...
 
time64 gnc_time64_get_today_start (void)
 The gnc_time64_get_today_start() routine returns a time64 value corresponding to the first second of today. More...
 
time64 gnc_time64_get_today_end (void)
 The gnc_time64_get_today_end() routine returns a time64 value corresponding to the last second of today. More...
 
char * gnc_date_timestamp (void)
 Make a timestamp in YYYYMMDDHHMMSS format. More...
 
void gnc_dow_abbrev (gchar *buf, int buf_len, int dow)
 Localized DOW abbreviation. More...
 

Detailed Description

Date and Time handling routines.

Definition in file gnc-date.h.

Function Documentation

◆ dateSeparator()

gchar dateSeparator ( void  )

dateSeparator Return the field separator for the current date format

Args: none

Return: date character

Globals: global dateFormat value

Definition at line 932 of file gnc-date.cpp.

933 {
934  static char locale_separator = '\0';
935 
936  switch (dateFormat)
937  {
938  case QOF_DATE_FORMAT_CE:
939  return '.';
940  case QOF_DATE_FORMAT_ISO:
941  case QOF_DATE_FORMAT_UTC:
942  return '-';
943  case QOF_DATE_FORMAT_US:
944  case QOF_DATE_FORMAT_UK:
945  default:
946  return '/';
948  if (locale_separator != '\0')
949  return locale_separator;
950  else
951  {
952  /* Make a guess */
953  gchar string[256];
954  struct tm tm;
955  time64 secs;
956  gchar *s;
957 
958  secs = gnc_time (nullptr);
959  gnc_localtime_r(&secs, &tm);
960  auto normalized_fmt =
961  normalize_format(qof_date_format_get_string(dateFormat));
962  qof_strftime(string, sizeof(string), normalized_fmt.c_str(), &tm);
963 
964  for (s = string; *s != '\0'; s++)
965  if (!isdigit(*s))
966  return (locale_separator = *s);
967  }
968  break;
969  }
970  return '\0';
971 }
ISO: yyyy-mm-dd.
Definition: gnc-date.h:127
gsize qof_strftime(gchar *buf, gsize max, const gchar *format, const struct tm *tm)
qof_strftime calls qof_format_time to print a given time and afterwards tries to put the result into ...
Definition: gnc-date.cpp:1062
Continental Europe: dd.mm.yyyy.
Definition: gnc-date.h:126
struct tm * gnc_localtime_r(const time64 *secs, struct tm *time)
fill out a time struct from a 64-bit time value adjusted for the current time zone.
Definition: gnc-date.cpp:113
UTC: 2004-12-12T23:39:11Z.
Definition: gnc-date.h:129
time64 gnc_time(time64 *tbuf)
get the current time
Definition: gnc-date.cpp:260
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87
Take from locale information.
Definition: gnc-date.h:128
Britain: dd/mm/yyyy.
Definition: gnc-date.h:125
const gchar * qof_date_format_get_string(QofDateFormat df)
This function returns a strftime formatting string for printing an all numeric date (e...
Definition: gnc-date.cpp:500
United states: mm/dd/yyyy.
Definition: gnc-date.h:124

◆ gnc_date_get_last_mday()

int gnc_date_get_last_mday ( int  month,
int  year 
)

Get the numerical last date of the month.

(28, 29, 30, 31)

Definition at line 411 of file gnc-date.cpp.

412 {
413  static int last_day_of_month[12] =
414  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
415 
416  g_assert(month >= 0 && month < 12);
417 
418  // To be a leap year, the year number must be divisible by four,
419  // except for end-of-century years, which must be divisible by 400.
420 
421  return last_day_of_month[month] +
422  (month == 1 && year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0) ?
423  1 : 0);
424 }

◆ gnc_date_timestamp()

char* gnc_date_timestamp ( void  )

Make a timestamp in YYYYMMDDHHMMSS format.

Returns
A pointer to the generated string.
Note
The caller owns this buffer and must g_free it when done.

Definition at line 1107 of file gnc-date.cpp.

1108 {
1109  auto timestamp = GncDateTime::timestamp();
1110  return g_strdup(timestamp.c_str());
1111 }
static std::string timestamp()
Get an undelimited string representing the current date and time.

◆ gnc_dow_abbrev()

void gnc_dow_abbrev ( gchar *  buf,
int  buf_len,
int  dow 
)

Localized DOW abbreviation.

Parameters
buf_lenat least MIN_BUF_LEN
dowstruct tm semantics: 0=sunday .. 6=saturday

Definition at line 1371 of file gnc-date.cpp.

1372 {
1373  struct tm my_tm;
1374  int i;
1375 
1376  memset(buf, 0, buf_len);
1377  memset(&my_tm, 0, sizeof(struct tm));
1378  my_tm.tm_wday = dow;
1379  i = qof_strftime(buf, buf_len, "%a", &my_tm);
1380  buf[i] = 0;
1381 }
gsize qof_strftime(gchar *buf, gsize max, const gchar *format, const struct tm *tm)
qof_strftime calls qof_format_time to print a given time and afterwards tries to put the result into ...
Definition: gnc-date.cpp:1062

◆ gnc_gdate_equal()

gint gnc_gdate_equal ( gconstpointer  gda,
gconstpointer  gdb 
)

Compares two GDate*'s for equality; useful for using GDate*'s as GHashTable keys.

Definition at line 1407 of file gnc-date.cpp.

1408 {
1409  return (g_date_compare( (GDate*)gda, (GDate*)gdb ) == 0 ? TRUE : FALSE);
1410 }

◆ gnc_gdate_hash()

guint gnc_gdate_hash ( gconstpointer  gd)

Provides a "hash" of a GDate* value; useful for using GDate*'s as GHashTable keys.

Definition at line 1413 of file gnc-date.cpp.

1414 {
1415  gint val = (g_date_get_year( (GDate*)gd ) * 10000)
1416  + (g_date_get_month( (GDate*)gd ) * 100)
1417  + g_date_get_day( (GDate*)gd );
1418  return g_int_hash( &val );
1419 }

◆ gnc_gdate_set_fiscal_year_end()

void gnc_gdate_set_fiscal_year_end ( GDate *  date,
const GDate *  year_end 
)

This function modifies a GDate to set it to the last day of the fiscal year in which it falls.

For example, if this function is called with a date of 2003-09-24 and a fiscal year ending July 31st, the date will be modified to 2004-07-31.

Parameters
dateThe GDate to modify.
year_endA GDate containing the last month and day of the fiscal year. The year field of this argument is ignored.

Definition at line 1610 of file gnc-date.cpp.

1612 {
1613  GDate temp;
1614  gboolean new_fy;
1615 
1616  g_return_if_fail(date);
1617  g_return_if_fail(fy_end);
1618 
1619  /* Compute the FY end that occurred this CY */
1620  temp = *fy_end;
1621  g_date_set_year(&temp, g_date_get_year(date));
1622 
1623  /* Has it already passed? */
1624  new_fy = (g_date_compare(date, &temp) > 0);
1625 
1626  /* Set end date */
1627  *date = temp;
1628  if (new_fy)
1629  g_date_add_years(date, 1);
1630 }

◆ gnc_gdate_set_fiscal_year_start()

void gnc_gdate_set_fiscal_year_start ( GDate *  date,
const GDate *  year_end 
)

This function modifies a GDate to set it to the first day of the fiscal year in which it falls.

For example, if this function is called with a date of 2003-09-24 and a fiscal year ending July 31st, the date will be modified to 2003-08-01.

Parameters
dateThe GDate to modify.
year_endA GDate containing the last month and day of the fiscal year. The year field of this argument is ignored.

Definition at line 1586 of file gnc-date.cpp.

1588 {
1589  GDate temp;
1590  gboolean new_fy;
1591 
1592  g_return_if_fail(date);
1593  g_return_if_fail(fy_end);
1594 
1595  /* Compute the FY end that occurred this CY */
1596  temp = *fy_end;
1597  g_date_set_year(&temp, g_date_get_year(date));
1598 
1599  /* Has it already passed? */
1600  new_fy = (g_date_compare(date, &temp) > 0);
1601 
1602  /* Set start date */
1603  *date = temp;
1604  g_date_add_days(date, 1);
1605  if (!new_fy)
1606  g_date_subtract_years(date, 1);
1607 }

◆ gnc_gdate_set_month_end()

void gnc_gdate_set_month_end ( GDate *  date)

This function modifies a GDate to set it to the last day of the month in which it falls.

For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-09-30.

Parameters
dateThe GDate to modify.

This function modifies a GDate to set it to the last day of the month in which it falls.

This routine has no knowledge of how many days are in a month, whether its a leap year, etc. All that information is contained in the glib date functions.

Parameters
dateThe GDate to modify.

Definition at line 1473 of file gnc-date.cpp.

1474 {
1475  /* First set the start of next month. */
1476  g_date_set_day(date, 1);
1477  g_date_add_months(date, 1);
1478 
1479  /* Then back up one day */
1480  g_date_subtract_days(date, 1);
1481 }

◆ gnc_gdate_set_month_start()

void gnc_gdate_set_month_start ( GDate *  date)

This function modifies a GDate to set it to the first day of the month in which it falls.

For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-09-01.

Parameters
dateThe GDate to modify.

Definition at line 1460 of file gnc-date.cpp.

1461 {
1462  g_date_set_day(date, 1);
1463 }

◆ gnc_gdate_set_prev_fiscal_year_end()

void gnc_gdate_set_prev_fiscal_year_end ( GDate *  date,
const GDate *  year_end 
)

This function modifies a GDate to set it to the last day of the fiscal year prior to the one in which it falls.

For example, if this function is called with a date of 2003-09-24 and a fiscal year ending July 31st, the date will be modified to 2003-07-31.

Parameters
dateThe GDate to modify.
year_endA GDate containing the last month and day of the fiscal year. The year field of this argument is ignored.

Definition at line 1644 of file gnc-date.cpp.

1646 {
1647  g_return_if_fail(date);
1648  g_return_if_fail(fy_end);
1649 
1650  gnc_gdate_set_fiscal_year_end(date, fy_end);
1651  g_date_subtract_years(date, 1);
1652 }
void gnc_gdate_set_fiscal_year_end(GDate *date, const GDate *year_end)
This function modifies a GDate to set it to the last day of the fiscal year in which it falls...
Definition: gnc-date.cpp:1610

◆ gnc_gdate_set_prev_fiscal_year_start()

void gnc_gdate_set_prev_fiscal_year_start ( GDate *  date,
const GDate *  year_end 
)

This function modifies a GDate to set it to the first day of the fiscal year prior to the one in which it falls.

For example, if this function is called with a date of 2003-09-24 and a fiscal year ending July 31st, the date will be modified to 2002-08-01.

Parameters
dateThe GDate to modify.
year_endA GDate containing the last month and day of the fiscal year. The year field of this argument is ignored.

Definition at line 1633 of file gnc-date.cpp.

1635 {
1636  g_return_if_fail(date);
1637  g_return_if_fail(fy_end);
1638 
1639  gnc_gdate_set_fiscal_year_start(date, fy_end);
1640  g_date_subtract_years(date, 1);
1641 }
void gnc_gdate_set_fiscal_year_start(GDate *date, const GDate *year_end)
This function modifies a GDate to set it to the first day of the fiscal year in which it falls...
Definition: gnc-date.cpp:1586

◆ gnc_gdate_set_prev_month_end()

void gnc_gdate_set_prev_month_end ( GDate *  date)

This function modifies a GDate to set it to the last day of the month prior to the one in which it falls.

For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-08-31.

Parameters
dateThe GDate to modify.

This function modifies a GDate to set it to the last day of the month prior to the one in which it falls.

This routine has no knowledge of how many days are in a month, whether its a leap year, etc. All that information is contained in the glib date functions.

Parameters
dateThe GDate to modify.

Definition at line 1505 of file gnc-date.cpp.

1506 {
1507  /* This will correctly handle the varying month lengths */
1508  g_date_set_day(date, 1);
1509  g_date_subtract_days(date, 1);
1510 }

◆ gnc_gdate_set_prev_month_start()

void gnc_gdate_set_prev_month_start ( GDate *  date)

This function modifies a GDate to set it to the first day of the month prior to the one in which it falls.

For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-08-01.

Parameters
dateThe GDate to modify.

This function modifies a GDate to set it to the first day of the month prior to the one in which it falls.

This routine has no knowledge of how many days are in a month, whether its a leap year, etc. All that information is contained in the glib date functions.

Parameters
dateThe GDate to modify.

Definition at line 1491 of file gnc-date.cpp.

1492 {
1493  g_date_set_day(date, 1);
1494  g_date_subtract_months(date, 1);
1495 }

◆ gnc_gdate_set_prev_quarter_end()

void gnc_gdate_set_prev_quarter_end ( GDate *  date)

This function modifies a GDate to set it to the last day of the quarter prior to the one in which it falls.

For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-06-30.

Parameters
dateThe GDate to modify.

Definition at line 1547 of file gnc-date.cpp.

1548 {
1549  g_date_subtract_months(date, 3);
1551 }
void gnc_gdate_set_quarter_end(GDate *date)
This function modifies a GDate to set it to the last day of the quarter in which it falls...
Definition: gnc-date.cpp:1528

◆ gnc_gdate_set_prev_quarter_start()

void gnc_gdate_set_prev_quarter_start ( GDate *  date)

This function modifies a GDate to set it to the first day of the quarter prior to the one in which it falls.

For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-04-01.

Parameters
dateThe GDate to modify.

Definition at line 1540 of file gnc-date.cpp.

1541 {
1542  g_date_subtract_months(date, 3);
1544 }
void gnc_gdate_set_quarter_start(GDate *date)
This function modifies a GDate to set it to the first day of the quarter in which it falls...
Definition: gnc-date.cpp:1515

◆ gnc_gdate_set_prev_year_end()

void gnc_gdate_set_prev_year_end ( GDate *  date)

This function modifies a GDate to set it to the last day of the year prior to the one in which it falls.

For example, if this function is called with a date of 2003-09-24 the date will be modified to 2002-12-31.

Parameters
dateThe GDate to modify.

Definition at line 1577 of file gnc-date.cpp.

1578 {
1579  gnc_gdate_set_year_end(date);
1580  g_date_subtract_years(date, 1);
1581 }
void gnc_gdate_set_year_end(GDate *date)
This function modifies a GDate to set it to the last day of the year in which it falls.
Definition: gnc-date.cpp:1563

◆ gnc_gdate_set_prev_year_start()

void gnc_gdate_set_prev_year_start ( GDate *  date)

This function modifies a GDate to set it to the first day of the year prior to the one in which it falls.

For example, if this function is called with a date of 2003-09-24 the date will be modified to 2002-01-01.

Parameters
dateThe GDate to modify.

Definition at line 1570 of file gnc-date.cpp.

1571 {
1573  g_date_subtract_years(date, 1);
1574 }
void gnc_gdate_set_year_start(GDate *date)
This function modifies a GDate to set it to the first day of the year in which it falls...
Definition: gnc-date.cpp:1556

◆ gnc_gdate_set_quarter_end()

void gnc_gdate_set_quarter_end ( GDate *  date)

This function modifies a GDate to set it to the last day of the quarter in which it falls.

For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-09-30.

Parameters
dateThe GDate to modify.

Definition at line 1528 of file gnc-date.cpp.

1529 {
1530  const GDateMonth months[] = {G_DATE_MARCH, G_DATE_JUNE,
1531  G_DATE_SEPTEMBER, G_DATE_DECEMBER};
1532  const GDateDay days[] = {31, 30, 30, 31};
1533  int quarter = (g_date_get_month (date) - 1) / 3;
1534 
1535  g_date_set_month (date, months[quarter]);
1536  g_date_set_day (date, days[quarter]);
1537 }

◆ gnc_gdate_set_quarter_start()

void gnc_gdate_set_quarter_start ( GDate *  date)

This function modifies a GDate to set it to the first day of the quarter in which it falls.

For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-07-01.

Parameters
dateThe GDate to modify.

Definition at line 1515 of file gnc-date.cpp.

1516 {
1517  gint months;
1518 
1519  /* Set the date to the first day of the specified month. */
1520  g_date_set_day(date, 1);
1521 
1522  /* Back up 0-2 months */
1523  months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
1524  g_date_subtract_months(date, months);
1525 }

◆ gnc_gdate_set_year_end()

void gnc_gdate_set_year_end ( GDate *  date)

This function modifies a GDate to set it to the last day of the year in which it falls.

For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-12-31.

Parameters
dateThe GDate to modify.

Definition at line 1563 of file gnc-date.cpp.

1564 {
1565  g_date_set_month(date, G_DATE_DECEMBER);
1566  g_date_set_day(date, 31);
1567 }

◆ gnc_gdate_set_year_start()

void gnc_gdate_set_year_start ( GDate *  date)

This function modifies a GDate to set it to the first day of the year in which it falls.

For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-01-01.

Parameters
dateThe GDate to modify.

Definition at line 1556 of file gnc-date.cpp.

1557 {
1558  g_date_set_month(date, G_DATE_JANUARY);
1559  g_date_set_day(date, 1);
1560 }

◆ gnc_time64_get_day_end()

time64 gnc_time64_get_day_end ( time64  time_val)

The gnc_time64_get_day_end() routine will take the given time in seconds and adjust it to the last second of that day.

Definition at line 1322 of file gnc-date.cpp.

1323 {
1324  struct tm tm;
1325  time64 new_time;
1326 
1327  gnc_tm_get_day_end(&tm, time_val);
1328  new_time = gnc_mktime(&tm);
1329  return new_time;
1330 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:217
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87

◆ gnc_time64_get_day_neutral()

time64 gnc_time64_get_day_neutral ( time64  time_val)

The gnc_time64_get_day_neutral() routine will take the given time in seconds and adjust it to 10:59:00Z of that day.

Definition at line 1313 of file gnc-date.cpp.

1314 {
1315  struct tm tm;
1316  gnc_localtime_r(&time_val, &tm);
1317  return gnc_dmy2time64_internal(tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900,
1318  DayPart::neutral);
1319 }
struct tm * gnc_localtime_r(const time64 *secs, struct tm *time)
fill out a time struct from a 64-bit time value adjusted for the current time zone.
Definition: gnc-date.cpp:113

◆ gnc_time64_get_day_start()

time64 gnc_time64_get_day_start ( time64  time_val)

The gnc_time64_get_day_start() routine will take the given time in seconds and adjust it to the first second of that day.

Definition at line 1302 of file gnc-date.cpp.

1303 {
1304  struct tm tm;
1305  time64 new_time;
1306 
1307  gnc_tm_get_day_start(&tm, time_val);
1308  new_time = gnc_mktime(&tm);
1309  return new_time;
1310 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:217
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87

◆ gnc_time64_get_today_end()

time64 gnc_time64_get_today_end ( void  )

The gnc_time64_get_today_end() routine returns a time64 value corresponding to the last second of today.

Definition at line 1362 of file gnc-date.cpp.

1363 {
1364  struct tm tm;
1365 
1366  gnc_tm_get_day_end(&tm, gnc_time(nullptr));
1367  return gnc_mktime(&tm);
1368 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:217
time64 gnc_time(time64 *tbuf)
get the current time
Definition: gnc-date.cpp:260

◆ gnc_time64_get_today_start()

time64 gnc_time64_get_today_start ( void  )

The gnc_time64_get_today_start() routine returns a time64 value corresponding to the first second of today.

Definition at line 1353 of file gnc-date.cpp.

1354 {
1355  struct tm tm;
1356 
1357  gnc_tm_get_day_start(&tm, gnc_time(nullptr));
1358  return gnc_mktime(&tm);
1359 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:217
time64 gnc_time(time64 *tbuf)
get the current time
Definition: gnc-date.cpp:260

◆ gnc_tm_get_today_end()

void gnc_tm_get_today_end ( struct tm *  tm)

The gnc_tm_get_today_end() routine takes a pointer to a struct tm and fills it in with the last second of the today.

Definition at line 1347 of file gnc-date.cpp.

1348 {
1349  gnc_tm_get_day_end(tm, gnc_time(nullptr));
1350 }
time64 gnc_time(time64 *tbuf)
get the current time
Definition: gnc-date.cpp:260

◆ gnc_tm_get_today_neutral()

void gnc_tm_get_today_neutral ( struct tm *  tm)

The gnc_tm_get_today_start() routine takes a pointer to a struct tm and fills it in with the timezone neutral time (10:59:00Z).

Definition at line 1341 of file gnc-date.cpp.

1342 {
1343  gnc_tm_get_day_neutral(tm, gnc_time(nullptr));
1344 }
time64 gnc_time(time64 *tbuf)
get the current time
Definition: gnc-date.cpp:260

◆ gnc_tm_get_today_start()

void gnc_tm_get_today_start ( struct tm *  tm)

The gnc_tm_get_today_start() routine takes a pointer to a struct tm and fills it in with the first second of the today.

Definition at line 1335 of file gnc-date.cpp.

1336 {
1337  gnc_tm_get_day_start(tm, gnc_time(nullptr));
1338 }
time64 gnc_time(time64 *tbuf)
get the current time
Definition: gnc-date.cpp:260

◆ qof_date_completion_set()

void qof_date_completion_set ( QofDateCompletion  dc,
int  backmonths 
)

The qof_date_completion_set() routing sets the date completion method to one of QOF_DATE_COMPLETION_THISYEAR (for completing the year to the current calendar year) or QOF_DATE_COMPLETION_SLIDING (for using a sliding 12-month window).

The sliding window starts 'backmonth' months before the current month (0-11)

Definition at line 466 of file gnc-date.cpp.

467 {
468  if (dc == QOF_DATE_COMPLETION_THISYEAR ||
470  {
471  dateCompletion = dc;
472  }
473  else
474  {
475  /* hack alert - Use a neutral default. */
476  PERR("non-existent date completion set attempted. Setting current year completion as default");
477  dateCompletion = QOF_DATE_COMPLETION_THISYEAR;
478  }
479 
480  if (backmonths < 0)
481  {
482  backmonths = 0;
483  }
484  else if (backmonths > 11)
485  {
486  backmonths = 11;
487  }
488  dateCompletionBackMonths = backmonths;
489 
490  return;
491 }
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
use sliding 12-month window
Definition: gnc-date.h:141
use current year
Definition: gnc-date.h:140

◆ qof_date_format_get()

QofDateFormat qof_date_format_get ( void  )

The qof_date_format_get routine returns the date format that the date printing will use when printing a date, and the scanning routines will assume when parsing a date.

Returns
: the one of the enumerated date formats.

Definition at line 426 of file gnc-date.cpp.

427 {
428  return dateFormat;
429 }

◆ qof_date_format_get_string()

const gchar* qof_date_format_get_string ( QofDateFormat  df)

This function returns a strftime formatting string for printing an all numeric date (e.g.

2005-09-14). The string returned is based upon the location specified.

Parameters
dfThe date style (us, uk, iso, etc) that should be provided.
Returns
A formatting string that will print a date in the requested style

Definition at line 500 of file gnc-date.cpp.

501 {
502  switch (df)
503  {
504  case QOF_DATE_FORMAT_US:
505  return "%m/%d/%Y";
506  case QOF_DATE_FORMAT_UK:
507  return "%d/%m/%Y";
508  case QOF_DATE_FORMAT_CE:
509  return "%d.%m.%Y";
510  case QOF_DATE_FORMAT_UTC:
511  return "%Y-%m-%dT%H:%M:%SZ";
512  case QOF_DATE_FORMAT_ISO:
513  return "%Y-%m-%d";
514  case QOF_DATE_FORMAT_UNSET: // use global
515  return qof_date_format_get_string (dateFormat);
517  default:
518  break;
519  };
520  return GNC_D_FMT;
521 }
ISO: yyyy-mm-dd.
Definition: gnc-date.h:127
Continental Europe: dd.mm.yyyy.
Definition: gnc-date.h:126
No Fancy Date Format, use Global.
Definition: gnc-date.h:131
UTC: 2004-12-12T23:39:11Z.
Definition: gnc-date.h:129
Take from locale information.
Definition: gnc-date.h:128
Britain: dd/mm/yyyy.
Definition: gnc-date.h:125
const gchar * qof_date_format_get_string(QofDateFormat df)
This function returns a strftime formatting string for printing an all numeric date (e...
Definition: gnc-date.cpp:500
United states: mm/dd/yyyy.
Definition: gnc-date.h:124

◆ qof_date_format_set()

void qof_date_format_set ( QofDateFormat  df)

The qof_date_format_set() routine sets date format to one of US, UK, CE, OR ISO.

Checks to make sure it's a legal value. Args: QofDateFormat: enumeration indicating preferred format

Definition at line 431 of file gnc-date.cpp.

432 {
433 //avoid UB if df is out of range
434  auto dfi{static_cast<uint8_t>(df)};
435  if (dfi >= DATE_FORMAT_FIRST && dfi <= DATE_FORMAT_LAST)
436  {
437  prevQofDateFormat = dateFormat;
438  dateFormat = df;
439  }
440  else
441  {
442  /* hack alert - Use a neutral default. */
443  PERR("non-existent date format set attempted. Setting ISO default");
444  prevQofDateFormat = dateFormat;
445  dateFormat = QOF_DATE_FORMAT_ISO;
446  }
447 
448  return;
449 }
ISO: yyyy-mm-dd.
Definition: gnc-date.h:127
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244

◆ qof_date_text_format_get_string()

const gchar* qof_date_text_format_get_string ( QofDateFormat  df)

This function returns a strftime formatting string for printing a date using words and numbers (e.g.

2005-September-14). The string returned is based upon the location specified.

Parameters
dfThe date style (us, uk, iso, etc) that should be provided.
Returns
A formatting string that will print a date in the requested style

Definition at line 523 of file gnc-date.cpp.

524 {
525  switch (df)
526  {
527  case QOF_DATE_FORMAT_US:
528  return "%b %d, %Y";
529  case QOF_DATE_FORMAT_UK:
530  case QOF_DATE_FORMAT_CE:
531  return "%d %b %Y";
532  case QOF_DATE_FORMAT_UTC:
533  return "%Y-%m-%dT%H:%M:%SZ";
534  case QOF_DATE_FORMAT_ISO:
535  return "%Y-%b-%d";
536  case QOF_DATE_FORMAT_UNSET: // use global
537  return qof_date_text_format_get_string (dateFormat);
539  default:
540  break;
541  };
542  return GNC_D_FMT;
543 }
ISO: yyyy-mm-dd.
Definition: gnc-date.h:127
Continental Europe: dd.mm.yyyy.
Definition: gnc-date.h:126
No Fancy Date Format, use Global.
Definition: gnc-date.h:131
UTC: 2004-12-12T23:39:11Z.
Definition: gnc-date.h:129
const gchar * qof_date_text_format_get_string(QofDateFormat df)
This function returns a strftime formatting string for printing a date using words and numbers (e...
Definition: gnc-date.cpp:523
Take from locale information.
Definition: gnc-date.h:128
Britain: dd/mm/yyyy.
Definition: gnc-date.h:125
United states: mm/dd/yyyy.
Definition: gnc-date.h:124

◆ qof_print_date()

char* qof_print_date ( time64  secs)

Convenience; calls through to qof_print_date_dmy_buff().

Return: string, which should be freed when no longer needed.

Definition at line 608 of file gnc-date.cpp.

609 {
610  char buff[MAX_DATE_LENGTH + 1];
611  memset (buff, 0, sizeof (buff));
613  return g_strdup (buff);
614 }
#define MAX_DATE_LENGTH
The maximum length of a string created by the date printers.
Definition: gnc-date.h:108
size_t qof_print_date_buff(char *buff, size_t buflen, time64 secs)
Convenience: calls through to qof_print_date_dmy_buff().
Definition: gnc-date.cpp:572

◆ qof_print_date_buff()

size_t qof_print_date_buff ( char *  buff,
size_t  buflen,
time64  secs 
)

Convenience: calls through to qof_print_date_dmy_buff().

Definition at line 572 of file gnc-date.cpp.

573 {
574  if (!buff) return 0;
575 
576  try
577  {
578  GncDateTime gncdt(t);
579  std::string str = gncdt.format(qof_date_format_get_string(dateFormat));
580  strncpy(buff, str.c_str(), len);
581  if (str.length() >= len)
582  buff[len - 1] = '\0';
583  }
584  catch(std::logic_error& err)
585  {
586  PWARN("Error processing time64 %" PRId64 ": %s", t, err.what());
587  }
588  catch(std::runtime_error& err)
589  {
590  PWARN("Error processing time64 %" PRId64 ": %s", t, err.what());
591  }
592  return strlen(buff);
593 }
GnuCash DateTime class.
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
const gchar * qof_date_format_get_string(QofDateFormat df)
This function returns a strftime formatting string for printing an all numeric date (e...
Definition: gnc-date.cpp:500

◆ qof_print_date_dmy_buff()

size_t qof_print_date_dmy_buff ( gchar *  buff,
size_t  buflen,
int  day,
int  month,
int  year 
)

qof_print_date_dmy_buff Convert a date as day / month / year integers into a localized string representation

Args: buff - pointer to previously allocated character array; its size must be at lease MAX_DATE_LENTH bytes. len - length of the buffer, in bytes. day - day of the month as 1 ... 31 month - month of the year as 1 ... 12 year - year (4-digit)

Returns: number of characters printed

Globals: global dateFormat value

◆ qof_print_gdate()

size_t qof_print_gdate ( char *  buf,
size_t  bufflen,
const GDate *  gd 
)

Convenience; calls through to qof_print_date_dmy_buff().

Definition at line 596 of file gnc-date.cpp.

597 {
598  GDate date;
599  g_date_clear (&date, 1);
600  date = *gd;
601  return qof_print_date_dmy_buff( buf, len,
602  g_date_get_day(&date),
603  g_date_get_month(&date),
604  g_date_get_year(&date) );
605 }
size_t qof_print_date_dmy_buff(gchar *buff, size_t buflen, int day, int month, int year)
qof_print_date_dmy_buff Convert a date as day / month / year integers into a localized string represe...

◆ qof_scan_date()

gboolean qof_scan_date ( const char *  buff,
int *  day,
int *  month,
int *  year 
)

qof_scan_date Convert a string into day / month / year integers according to the current dateFormat value.

Args: buff - pointer to date string day - will store day of the month as 1 ... 31 month - will store month of the year as 1 ... 12 year - will store the year (4-digit)

Return: TRUE if the string seemed to be a valid date; else FALSE.

Globals: uses global dateFormat value to assist in parsing.

Definition at line 924 of file gnc-date.cpp.

925 {
926  return qof_scan_date_internal(buff, day, month, year, dateFormat);
927 }

◆ qof_strftime()

gsize qof_strftime ( gchar *  buf,
gsize  max,
const gchar *  format,
const struct tm *  tm 
)

qof_strftime calls qof_format_time to print a given time and afterwards tries to put the result into a buffer of fixed size.

Warning
HACK ALERT – the scan and print routines should probably be moved to somewhere else. The engine really isn't involved with things like printing formats. This is needed mostly by the GUI and so on. If a file-io thing needs date handling, it should do it itself, instead of depending on the routines here.
Parameters
bufA buffer.
maxThe size of buf in bytes.
formatA format specification in UTF-8.
tmA broken-down time.
Returns
The number of characters written, not include the null byte, if the complete string, including the null byte, fits into the buffer. Otherwise 0.

Definition at line 1062 of file gnc-date.cpp.

1063 {
1064  gsize convlen, retval;
1065  gchar *convbuf;
1066 
1067  g_return_val_if_fail(buf, 0);
1068  g_return_val_if_fail(max > 0, 0);
1069  g_return_val_if_fail(format, 0);
1070  g_return_val_if_fail(tm, 0);
1071 
1072  convbuf = qof_format_time(format, tm);
1073  if (!convbuf)
1074  {
1075  buf[0] = '\0';
1076  return 0;
1077  }
1078 
1079  convlen = strlen(convbuf);
1080 
1081  if (max <= convlen)
1082  {
1083  /* Ensure only whole characters are copied into the buffer. */
1084  gchar *end = g_utf8_find_prev_char(convbuf, convbuf + max);
1085  g_assert(end != nullptr);
1086  convlen = end - convbuf;
1087 
1088  /* Return 0 because the buffer isn't large enough. */
1089  retval = 0;
1090  }
1091  else
1092  {
1093  retval = convlen;
1094  }
1095 
1096  memcpy(buf, convbuf, convlen);
1097  buf[convlen] = '\0';
1098  g_free(convbuf);
1099 
1100  return retval;
1101 }

◆ time64_to_gdate()

GDate time64_to_gdate ( time64  t)

Returns the GDate in which the time64 occurs.

Parameters
tThe time64
Returns
A GDate for the day in which the time64 occurs.

Definition at line 1217 of file gnc-date.cpp.

1218 {
1219  GDate result;
1220 
1221  g_date_clear (&result, 1);
1222  GncDateTime time(t);
1223  auto date = time.date().year_month_day();
1224  g_date_set_dmy (&result, date.day, static_cast<GDateMonth>(date.month),
1225  date.year);
1226  g_assert(g_date_valid (&result));
1227 
1228  return result;
1229 }
GnuCash DateTime class.