GnuCash  3.1-100-geb67bab
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...
 
time64 time64CanonicalDayTime (time64 t)
 
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 time64_to_gdate (time64 t)
 
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.
 
time64 gdate_to_time64 (GDate d)
 Turns a GDate into a time64, returning the first second of the day.
 
time64 gnc_dmy2time64 (gint day, gint month, gint year)
 Convert a day, month, and year to a time64, returning the first second of the day.
 
time64 gnc_dmy2time64_neutral (gint day, gint month, gint year)
 
time64 gnc_dmy2time64_end (gint day, gint month, gint year)
 
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...
 
time64 gnc_iso8601_to_time64_gmt (const gchar *)
 The gnc_iso8601_to_time64_gmt() routine converts an ISO-8601 style date/time string to time64. More...
 
gchar * gnc_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...
 
gchar * gnc_time64_to_iso8601_buff (time64, char *buff)
 
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 1014 of file gnc-date.cpp.

1015 {
1016  static char locale_separator = '\0';
1017 
1018  switch (dateFormat)
1019  {
1020  case QOF_DATE_FORMAT_CE:
1021  return '.';
1022  case QOF_DATE_FORMAT_ISO:
1023  case QOF_DATE_FORMAT_UTC:
1024  return '-';
1025  case QOF_DATE_FORMAT_US:
1026  case QOF_DATE_FORMAT_UK:
1027  default:
1028  return '/';
1030  if (locale_separator != '\0')
1031  return locale_separator;
1032  else
1033  {
1034  /* Make a guess */
1035  gchar string[256];
1036  struct tm tm;
1037  time64 secs;
1038  gchar *s;
1039 
1040  secs = gnc_time (NULL);
1041  gnc_localtime_r(&secs, &tm);
1042  auto normalized_fmt =
1043  normalize_format(qof_date_format_get_string(dateFormat));
1044  qof_strftime(string, sizeof(string), normalized_fmt.c_str(), &tm);
1045 
1046  for (s = string; *s != '\0'; s++)
1047  if (!isdigit(*s))
1048  return (locale_separator = *s);
1049  }
1050  break;
1051  }
1052  return '\0';
1053 }
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
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:586
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:1144
time64 gnc_time(time64 *tbuf)
get the current local time
Definition: gnc-date.cpp:248
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 242 of file gnc-date.cpp.

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

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

