GnuCash  2.7.0
Files | Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
Date: Date and Time Printing, Parsing and Manipulation

Utility functions to handle date and time (adjusting, getting the current date, printing the date and time, etc.) More...

Files

file  gnc-date.h
 Date and Time handling routines.
 

Data Structures

struct  Timespec
 Use a 64-bit unsigned int timespec. More...
 

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
 
#define qof_date_format_get_format   qof_date_text_format_get_string
 

Typedefs

typedef gint64 time64
 Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-32 typedef for time_t. 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...
 
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 local time More...
 
gdouble gnc_difftime (const time64 secs1, const time64 secs2)
 Find the difference in seconds between two time values. More...
 
void gnc_tm_free (struct tm *time)
 free a struct tm* created with gnc_localtime() or gnc_gmtime() 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...
 

Variables

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

GValue

GType timespec_get_type (void)
 
#define GNC_TYPE_TIMESPEC   (timespec_get_type ())
 

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...
 

Timespec functions

gboolean timespec_equal (const Timespec *ta, const Timespec *tb)
 strict equality
 
gint timespec_cmp (const Timespec *ta, const Timespec *tb)
 comparison: if (ta < tb) -1; else if (ta > tb) 1; else 0;
 
Timespec timespec_diff (const Timespec *ta, const Timespec *tb)
 difference between ta and tb, results are normalized ie tv_sec and tv_nsec of the result have the same size abs(result.tv_nsec) <= 1000000000
 
Timespec timespec_abs (const Timespec *t)
 absolute value, also normalized
 
Timespec timespecCanonicalDayTime (Timespec t)
 convert a timepair on a certain day (localtime) to the timepair representing midday on that day. More...
 
Timespec timespec_now (void)
 Returns the current clock time as a Timespec, taken from time(2). More...
 
void timespecFromTime64 (Timespec *ts, time64 t)
 Turns a time64 into a Timespec.
 
time64 timespecToTime64 (Timespec ts)
 Turns a Timespec into a time64.
 
GDate timespec_to_gdate (Timespec ts)
 Turns a Timespec into a GDate.
 
Timespec gdate_to_timespec (GDate d)
 Turns a GDate into a Timespec, returning the first second of the day.
 
Timespec gnc_dmy2timespec (gint day, gint month, gint year)
 Convert a day, month, and year to a Timespec, returning the first second of the day.
 
Timespec gnc_dmy2timespec_end (gint day, gint month, gint year)
 Same as gnc_dmy2timespec, but last second of the day.
 
Timespec gnc_dmy2timespec_neutral (gint day, gint month, gint year)
 Converts a day, month, and year to a Timespec 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...
 
Timespec gnc_iso8601_to_timespec_gmt (const gchar *)
 The gnc_iso8601_to_timespec_gmt() routine converts an ISO-8601 style date/time string to Timespec. More...
 
gchar * gnc_timespec_to_iso8601_buff (Timespec ts, gchar *buff)
 The gnc_timespec_to_iso8601_buff() routine takes the input UTC Timespec value and prints it as an ISO-8601 style string. More...
 
void gnc_timespec2dmy (Timespec ts, gint *day, gint *month, gint *year)
 Set the proleptic Gregorian day, month, and year from a Timespec. 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...
 
const char * gnc_print_date (Timespec ts)
 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.

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 last second 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...
 

Today's Date

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_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...
 
#define MIN_BUF_LEN   10
 

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

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...
 

Detailed Description

Utility functions to handle date and time (adjusting, getting the current date, printing the date and time, etc.)

Overall, this file is quite a mess. Note, however, that other applications, besides just GnuCash, use this file. In particular, GnoTime (gttr.sourcefore.net) uses this file, and this file is formally a part of QOF (qof.sourceforge.net).

An important note about time-keeping: The general goal of any program that works with numeric time values SHOULD BE to always stores and use UNIVERSAL TIME internally. Universal time is the 'one true time' that is independent of one's location on planet Earth. It is measured in seconds from midnight January 1, 1970 in localtime-Greenwich (GMT). If one wants to display the local time, then the display-print routine should make all final tweaks to print the local time. The local time must not be kept as a numeric value anywhere in the program. If one wants to parse a user's input string as if it were local time, then the output of the parse routine MUST BE universal time. A sane program must never ever store (to file or db) a time that is not Universal Time. Break these rules, and you will rue the day...

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 backend needs date handling, it should do it itself, instead of depending on the routines here.

(to be renamed qofdate.h in libqof2.)

Author
Copyright (C) 1997 Robin D. Clark rclar.nosp@m.k@cs.nosp@m..hmc..nosp@m.edu
Copyright (C) 1998-2001,2003 Linas Vepstas linas.nosp@m.@lin.nosp@m.as.or.nosp@m.g

