GnuCash  2.7.2
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 998 of file gnc-date.cpp.

999 {
1000  static char locale_separator = '\0';
1001 
1002  switch (dateFormat)
1003  {
1004  case QOF_DATE_FORMAT_CE:
1005  return '.';
1006  case QOF_DATE_FORMAT_ISO:
1007  case QOF_DATE_FORMAT_UTC:
1008  return '-';
1009  case QOF_DATE_FORMAT_US:
1010  case QOF_DATE_FORMAT_UK:
1011  default:
1012  return '/';
1014  if (locale_separator != '\0')
1015  return locale_separator;
1016  else
1017  {
1018  /* Make a guess */
1019  gchar string[256];
1020  struct tm tm;
1021  time64 secs;
1022  gchar *s;
1023 
1024  secs = gnc_time (NULL);
1025  gnc_localtime_r(&secs, &tm);
1026  qof_strftime(string, sizeof(string), GNC_D_FMT, &tm);
1027 
1028  for (s = string; *s != '\0'; s++)
1029  if (!isdigit(*s))
1030  return (locale_separator = *s);
1031  }
1032  break;
1033  }
1034  return '\0';
1035 }
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:116
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:1126
time64 gnc_time(time64 *tbuf)
get the current local time
Definition: gnc-date.cpp:240
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 234 of file gnc-date.cpp.

235 {
236  return gnc_print_time64(*secs, "%a %b %d %H:%M:%S %Y");
237 }
char * gnc_print_time64(time64 time, const char *format)
print a time64 as a date string per format
Definition: gnc-date.cpp:346

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