500 {
501  static int last_day_of_month[2][12] =
502  {
503  /* non leap */ {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
504  /* leap */ {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
505  };
506 
507  /* Is this a leap year? */
508  if (year % 2000 == 0) return last_day_of_month[1][month];
509  if (year % 400 == 0 ) return last_day_of_month[0][month];
510  if (year % 4 == 0 ) return last_day_of_month[1][month];
511  return last_day_of_month[0][month];
512 }

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

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

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

259 {
260  return (double)secs1 - (double)secs2;
261 }

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

1504 {
1505  struct tm my_tm;
1506  int i;
1507 
1508  memset(buf, 0, buf_len);
1509  memset(&my_tm, 0, sizeof(struct tm));
1510  my_tm.tm_wday = dow;
1511  i = qof_strftime(buf, buf_len, "%a", &my_tm);
1512  buf[i] = 0;
1513 }
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:1144

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

1390 {
1391  GncDate gncd;
1392  auto ymd = gncd.year_month_day();
1393  auto month = static_cast<GDateMonth>(ymd.month);
1394  auto result = g_date_new_dmy (ymd.day, month, ymd.year);
1395  g_assert(g_date_valid (result));
1396  return result;
1397 }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 1560 of file gnc-date.cpp.

1561 {
1562  return (g_date_compare( (GDate*)gda, (GDate*)gdb ) == 0 ? TRUE : FALSE);
1563 }

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

1567 {
1568  gint val = (g_date_get_year( (GDate*)gd ) * 10000)
1569  + (g_date_get_month( (GDate*)gd ) * 100)
1570  + g_date_get_day( (GDate*)gd );
1571  return g_int_hash( &val );
1572 }

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

1769 {
1770  GDate temp;
1771  gboolean new_fy;
1772 
1773  g_return_if_fail(date);
1774  g_return_if_fail(fy_end);
1775 
1776  /* Compute the FY end that occurred this CY */
1777  temp = *fy_end;
1778  g_date_set_year(&temp, g_date_get_year(date));
1779 
1780  /* Has it already passed? */
1781  new_fy = (g_date_compare(date, &temp) > 0);
1782 
1783  /* Set end date */
1784  *date = temp;
1785  if (new_fy)
1786  g_date_add_years(date, 1);
1787 }

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

1745 {
1746  GDate temp;
1747  gboolean new_fy;
1748 
1749  g_return_if_fail(date);
1750  g_return_if_fail(fy_end);
1751 
1752  /* Compute the FY end that occurred this CY */
1753  temp = *fy_end;
1754  g_date_set_year(&temp, g_date_get_year(date));
1755 
1756  /* Has it already passed? */
1757  new_fy = (g_date_compare(date, &temp) > 0);
1758 
1759  /* Set start date */
1760  *date = temp;
1761  g_date_add_days(date, 1);
1762  if (!new_fy)
1763  g_date_subtract_years(date, 1);
1764 }

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

1627 {
1628  /* First set the start of next month. */
1629  g_date_set_day(date, 1);
1630  g_date_add_months(date, 1);
1631 
1632  /* Then back up one day */
1633  g_date_subtract_days(date, 1);
1634 }

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

1614 {
1615  g_date_set_day(date, 1);
1616 }

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

1803 {
1804  g_return_if_fail(date);
1805  g_return_if_fail(fy_end);
1806 
1807  gnc_gdate_set_fiscal_year_end(date, fy_end);
1808  g_date_subtract_years(date, 1);
1809 }
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:1767

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

1792 {
1793  g_return_if_fail(date);
1794  g_return_if_fail(fy_end);
1795 
1796  gnc_gdate_set_fiscal_year_start(date, fy_end);
1797  g_date_subtract_years(date, 1);
1798 }
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:1743

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

1659 {
1660  /* This will correctly handle the varying month lengths */
1661  g_date_set_day(date, 1);
1662  g_date_subtract_days(date, 1);
1663 }

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

1645 {
1646  g_date_set_day(date, 1);
1647  g_date_subtract_months(date, 1);
1648 }

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

1705 {
1707  g_date_subtract_months(date, 3);
1708 }
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:1681

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

1698 {
1700  g_date_subtract_months(date, 3);
1701 }
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:1668

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

1735 {
1736  gnc_gdate_set_year_end(date);
1737  g_date_subtract_years(date, 1);
1738 }
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:1720

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

1728 {
1730  g_date_subtract_years(date, 1);
1731 }
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:1713

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

1682 {
1683  gint months;
1684 
1685  /* Set the date to the first day of the specified month. */
1686  g_date_set_day(date, 1);
1687 
1688  /* Add 1-3 months to get the first day of the next quarter.*/
1689  months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
1690  g_date_add_months(date, 3 - months);
1691 
1692  /* Now back up one day */
1693  g_date_subtract_days(date, 1);
1694 }

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

1669 {
1670  gint months;
1671 
1672  /* Set the date to the first day of the specified month. */
1673  g_date_set_day(date, 1);
1674 
1675  /* Back up 0-2 months */
1676  months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
1677  g_date_subtract_months(date, months);
1678 }

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

1408 {
1409  struct tm tm;
1410  gnc_localtime_r(&time, &tm);
1411  g_date_set_dmy (gd, tm.tm_mday,
1412  static_cast<GDateMonth>(tm.tm_mon + 1),
1413  tm.tm_year + 1900);
1414 }
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 1399 of file gnc-date.cpp.

1400 {
1401  GDate *today = gnc_g_date_new_today ();
1402  g_date_set_julian (gd, g_date_get_julian (today));
1403  g_date_free (today);
1404 }
GDate * gnc_g_date_new_today()
Returns a newly allocated date of the current clock time, taken from time(2).
Definition: gnc-date.cpp:1389

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

1721 {
1722  g_date_set_month(date, G_DATE_DECEMBER);
1723  g_date_set_day(date, 31);
1724 }

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

1714 {
1715  g_date_set_month(date, G_DATE_JANUARY);
1716  g_date_set_day(date, 1);
1717 }

◆ 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_time64_gmt()

time64 gnc_iso8601_to_time64_gmt ( const gchar *  )

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

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  *time = static_cast<struct tm>(gncdt);
212  return static_cast<time64>(gncdt);
213  }
214  catch(std::invalid_argument&)
215  {
216  return 0;
217  }
218 }
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 701 of file gnc-date.cpp.

702 {
703  static char buff[MAX_DATE_LENGTH];
704  time64 t;
705 
706  memset (buff, 0, sizeof (buff));
707  t = ts.tv_sec + (time64)(ts.tv_nsec / 1000000000.0);
708 
710 
711  return buff;
712 }
#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:657

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

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

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

249 {
250  GncDateTime gncdt;
251  auto time = static_cast<time64>(gncdt);
252  if (tbuf != NULL)
253  *tbuf = time;
254  return time;
255 }
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 1460 of file gnc-date.cpp.

1461 {
1462  struct tm tm;
1463  time64 new_time;
1464 
1465  gnc_tm_get_day_end(&tm, time_val);
1466  new_time = gnc_mktime(&tm);
1467  return new_time;
1468 }
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 1449 of file gnc-date.cpp.

1450 {
1451  struct tm tm;
1452  time64 new_time;
1453 
1454  gnc_tm_get_day_start(&tm, time_val);
1455  new_time = gnc_mktime(&tm);
1456  return new_time;
1457 }
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 1494 of file gnc-date.cpp.