Macro Definition Documentation

◆ qof_date_format_get_format

#define qof_date_format_get_format   qof_date_text_format_get_string
Deprecated:
qof_date_format_get_format has been replaced by qof_date_text_format_get_string

Definition at line 145 of file gnc-date.h.

◆ QOF_UTC_DATE_FORMAT

#define QOF_UTC_DATE_FORMAT   "%Y-%m-%dT%H:%M:%SZ"

Constants.

UTC date format string.

Time zone independent, date and time inclusive, as used in the QSF backend. The T and Z characters are from xsd:dateTime format in coordinated universal time, UTC. You can reproduce the string from the GNU/Linux command line using the date utility: date -u +Y-m-dTH:M:SZ = 2004-12-12T23:39:11Z The datestring must be time zone independent and include all specified fields. Remember to use gmtime() NOT localtime()!

Definition at line 118 of file gnc-date.h.

Typedef Documentation

◆ time64

typedef gint64 time64

Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-32 typedef for time_t.

Since this stops working in 2038, we define our own:

Definition at line 84 of file gnc-date.h.

Enumeration Type Documentation

◆ QofDateCompletion

Enum for date completion modes (for dates entered without year)

Enumerator
QOF_DATE_COMPLETION_THISYEAR 

use current year

QOF_DATE_COMPLETION_SLIDING 

use sliding 12-month window

Definition at line 137 of file gnc-date.h.