483 {
484  static int last_day_of_month[2][12] =
485  {
486  /* non leap */ {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
487  /* leap */ {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
488  };
489 
490  /* Is this a leap year? */
491  if (year % 2000 == 0) return last_day_of_month[1][month];
492  if (year % 400 == 0 ) return last_day_of_month[0][month];
493  if (year % 4 == 0 ) return last_day_of_month[1][month];
494  return last_day_of_month[0][month];
495 }

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

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

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

251 {
252  return (double)secs1 - (double)secs2;
253 }

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

1440 {
1441  struct tm my_tm;
1442  int i;
1443 
1444  memset(buf, 0, buf_len);
1445  memset(&my_tm, 0, sizeof(struct tm));
1446  my_tm.tm_wday = dow;
1447  i = qof_strftime(buf, buf_len, "%a", &my_tm);
1448  buf[i] = 0;
1449 }
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:1126

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

1333 {
1334  GncDate gncd;
1335  auto ymd = gncd.year_month_day();
1336  auto month = static_cast<GDateMonth>(ymd.month);
1337  auto result = g_date_new_dmy (ymd.day, month, ymd.year);
1338  g_assert(g_date_valid (result));
1339  return result;
1340 }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 1496 of file gnc-date.cpp.

1497 {
1498  return (g_date_compare( (GDate*)gda, (GDate*)gdb ) == 0 ? TRUE : FALSE);
1499 }

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

1503 {
1504  gint val = (g_date_get_year( (GDate*)gd ) * 10000)
1505  + (g_date_get_month( (GDate*)gd ) * 100)
1506  + g_date_get_day( (GDate*)gd );
1507  return g_int_hash( &val );
1508 }

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

1705 {
1706  GDate temp;
1707  gboolean new_fy;
1708 
1709  g_return_if_fail(date);
1710  g_return_if_fail(fy_end);
1711 
1712  /* Compute the FY end that occurred this CY */
1713  temp = *fy_end;
1714  g_date_set_year(&temp, g_date_get_year(date));
1715 
1716  /* Has it already passed? */
1717  new_fy = (g_date_compare(date, &temp) > 0);
1718 
1719  /* Set end date */
1720  *date = temp;
1721  if (new_fy)
1722  g_date_add_years(date, 1);
1723 }

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

1681 {
1682  GDate temp;
1683  gboolean new_fy;
1684 
1685  g_return_if_fail(date);
1686  g_return_if_fail(fy_end);
1687 
1688  /* Compute the FY end that occurred this CY */
1689  temp = *fy_end;
1690  g_date_set_year(&temp, g_date_get_year(date));
1691 
1692  /* Has it already passed? */
1693  new_fy = (g_date_compare(date, &temp) > 0);
1694 
1695  /* Set start date */
1696  *date = temp;
1697  g_date_add_days(date, 1);
1698  if (!new_fy)
1699  g_date_subtract_years(date, 1);
1700 }

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

1563 {
1564  /* First set the start of next month. */
1565  g_date_set_day(date, 1);
1566  g_date_add_months(date, 1);
1567 
1568  /* Then back up one day */
1569  g_date_subtract_days(date, 1);
1570 }

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

1550 {
1551  g_date_set_day(date, 1);
1552 }

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

1739 {
1740  g_return_if_fail(date);
1741  g_return_if_fail(fy_end);
1742 
1743  gnc_gdate_set_fiscal_year_end(date, fy_end);
1744  g_date_subtract_years(date, 1);
1745 }
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:1703

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

1728 {
1729  g_return_if_fail(date);
1730  g_return_if_fail(fy_end);
1731 
1732  gnc_gdate_set_fiscal_year_start(date, fy_end);
1733  g_date_subtract_years(date, 1);
1734 }
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:1679

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

1595 {
1596  /* This will correctly handle the varying month lengths */
1597  g_date_set_day(date, 1);
1598  g_date_subtract_days(date, 1);
1599 }

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

1581 {
1582  g_date_set_day(date, 1);
1583  g_date_subtract_months(date, 1);
1584 }

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

1641 {
1643  g_date_subtract_months(date, 3);
1644 }
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:1617

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

1634 {
1636  g_date_subtract_months(date, 3);
1637 }
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:1604

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

1671 {
1672  gnc_gdate_set_year_end(date);
1673  g_date_subtract_years(date, 1);
1674 }
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:1656

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

1664 {
1666  g_date_subtract_years(date, 1);
1667 }
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:1649

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

1618 {
1619  gint months;
1620 
1621  /* Set the date to the first day of the specified month. */
1622  g_date_set_day(date, 1);
1623 
1624  /* Add 1-3 months to get the first day of the next quarter.*/
1625  months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
1626  g_date_add_months(date, 3 - months);
1627 
1628  /* Now back up one day */
1629  g_date_subtract_days(date, 1);
1630 }

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

1605 {
1606  gint months;
1607 
1608  /* Set the date to the first day of the specified month. */
1609  g_date_set_day(date, 1);
1610 
1611  /* Back up 0-2 months */
1612  months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
1613  g_date_subtract_months(date, months);
1614 }

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

1351 {
1352  struct tm tm;
1353  gnc_localtime_r(&time, &tm);
1354  g_date_set_dmy (gd, tm.tm_mday,
1355  static_cast<GDateMonth>(tm.tm_mon + 1),
1356  tm.tm_year + 1900);
1357 }
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:116

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

1343 {
1344  GDate *today = gnc_g_date_new_today ();
1345  g_date_set_julian (gd, g_date_get_julian (today));
1346  g_date_free (today);
1347 }
GDate * gnc_g_date_new_today()
Returns a newly allocated date of the current clock time, taken from time(2).
Definition: gnc-date.cpp:1332

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

1657 {
1658  g_date_set_month(date, G_DATE_DECEMBER);
1659  g_date_set_day(date, 31);
1660 }

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

1650 {
1651  g_date_set_month(date, G_DATE_JANUARY);
1652  g_date_set_day(date, 1);
1653 }

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

189 {
190  try
191  {
192  auto time = static_cast<struct tm*>(calloc(1, sizeof(struct tm)));
193  GncDateTime gncdt(*secs);
194  *time = gncdt.utc_tm();
195  return time;
196  }
197  catch(std::invalid_argument)
198  {
199  return NULL;
200  }
201 
202 }
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 104 of file gnc-date.cpp.

105 {
106  auto time = static_cast<struct tm*>(calloc(1, sizeof(struct tm)));
107  if (gnc_localtime_r (secs, time) == NULL)
108  {
109  gnc_tm_free (time);
110  return NULL;
111  }
112  return time;
113 }
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:116
void gnc_tm_free(struct tm *time)
free a struct tm* created with gnc_localtime() or gnc_gmtime()
Definition: gnc-date.cpp:98

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

117 {
118  try
119  {
120  *time = static_cast<struct tm>(GncDateTime(*secs));
121  return time;
122  }
123  catch(std::invalid_argument)
124  {
125  return NULL;
126  }
127 }
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 205 of file gnc-date.cpp.

206 {
207  try
208  {
209  normalize_struct_tm (time);
210  GncDateTime gncdt(*time);
211  return static_cast<time64>(gncdt) - gncdt.offset();
212  }
213  catch(std::invalid_argument)
214  {
215  return 0;
216  }
217 }
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 684 of file gnc-date.cpp.

685 {
686  static char buff[MAX_DATE_LENGTH];
687  time64 t;
688 
689  memset (buff, 0, sizeof (buff));
690  t = ts.tv_sec + (time64)(ts.tv_nsec / 1000000000.0);
691 
693 
694  return buff;
695 }
#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:640

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

347 {
348  try
349  {
350  GncDateTime gncdt(time);
351  auto sstr = gncdt.format(format);
352  //ugly C allocation so that the ptr can be freed at the other end
353  char* cstr = static_cast<char*>(malloc(sstr.length() + 1));
354  memset(cstr, 0, sstr.length() + 1);
355  strncpy(cstr, sstr.c_str(), sstr.length());
356  return cstr;
357  }
358  catch(std::runtime_error& err)
359  {
360  PWARN("Error processing time64 %" PRId64 ": %s", time, err.what());
361  return nullptr;
362  }
363  catch(std::logic_error& err)
364  {
365  PWARN("Error processing time64 %" PRId64 ": %s", time, err.what());
366  return nullptr;
367  }
368 }
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 240 of file gnc-date.cpp.

241 {
242  GncDateTime gncdt;
243  auto time = static_cast<time64>(gncdt);
244  if (tbuf != NULL)
245  *tbuf = time;
246  return time;
247 }
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 1396 of file gnc-date.cpp.

1397 {
1398  struct tm tm;
1399  time64 new_time;
1400 
1401  gnc_tm_get_day_end(&tm, time_val);
1402  new_time = gnc_mktime(&tm);
1403  return new_time;
1404 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:205
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 1385 of file gnc-date.cpp.

1386 {
1387  struct tm tm;
1388  time64 new_time;
1389 
1390  gnc_tm_get_day_start(&tm, time_val);
1391  new_time = gnc_mktime(&tm);
1392  return new_time;
1393 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:205
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 1430 of file gnc-date.cpp.

1431 {
1432  struct tm tm;
1433 
1434  gnc_tm_get_day_end(&tm, time(NULL));
1435  return gnc_mktime(&tm);
1436 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:205

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

1422 {
1423  struct tm tm;
1424 
1425  gnc_tm_get_day_start(&tm, time(NULL));
1426  return gnc_mktime(&tm);
1427 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:205

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

221 {
222  try
223  {
224  normalize_struct_tm(time);
225  return static_cast<time64>(GncDateTime(*time));
226  }
227  catch(std::invalid_argument)
228  {
229  return 0;
230  }
231 }
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 98 of file gnc-date.cpp.

99 {
100  free(time);
101 }

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

1416 {
1417  gnc_tm_get_day_end(tm, time(NULL));
1418 }

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

1410 {
1411  gnc_tm_get_day_start(tm, time(NULL));
1412 }

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

536 {
537  if (dc == QOF_DATE_COMPLETION_THISYEAR ||
539  {
540  dateCompletion = dc;
541  }
542  else
543  {
544  /* hack alert - Use a neutral default. */
545  PERR("non-existent date completion set attempted. Setting current year completion as default");
546  dateCompletion = QOF_DATE_COMPLETION_THISYEAR;
547  }
548 
549  if (backmonths < 0)
550  {
551  backmonths = 0;
552  }
553  else if (backmonths > 11)
554  {
555  backmonths = 11;
556  }
557  dateCompletionBackMonths = backmonths;
558 
559  return;
560 }
#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 497 of file gnc-date.cpp.

498 {
499  return dateFormat;
500 }

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

570 {
571  switch (df)
572  {
573  case QOF_DATE_FORMAT_US:
574  return "%m/%d/%Y";
575  case QOF_DATE_FORMAT_UK:
576  return "%d/%m/%Y";
577  case QOF_DATE_FORMAT_CE:
578  return "%d.%m.%Y";
579  case QOF_DATE_FORMAT_UTC:
580  return "%Y-%m-%dT%H:%M:%SZ";
581  case QOF_DATE_FORMAT_ISO:
582  return "%Y-%m-%d";
583  case QOF_DATE_FORMAT_UNSET: // use global
584  return qof_date_format_get_string (dateFormat);
586  default:
587  break;
588  };
589  return GNC_D_FMT;
590 }
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:569
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 502 of file gnc-date.cpp.

503 {
504  if (df >= DATE_FORMAT_FIRST && df <= DATE_FORMAT_LAST)
505  {
506  prevQofDateFormat = dateFormat;
507  dateFormat = df;
508  }
509  else
510  {
511  /* hack alert - Use a neutral default. */
512  PERR("non-existent date format set attempted. Setting ISO default");
513  prevQofDateFormat = dateFormat;
514  dateFormat = QOF_DATE_FORMAT_ISO;
515  }
516 
517  return;
518 }
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 592 of file gnc-date.cpp.

593 {
594  switch (df)
595  {
596  case QOF_DATE_FORMAT_US:
597  return "%b %d, %Y";
598  case QOF_DATE_FORMAT_UK:
599  case QOF_DATE_FORMAT_CE:
600  return "%d %b %Y";
601  case QOF_DATE_FORMAT_UTC:
602  return "%Y-%m-%dT%H:%M:%SZ";
603  case QOF_DATE_FORMAT_ISO:
604  return "%Y-%b-%d";
605  case QOF_DATE_FORMAT_UNSET: // use global
606  return qof_date_text_format_get_string (dateFormat);
608  default:
609  break;
610  };
611  return GNC_D_FMT;
612 }
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:592
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 675 of file gnc-date.cpp.

676 {
677  char buff[MAX_DATE_LENGTH];
678  memset (buff, 0, sizeof (buff));
680  return g_strdup (buff);
681 }
#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:640

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

641 {
642  if (!buff) return 0;
643  try
644  {
645  GncDateTime gncdt(t);
646  std::string str = gncdt.format(qof_date_format_get_string(dateFormat));
647  strncpy(buff, str.c_str(), len);
648  if (str.length() >= len)
649  buff[len - 1] = '\0';
650  }
651  catch(std::logic_error& err)
652  {
653  PWARN("Error processing time64 %" PRId64 ": %s", t, err.what());
654  }
655  catch(std::runtime_error& err)
656  {
657  PWARN("Error processing time64 %" PRId64 ": %s", t, err.what());
658  }
659  return strlen(buff);
660 }
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:569

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

664 {
665  GDate date;
666  g_date_clear (&date, 1);
667  date = *gd;
668  return qof_print_date_dmy_buff( buf, len,
669  g_date_get_day(&date),
670  g_date_get_month(&date),
671  g_date_get_year(&date) );
672 }
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 990 of file gnc-date.cpp.

991 {
992  return qof_scan_date_internal(buff, day, month, year, dateFormat);
993 }

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

1127 {
1128  gsize convlen, retval;
1129  gchar *convbuf;
1130 
1131  g_return_val_if_fail(buf, 0);
1132  g_return_val_if_fail(max > 0, 0);
1133  g_return_val_if_fail(format, 0);
1134  g_return_val_if_fail(tm, 0);
1135 
1136  convbuf = qof_format_time(format, tm);
1137  if (!convbuf)
1138  {
1139  buf[0] = '\0';
1140  return 0;
1141  }
1142 
1143  convlen = strlen(convbuf);
1144 
1145  if (max <= convlen)
1146  {
1147  /* Ensure only whole characters are copied into the buffer. */
1148  gchar *end = g_utf8_find_prev_char(convbuf, convbuf + max);
1149  g_assert(end != NULL);
1150  convlen = end - convbuf;
1151 
1152  /* Return 0 because the buffer isn't large enough. */
1153  retval = 0;
1154  }
1155  else
1156  {
1157  retval = convlen;
1158  }
1159 
1160  memcpy(buf, convbuf, convlen);
1161  buf[convlen] = '\0';
1162  g_free(convbuf);
1163 
1164  return retval;
1165 }

◆ timespec_now()

Timespec timespec_now ( void  )

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

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

1301 {
1302  Timespec ts;
1303  ts.tv_sec = gnc_time(NULL);
1304  ts.tv_nsec = 0;
1305  return ts;
1306 }
time64 gnc_time(time64 *tbuf)
get the current local time
Definition: gnc-date.cpp:240

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

470 {
471  struct tm tm;
472  Timespec retval;
473  time64 t_secs = t.tv_sec + (t.tv_nsec / NANOS_PER_SECOND);
474  gnc_localtime_r(&t_secs, &tm);
475  gnc_tm_set_day_middle(&tm);
476  retval.tv_sec = gnc_mktime(&tm);
477  retval.tv_nsec = 0;
478  return retval;
479 }
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:116
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:205
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 75 of file gnc-date.cpp.