1495 {
1496  struct tm tm;
1497 
1498  gnc_tm_get_day_end(&tm, time(NULL));
1499  return gnc_mktime(&tm);
1500 }
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 1485 of file gnc-date.cpp.

1486 {
1487  struct tm tm;
1488 
1489  gnc_tm_get_day_start(&tm, time(NULL));
1490  return gnc_mktime(&tm);
1491 }
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 221 of file gnc-date.cpp.

222 {
223  try
224  {
225  normalize_struct_tm(time);
226  GncDateTime gncdt(*time);
227  *time = static_cast<struct tm>(gncdt);
228  time->tm_sec -= gncdt.offset();
229  normalize_struct_tm(time);
230 #ifdef HAVE_STRUcT_TM_GMTOFF
231  time->tm_gmtoff = 0;
232 #endif
233  return static_cast<time64>(gncdt) - gncdt.offset();
234  }
235  catch(std::invalid_argument&)
236  {
237  return 0;
238  }
239 }
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 1479 of file gnc-date.cpp.

1480 {
1481  gnc_tm_get_day_end(tm, time(NULL));
1482 }

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

1474 {
1475  gnc_tm_get_day_start(tm, time(NULL));
1476 }

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

553 {
554  if (dc == QOF_DATE_COMPLETION_THISYEAR ||
556  {
557  dateCompletion = dc;
558  }
559  else
560  {
561  /* hack alert - Use a neutral default. */
562  PERR("non-existent date completion set attempted. Setting current year completion as default");
563  dateCompletion = QOF_DATE_COMPLETION_THISYEAR;
564  }
565 
566  if (backmonths < 0)
567  {
568  backmonths = 0;
569  }
570  else if (backmonths > 11)
571  {
572  backmonths = 11;
573  }
574  dateCompletionBackMonths = backmonths;
575 
576  return;
577 }
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:242
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 514 of file gnc-date.cpp.

515 {
516  return dateFormat;
517 }

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

587 {
588  switch (df)
589  {
590  case QOF_DATE_FORMAT_US:
591  return "%m/%d/%Y";
592  case QOF_DATE_FORMAT_UK:
593  return "%d/%m/%Y";
594  case QOF_DATE_FORMAT_CE:
595  return "%d.%m.%Y";
596  case QOF_DATE_FORMAT_UTC:
597  return "%Y-%m-%dT%H:%M:%SZ";
598  case QOF_DATE_FORMAT_ISO:
599  return "%Y-%m-%d";
600  case QOF_DATE_FORMAT_UNSET: // use global
601  return qof_date_format_get_string (dateFormat);
603  default:
604  break;
605  };
606  return GNC_D_FMT;
607 }
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:586
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 519 of file gnc-date.cpp.

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

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

610 {
611  switch (df)
612  {
613  case QOF_DATE_FORMAT_US:
614  return "%b %d, %Y";
615  case QOF_DATE_FORMAT_UK:
616  case QOF_DATE_FORMAT_CE:
617  return "%d %b %Y";
618  case QOF_DATE_FORMAT_UTC:
619  return "%Y-%m-%dT%H:%M:%SZ";
620  case QOF_DATE_FORMAT_ISO:
621  return "%Y-%b-%d";
622  case QOF_DATE_FORMAT_UNSET: // use global
623  return qof_date_text_format_get_string (dateFormat);
625  default:
626  break;
627  };
628  return GNC_D_FMT;
629 }
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:609
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 692 of file gnc-date.cpp.

693 {
694  char buff[MAX_DATE_LENGTH];
695  memset (buff, 0, sizeof (buff));
697  return g_strdup (buff);
698 }
#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:657

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

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

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

681 {
682  GDate date;
683  g_date_clear (&date, 1);
684  date = *gd;
685  return qof_print_date_dmy_buff( buf, len,
686  g_date_get_day(&date),
687  g_date_get_month(&date),
688  g_date_get_year(&date) );
689 }
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 1006 of file gnc-date.cpp.

1007 {
1008  return qof_scan_date_internal(buff, day, month, year, dateFormat);
1009 }

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

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

◆ timespec_now()

Timespec timespec_now ( void  )

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

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

1344 {
1345  Timespec ts;
1346  ts.tv_sec = gnc_time(NULL);
1347  ts.tv_nsec = 0;
1348  return ts;
1349 }
time64 gnc_time(time64 *tbuf)
get the current local time
Definition: gnc-date.cpp:248

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

478 {
479  struct tm tm;
480  Timespec retval;
481  time64 t_secs = t.tv_sec + (t.tv_nsec / NANOS_PER_SECOND);
482  gnc_localtime_r(&t_secs, &tm);
483  gnc_tm_set_day_middle(&tm);
484  retval.tv_sec = gnc_mktime(&tm);
485  retval.tv_nsec = 0;
486  return retval;
487 }
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.