138 {
QofDateCompletion
Enum for date completion modes (for dates entered without year)
Definition: gnc-date.h:137
use sliding 12-month window
Definition: gnc-date.h:140
use current year
Definition: gnc-date.h:139

◆ QofDateFormat

Enum for determining a date format.

Enumerator
QOF_DATE_FORMAT_US 

United states: mm/dd/yyyy.

QOF_DATE_FORMAT_UK 

Britain: dd/mm/yyyy.

QOF_DATE_FORMAT_CE 

Continental Europe: dd.mm.yyyy.

QOF_DATE_FORMAT_ISO 

ISO: yyyy-mm-dd.

QOF_DATE_FORMAT_LOCALE 

Take from locale information.

QOF_DATE_FORMAT_UTC 

UTC: 2004-12-12T23:39:11Z.

QOF_DATE_FORMAT_CUSTOM 

Used by the check printing code.

QOF_DATE_FORMAT_UNSET 

No Fancy Date Format, use Global.

Definition at line 121 of file gnc-date.h.

122 {
131 } QofDateFormat;
ISO: yyyy-mm-dd.
Definition: gnc-date.h:126
Used by the check printing code.
Definition: gnc-date.h:129
Continental Europe: dd.mm.yyyy.
Definition: gnc-date.h:125
No Fancy Date Format, use Global.
Definition: gnc-date.h:130
UTC: 2004-12-12T23:39:11Z.
Definition: gnc-date.h:128
Take from locale information.
Definition: gnc-date.h:127
Britain: dd/mm/yyyy.
Definition: gnc-date.h:124
QofDateFormat
Enum for determining a date format.
Definition: gnc-date.h:121
United states: mm/dd/yyyy.
Definition: gnc-date.h:123

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 1010 of file gnc-date.cpp.

1011 {
1012  static char locale_separator = '\0';
1013 
1014  switch (dateFormat)
1015  {
1016  case QOF_DATE_FORMAT_CE:
1017  return '.';
1018  case QOF_DATE_FORMAT_ISO:
1019  case QOF_DATE_FORMAT_UTC:
1020  return '-';
1021  case QOF_DATE_FORMAT_US:
1022  case QOF_DATE_FORMAT_UK:
1023  default:
1024  return '/';
1026  if (locale_separator != '\0')
1027  return locale_separator;
1028  else
1029  {
1030  /* Make a guess */
1031  gchar string[256];
1032  struct tm tm;
1033  time64 secs;
1034  gchar *s;
1035 
1036  secs = gnc_time (NULL);
1037  gnc_localtime_r(&secs, &tm);
1038  qof_strftime(string, sizeof(string), GNC_D_FMT, &tm);
1039 
1040  for (s = string; *s != '\0'; s++)
1041  if (!isdigit(*s))
1042  return (locale_separator = *s);
1043  }
1044  break;
1045  }
1046  return '\0';
1047 }
ISO: yyyy-mm-dd.
Definition: gnc-date.h:126
Continental Europe: dd.mm.yyyy.
Definition: gnc-date.h:125
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:127
UTC: 2004-12-12T23:39:11Z.
Definition: gnc-date.h:128
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:1138
time64 gnc_time(time64 *tbuf)
get the current local time
Definition: gnc-date.cpp:251
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:84
Take from locale information.
Definition: gnc-date.h:127
Britain: dd/mm/yyyy.
Definition: gnc-date.h:124
United states: mm/dd/yyyy.
Definition: gnc-date.h:123

◆ gnc_ctime()

gchar* gnc_ctime ( const time64 secs)

Return a string representation of a date from a 64-bit time value.

Parameters
secsSeconds since 00:00:01 UTC 01 January 1970 (negative values are seconds before that moment)
Returns
A string, which must be freed with g_free(), representing the date in the following format: Thu Nov 24 18:22:48 1986
\0 This is equivalent to the strftime format a b H:M:S Y.

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

246 {
247  return gnc_print_time64(clamp_time(*secs), "%a %b %d %H:%M:%S %Y");
248 }
char * gnc_print_time64(time64 time, const char *format)
print a time64 as a date string per format
Definition: gnc-date.cpp:357

◆ 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 493 of file gnc-date.cpp.

494 {
495  static int last_day_of_month[2][12] =
496  {
497  /* non leap */ {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
498  /* leap */ {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
499  };
500 
501  /* Is this a leap year? */
502  if (year % 2000 == 0) return last_day_of_month[1][month];
503  if (year % 400 == 0 ) return last_day_of_month[0][month];
504  if (year % 4 == 0 ) return last_day_of_month[1][month];
505  return last_day_of_month[0][month];
506 }

◆ gnc_date_string_to_dateformat()

gboolean gnc_date_string_to_dateformat ( const gchar *  format_string,
QofDateFormat format 
)

Converts the date format to a printable string.

Note the reversed return values!

Returns
FALSE on success, TRUE on failure.

◆ gnc_date_string_to_monthformat()

gboolean gnc_date_string_to_monthformat ( const gchar *  format_string,
GNCDateMonthFormat format 
)

Converts the month format to a printable string.

Note the reversed return values!

Returns
FALSE on success, TRUE on failure.

◆ 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 1183 of file gnc-date.cpp.

1184 {
1185  return gnc_print_time64(gnc_time(nullptr), "%Y%m%d%H%M%S");
1186 }
char * gnc_print_time64(time64 time, const char *format)
print a time64 as a date string per format
Definition: gnc-date.cpp:357
time64 gnc_time(time64 *tbuf)
get the current local time
Definition: gnc-date.cpp:251

◆ gnc_difftime()

gdouble gnc_difftime ( const time64  secs1,
const time64  secs2 
)

Find the difference in seconds between two time values.

Parameters
secs1The first time value, in Seconds since 00:00:01 UTC 01 January 1970 (negative values are seconds before that moment)
secs2The second time value, in Seconds since 00:00:01 UTC 01 January 1970 (negative values are seconds before that moment)
Returns
The difference in seconds between secs1 and secs2. If secs2 is later than secs1 the value will be negative.

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

262 {
263  return (double)secs1 - (double)secs2;
264 }

◆ gnc_dmy2timespec_neutral()

Timespec gnc_dmy2timespec_neutral ( gint  day,
gint  month,
gint  year 
)

Converts a day, month, and year to a Timespec 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.

Since users in those timezones would see dates immediately change by one day, the function checks the current timezone for those changes and adjusts the UTC time so that the date will be consistent.

◆ 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 1451 of file gnc-date.cpp.

1452 {
1453  struct tm my_tm;
1454  int i;
1455 
1456  memset(buf, 0, buf_len);
1457  memset(&my_tm, 0, sizeof(struct tm));
1458  my_tm.tm_wday = dow;
1459  i = qof_strftime(buf, buf_len, "%a", &my_tm);
1460  buf[i] = 0;
1461 }
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:1138

◆ gnc_g_date_new_today()

GDate* gnc_g_date_new_today ( void  )

Returns a newly allocated date of the current clock time, taken from time(2).

The caller must g_date_free() the object afterwards.

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

1345 {
1346  GncDate gncd;
1347  auto ymd = gncd.year_month_day();
1348  auto month = static_cast<GDateMonth>(ymd.month);
1349  auto result = g_date_new_dmy (ymd.day, month, ymd.year);
1350  g_assert(g_date_valid (result));
1351  return result;
1352 }void
ymd year_month_day() const
Get the year, month, and day from the date as a ymd.
GnuCash Date class.

◆ 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 1508 of file gnc-date.cpp.

1509 {
1510  return (g_date_compare( (GDate*)gda, (GDate*)gdb ) == 0 ? TRUE : FALSE);
1511 }

◆ 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 1514 of file gnc-date.cpp.

1515 {
1516  gint val = (g_date_get_year( (GDate*)gd ) * 10000)
1517  + (g_date_get_month( (GDate*)gd ) * 100)
1518  + g_date_get_day( (GDate*)gd );
1519  return g_int_hash( &val );
1520 }

◆ 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 1715 of file gnc-date.cpp.

1717 {
1718  GDate temp;
1719  gboolean new_fy;
1720 
1721  g_return_if_fail(date);
1722  g_return_if_fail(fy_end);
1723 
1724  /* Compute the FY end that occurred this CY */
1725  temp = *fy_end;
1726  g_date_set_year(&temp, g_date_get_year(date));
1727 
1728  /* Has it already passed? */
1729  new_fy = (g_date_compare(date, &temp) > 0);
1730 
1731  /* Set end date */
1732  *date = temp;
1733  if (new_fy)
1734  g_date_add_years(date, 1);
1735 }

◆ 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 1691 of file gnc-date.cpp.

1693 {
1694  GDate temp;
1695  gboolean new_fy;
1696 
1697  g_return_if_fail(date);
1698  g_return_if_fail(fy_end);
1699 
1700  /* Compute the FY end that occurred this CY */
1701  temp = *fy_end;
1702  g_date_set_year(&temp, g_date_get_year(date));
1703 
1704  /* Has it already passed? */
1705  new_fy = (g_date_compare(date, &temp) > 0);
1706 
1707  /* Set start date */
1708  *date = temp;
1709  g_date_add_days(date, 1);
1710  if (!new_fy)
1711  g_date_subtract_years(date, 1);
1712 }

◆ 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 1574 of file gnc-date.cpp.

1575 {
1576  /* First set the start of next month. */
1577  g_date_set_day(date, 1);
1578  g_date_add_months(date, 1);
1579 
1580  /* Then back up one day */
1581  g_date_subtract_days(date, 1);
1582 }

◆ 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 1561 of file gnc-date.cpp.

1562 {
1563  g_date_set_day(date, 1);
1564 }

◆ 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 1749 of file gnc-date.cpp.

1751 {
1752  g_return_if_fail(date);
1753  g_return_if_fail(fy_end);
1754 
1755  gnc_gdate_set_fiscal_year_end(date, fy_end);
1756  g_date_subtract_years(date, 1);
1757 }
void gnc_gdate_set_fiscal_year_end(GDate *date, const GDate *fy_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:1715

◆ 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 1738 of file gnc-date.cpp.

1740 {
1741  g_return_if_fail(date);
1742  g_return_if_fail(fy_end);
1743 
1744  gnc_gdate_set_fiscal_year_start(date, fy_end);
1745  g_date_subtract_years(date, 1);
1746 }
void gnc_gdate_set_fiscal_year_start(GDate *date, const GDate *fy_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:1691

◆ 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 1606 of file gnc-date.cpp.

1607 {
1608  /* This will correctly handle the varying month lengths */
1609  g_date_set_day(date, 1);
1610  g_date_subtract_days(date, 1);
1611 }

◆ 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 1592 of file gnc-date.cpp.

1593 {
1594  g_date_set_day(date, 1);
1595  g_date_subtract_months(date, 1);
1596 }

◆ 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-07-31.

Parameters
dateThe GDate to modify.

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

1653 {
1655  g_date_subtract_months(date, 3);
1656 }
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:1629

◆ 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-06-01.

Parameters
dateThe GDate to modify.

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

1646 {
1648  g_date_subtract_months(date, 3);
1649 }
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:1616

◆ 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 1682 of file gnc-date.cpp.

1683 {
1684  gnc_gdate_set_year_end(date);
1685  g_date_subtract_years(date, 1);
1686 }
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:1668

◆ 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 1675 of file gnc-date.cpp.

1676 {
1678  g_date_subtract_years(date, 1);
1679 }
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:1661

◆ 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-12-31.

Parameters
dateThe GDate to modify.

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

1630 {
1631  gint months;
1632 
1633  /* Set the date to the first day of the specified month. */
1634  g_date_set_day(date, 1);
1635 
1636  /* Add 1-3 months to get the first day of the next quarter.*/
1637  months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
1638  g_date_add_months(date, 3 - months);
1639 
1640  /* Now back up one day */
1641  g_date_subtract_days(date, 1);
1642 }

◆ 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-09-01.

Parameters
dateThe GDate to modify.

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

1617 {
1618  gint months;
1619 
1620  /* Set the date to the first day of the specified month. */
1621  g_date_set_day(date, 1);
1622 
1623  /* Back up 0-2 months */
1624  months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
1625  g_date_subtract_months(date, months);
1626 }

◆ gnc_gdate_set_time64()

void gnc_gdate_set_time64 ( GDate *  gd,
time64  time 
)

Set a GDate to a time64.

Parameters
gdthe date to act on
timethe time to set it to.

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

1363 {
1364  struct tm tm;
1365  gnc_localtime_r(&time, &tm);
1366  g_date_set_dmy (gd, tm.tm_mday,
1367  static_cast<GDateMonth>(tm.tm_mon + 1),
1368  tm.tm_year + 1900);
1369 }
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:127

◆ gnc_gdate_set_today()

void gnc_gdate_set_today ( GDate *  gd)

Set a GDate to the current day.

Parameters
gdThe date to act on

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

1355 {
1356  GDate *today = gnc_g_date_new_today ();
1357  g_date_set_julian (gd, g_date_get_julian (today));
1358  g_date_free (today);
1359 }
GDate * gnc_g_date_new_today()
Returns a newly allocated date of the current clock time, taken from time(2).
Definition: gnc-date.cpp:1344

◆ 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 1668 of file gnc-date.cpp.

1669 {
1670  g_date_set_month(date, G_DATE_DECEMBER);
1671  g_date_set_day(date, 31);
1672 }

◆ 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 1661 of file gnc-date.cpp.

1662 {
1663  g_date_set_month(date, G_DATE_JANUARY);
1664  g_date_set_day(date, 1);
1665 }

◆ gnc_gmtime()

struct tm* gnc_gmtime ( const time64 secs)

fill out a time struct from a 64-bit time value

Parameters
secsSeconds since 00:00:01 UTC 01 January 1970 (negative values are seconds before that moment)
Returns
A struct tm*, allocated on the heap. Must be freed with gnc_tm_free() The time is UTC.

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

200 {
201  try
202  {
203  auto time = static_cast<struct tm*>(calloc(1, sizeof(struct tm)));
204  GncDateTime gncdt(*secs);
205  *time = gncdt.utc_tm();
206  return time;
207  }
208  catch(std::invalid_argument)
209  {
210  return NULL;
211  }
212 
213 }
GnuCash DateTime class.

◆ gnc_iso8601_to_timespec_gmt()

Timespec gnc_iso8601_to_timespec_gmt ( const gchar *  )

The gnc_iso8601_to_timespec_gmt() routine converts an ISO-8601 style date/time string to Timespec.

Please note that ISO-8601 strings are a representation of Universal Time (UTC), and as such, they 'store' UTC. To make them human readable, they show time zone information along with a local-time string. But fundamentally, they are UTC. Thus, this routine takes a UTC input, and returns a UTC output.

For example: 1998-07-17 11:00:00.68-0500 is 680 milliseconds after 11 o'clock, central daylight time It is also 680 milliseconds after 16:00:00 hours UTC.

Returns
The universal time.

XXX Caution: this routine does not handle strings that specify times before January 1 1970.

◆ gnc_localtime()

struct tm* gnc_localtime ( const time64 secs)

fill out a time struct from a 64-bit time value.

Parameters
secsSeconds since 00:00:01 UTC 01 January 1970 (negative values are seconds before that moment).
Returns
A struct tm*, allocated on the heap. Must be freed with gnc_tm_free(). The time is adjusted for the current local time zone.

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

116 {
117  auto time = static_cast<struct tm*>(calloc(1, sizeof(struct tm)));
118  if (gnc_localtime_r (secs, time) == NULL)
119  {
120  gnc_tm_free (time);
121  return NULL;
122  }
123  return time;
124 }
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:127
void gnc_tm_free(struct tm *time)
free a struct tm* created with gnc_localtime() or gnc_gmtime()
Definition: gnc-date.cpp:109

◆ gnc_localtime_r()

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.

Parameters
secsSeconds since 00:00:01 UTC 01 January 1970 (negative values are seconds before that moment)
timeA struct tm* for the function to fill. The time is adjusted for the current local time zone.

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

128 {
129  try
130  {
131  *time = static_cast<struct tm>(GncDateTime(*secs));
132  return time;
133  }
134  catch(std::invalid_argument)
135  {
136  return NULL;
137  }
138 }
GnuCash DateTime class.

◆ gnc_mktime()

time64 gnc_mktime ( struct tm *  time)

calculate seconds from the epoch given a time struct

Parameters
timeA struct tm* containing the date-time information. The time is understood to be in the current local time zone.
Returns
Seconds since 00:00:01 UTC 01 January 1970 (negative values are seconds before that moment).

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

217 {
218  try
219  {
220  normalize_struct_tm (time);
221  GncDateTime gncdt(*time);
222  return static_cast<time64>(gncdt) - gncdt.offset();
223  }
224  catch(std::invalid_argument)
225  {
226  return 0;
227  }
228 }
GnuCash DateTime class.
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:84

◆ gnc_print_date()

const char* gnc_print_date ( Timespec  ts)

Convenience; calls through to qof_print_date_dmy_buff().

Return: static global string.

Warning
This routine is not thread-safe, because it uses a single global buffer to store the return value. Use qof_print_date_buff() or qof_print_date() instead.

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

697 {
698  static char buff[MAX_DATE_LENGTH];
699  time64 t;
700 
701  memset (buff, 0, sizeof (buff));
702  t = ts.tv_sec + (time64)(ts.tv_nsec / 1000000000.0);
703 
705 
706  return buff;
707 }
#define MAX_DATE_LENGTH
The maximum length of a string created by the date printers.
Definition: gnc-date.h:107
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:84
size_t qof_print_date_buff(char *buff, size_t len, time64 t)
Convenience: calls through to qof_print_date_dmy_buff().
Definition: gnc-date.cpp:651

◆ gnc_print_time64()

char* gnc_print_time64 ( time64  time,
const char *  format 
)

print a time64 as a date string per format

Parameters
timeThe time64 to print
formatA date format conforming to the strftime format rules.
Returns
a raw heap-allocated char* which must be freed.

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

358 {
359  try
360  {
361  GncDateTime gncdt(time);
362  auto sstr = gncdt.format(format);
363  //ugly C allocation so that the ptr can be freed at the other end
364  char* cstr = static_cast<char*>(malloc(sstr.length() + 1));
365  memset(cstr, 0, sstr.length() + 1);
366  strncpy(cstr, sstr.c_str(), sstr.length());
367  return cstr;
368  }
369  catch(std::runtime_error& err)
370  {
371  PWARN("Error processing time64 %" PRId64 ": %s", time, err.what());
372  return nullptr;
373  }
374  catch(std::logic_error& err)
375  {
376  PWARN("Error processing time64 %" PRId64 ": %s", time, err.what());
377  return nullptr;
378  }
379 }
GnuCash DateTime class.
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:243

◆ gnc_time()

time64 gnc_time ( time64 tbuf)

get the current local time

Parameters
Atime64* which, if not NULL, will be filled in with the same value as is returned.
Returns
Seconds since 00:00:01 UTC 01 January 1970 (negative values are seconds before that moment)

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

252 {
253  GncDateTime gncdt;
254  auto time = static_cast<time64>(gncdt);
255  if (tbuf != NULL)
256  *tbuf = time;
257  return time;
258 }
GnuCash DateTime class.
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:84

◆ 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 1408 of file gnc-date.cpp.

1409 {
1410  struct tm tm;
1411  time64 new_time;
1412 
1413  gnc_tm_get_day_end(&tm, time_val);
1414  new_time = gnc_mktime(&tm);
1415  return new_time;
1416 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:216
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:84

◆ 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 last second of that day.

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

1398 {
1399  struct tm tm;
1400  time64 new_time;
1401 
1402  gnc_tm_get_day_start(&tm, time_val);
1403  new_time = gnc_mktime(&tm);
1404  return new_time;
1405 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:216
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:84

◆ 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 1442 of file gnc-date.cpp.

1443 {
1444  struct tm tm;
1445 
1446  gnc_tm_get_day_end(&tm, time(NULL));
1447  return gnc_mktime(&tm);
1448 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:216

◆ 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 1433 of file gnc-date.cpp.

1434 {
1435  struct tm tm;
1436 
1437  gnc_tm_get_day_start(&tm, time(NULL));
1438  return gnc_mktime(&tm);
1439 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:216

◆ gnc_timegm()

time64 gnc_timegm ( struct tm *  time)

calculate seconds from the epoch given a time struct

Parameters
timeA struct tm* containing the date-time information The time is understood to be utc.
Returns
Seconds since 00:00:01 UTC 01 January 1970 (negative values are seconds before that moment).

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

232 {
233  try
234  {
235  normalize_struct_tm(time);
236  return static_cast<time64>(GncDateTime(*time));
237  }
238  catch(std::invalid_argument)
239  {
240  return 0;
241  }
242 }
GnuCash DateTime class.
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:84

◆ gnc_timespec2dmy()

void gnc_timespec2dmy ( Timespec  ts,
gint *  day,
gint *  month,
gint *  year 
)

Set the proleptic Gregorian day, month, and year from a Timespec.

Parameters
tsinput timespec
dayoutput day, 1 - 31
monthoutput month, 1 - 12
yearoutput year, 0001 - 9999 CE

◆ gnc_timespec_to_iso8601_buff()

gchar* gnc_timespec_to_iso8601_buff ( Timespec  ts,
gchar *  buff 
)

The gnc_timespec_to_iso8601_buff() routine takes the input UTC Timespec value and prints it as an ISO-8601 style string.

The buffer must be long enough to contain the NULL-terminated string (32 characters + NUL). This routine returns a pointer to the null terminator (and can thus be used in the 'stpcpy' metaphor of string concatenation).

Please note that ISO-8601 strings are a representation of Universal Time (UTC), and as such, they 'store' UTC. To make them human readable, they show time zone information along with a local-time string. But fundamentally, they are UTC. Thus, this routine takes a UTC input, and returns a UTC output.

The string generated by this routine uses the local time zone on the machine on which it is executing to create the time string.

◆ gnc_tm_free()

void gnc_tm_free ( struct tm *  time)

free a struct tm* created with gnc_localtime() or gnc_gmtime()

Parameters
timeThe struct tm* to be freed.

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

110 {
111  free(time);
112 }

◆ 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 1427 of file gnc-date.cpp.

1428 {
1429  gnc_tm_get_day_end(tm, time(NULL));
1430 }

◆ 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 1421 of file gnc-date.cpp.

1422 {
1423  gnc_tm_get_day_start(tm, time(NULL));
1424 }

◆ 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 546 of file gnc-date.cpp.

547 {
548  if (dc == QOF_DATE_COMPLETION_THISYEAR ||
550  {
551  dateCompletion = dc;
552  }
553  else
554  {
555  /* hack alert - Use a neutral default. */
556  PERR("non-existent date completion set attempted. Setting current year completion as default");
557  dateCompletion = QOF_DATE_COMPLETION_THISYEAR;
558  }
559 
560  if (backmonths < 0)
561  {
562  backmonths = 0;
563  }
564  else if (backmonths > 11)
565  {
566  backmonths = 11;
567  }
568  dateCompletionBackMonths = backmonths;
569 
570  return;
571 }
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:237
use sliding 12-month window
Definition: gnc-date.h:140
use current year
Definition: gnc-date.h:139

◆ 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 508 of file gnc-date.cpp.

509 {
510  return dateFormat;
511 }

◆ 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 580 of file gnc-date.cpp.

581 {
582  switch (df)
583  {
584  case QOF_DATE_FORMAT_US:
585  return "%m/%d/%Y";
586  case QOF_DATE_FORMAT_UK:
587  return "%d/%m/%Y";
588  case QOF_DATE_FORMAT_CE:
589  return "%d.%m.%Y";
590  case QOF_DATE_FORMAT_UTC:
591  return "%Y-%m-%dT%H:%M:%SZ";
592  case QOF_DATE_FORMAT_ISO:
593  return "%Y-%m-%d";
594  case QOF_DATE_FORMAT_UNSET: // use global
595  return qof_date_format_get_string (dateFormat);
597  default:
598  break;
599  };
600  return GNC_D_FMT;
601 }
ISO: yyyy-mm-dd.
Definition: gnc-date.h:126
Continental Europe: dd.mm.yyyy.
Definition: gnc-date.h:125
No Fancy Date Format, use Global.
Definition: gnc-date.h:130
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:580
UTC: 2004-12-12T23:39:11Z.
Definition: gnc-date.h:128
Take from locale information.
Definition: gnc-date.h:127
Britain: dd/mm/yyyy.
Definition: gnc-date.h:124
United states: mm/dd/yyyy.
Definition: gnc-date.h:123

◆ 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 513 of file gnc-date.cpp.

514 {
515  if (df >= DATE_FORMAT_FIRST && df <= DATE_FORMAT_LAST)
516  {
517  prevQofDateFormat = dateFormat;
518  dateFormat = df;
519  }
520  else
521  {
522  /* hack alert - Use a neutral default. */
523  PERR("non-existent date format set attempted. Setting ISO default");
524  prevQofDateFormat = dateFormat;
525  dateFormat = QOF_DATE_FORMAT_ISO;
526  }
527 
528  return;
529 }
ISO: yyyy-mm-dd.
Definition: gnc-date.h:126
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:237

◆ 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 603 of file gnc-date.cpp.

604 {
605  switch (df)
606  {
607  case QOF_DATE_FORMAT_US:
608  return "%b %d, %Y";
609  case QOF_DATE_FORMAT_UK:
610  case QOF_DATE_FORMAT_CE:
611  return "%d %b %Y";
612  case QOF_DATE_FORMAT_UTC:
613  return "%Y-%m-%dT%H:%M:%SZ";
614  case QOF_DATE_FORMAT_ISO:
615  return "%Y-%b-%d";
616  case QOF_DATE_FORMAT_UNSET: // use global
617  return qof_date_text_format_get_string (dateFormat);
619  default:
620  break;
621  };
622  return GNC_D_FMT;
623 }
ISO: yyyy-mm-dd.
Definition: gnc-date.h:126
Continental Europe: dd.mm.yyyy.
Definition: gnc-date.h:125
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:603
No Fancy Date Format, use Global.
Definition: gnc-date.h:130
UTC: 2004-12-12T23:39:11Z.
Definition: gnc-date.h:128
Take from locale information.
Definition: gnc-date.h:127
Britain: dd/mm/yyyy.
Definition: gnc-date.h:124
United states: mm/dd/yyyy.
Definition: gnc-date.h:123

◆ 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 687 of file gnc-date.cpp.

688 {
689  char buff[MAX_DATE_LENGTH];
690  memset (buff, 0, sizeof (buff));
692  return g_strdup (buff);
693 }
#define MAX_DATE_LENGTH
The maximum length of a string created by the date printers.
Definition: gnc-date.h:107
size_t qof_print_date_buff(char *buff, size_t len, time64 t)
Convenience: calls through to qof_print_date_dmy_buff().
Definition: gnc-date.cpp:651

◆ 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 651 of file gnc-date.cpp.

652 {
653  if (!buff) return 0;
654 
655  try
656  {
657  GncDateTime gncdt(clamp_time(t));
658  std::string str = gncdt.format(qof_date_format_get_string(dateFormat));
659  strncpy(buff, str.c_str(), len);
660  if (str.length() >= len)
661  buff[len - 1] = '\0';
662  }
663  catch(std::logic_error& err)
664  {
665  PWARN("Error processing time64 %" PRId64 ": %s", t, err.what());
666  }
667  catch(std::runtime_error& err)
668  {
669  PWARN("Error processing time64 %" PRId64 ": %s", t, err.what());
670  }
671  return strlen(buff);
672 }
GnuCash DateTime class.
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:243
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:580

◆ 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 675 of file gnc-date.cpp.

676 {
677  GDate date;
678  g_date_clear (&date, 1);
679  date = *gd;
680  return qof_print_date_dmy_buff( buf, len,
681  g_date_get_day(&date),
682  g_date_get_month(&date),
683  g_date_get_year(&date) );
684 }
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 1002 of file gnc-date.cpp.

1003 {
1004  return qof_scan_date_internal(buff, day, month, year, dateFormat);
1005 }

◆ 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 1138 of file gnc-date.cpp.

1139 {
1140  gsize convlen, retval;
1141  gchar *convbuf;
1142 
1143  g_return_val_if_fail(buf, 0);
1144  g_return_val_if_fail(max > 0, 0);
1145  g_return_val_if_fail(format, 0);
1146  g_return_val_if_fail(tm, 0);
1147 
1148  convbuf = qof_format_time(format, tm);
1149  if (!convbuf)
1150  {
1151  buf[0] = '\0';
1152  return 0;
1153  }
1154 
1155  convlen = strlen(convbuf);
1156 
1157  if (max <= convlen)
1158  {
1159  /* Ensure only whole characters are copied into the buffer. */
1160  gchar *end = g_utf8_find_prev_char(convbuf, convbuf + max);
1161  g_assert(end != NULL);
1162  convlen = end - convbuf;
1163 
1164  /* Return 0 because the buffer isn't large enough. */
1165  retval = 0;
1166  }
1167  else
1168  {
1169  retval = convlen;
1170  }
1171 
1172  memcpy(buf, convbuf, convlen);
1173  buf[convlen] = '\0';
1174  g_free(convbuf);
1175 
1176  return retval;
1177 }

◆ timespec_now()

Timespec timespec_now ( void  )

Returns the current clock time as a Timespec, taken from time(2).

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

1313 {
1314  Timespec ts;
1315  ts.tv_sec = gnc_time(NULL);
1316  ts.tv_nsec = 0;
1317  return ts;
1318 }
time64 gnc_time(time64 *tbuf)
get the current local time
Definition: gnc-date.cpp:251

◆ timespecCanonicalDayTime()

Timespec timespecCanonicalDayTime ( Timespec  t)

convert a timepair on a certain day (localtime) to the timepair representing midday on that day.

Watch out - this is not the first second of the day, which is returned by various other functions returning a Timespec.

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

481 {
482  struct tm tm;
483  Timespec retval;
484  time64 t_secs = t.tv_sec + (t.tv_nsec / NANOS_PER_SECOND);
485  gnc_localtime_r(&t_secs, &tm);
486  gnc_tm_set_day_middle(&tm);
487  retval.tv_sec = gnc_mktime(&tm);
488  retval.tv_nsec = 0;
489  return retval;
490 }
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:127
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:216
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:84

Variable Documentation

◆ gnc_default_strftime_date_format

const char* gnc_default_strftime_date_format

The default date format for use with strftime.

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