GnuCash  2.6.99
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
 

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 144 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 117 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 83 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 136 of file gnc-date.h.

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

◆ 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 120 of file gnc-date.h.

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

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

1013 {
1014  static char locale_separator = '\0';
1015 
1016  switch (dateFormat)
1017  {
1018  case QOF_DATE_FORMAT_CE:
1019  return '.';
1020  case QOF_DATE_FORMAT_ISO:
1021  case QOF_DATE_FORMAT_UTC:
1022  return '-';
1023  case QOF_DATE_FORMAT_US:
1024  case QOF_DATE_FORMAT_UK:
1025  default:
1026  return '/';
1028  if (locale_separator != '\0')
1029  return locale_separator;
1030  else
1031  {
1032  /* Make a guess */
1033  gchar string[256];
1034  struct tm tm;
1035  time64 secs;
1036  gchar *s;
1037 
1038  secs = gnc_time (NULL);
1039  gnc_localtime_r(&secs, &tm);
1040  qof_strftime(string, sizeof(string), GNC_D_FMT, &tm);
1041 
1042  for (s = string; *s != '\0'; s++)
1043  if (!isdigit(*s))
1044  return (locale_separator = *s);
1045  }
1046  break;
1047  }
1048 
1049  return '\0';
1050 }
ISO: yyyy-mm-dd.
Definition: gnc-date.h:125
Continental Europe: dd.mm.yyyy.
Definition: gnc-date.h:124
struct tm * gnc_localtime_r(const time64 *secs, struct tm *time)
fill out a time struct from a 64-bit time value adjusted for the current time zone.
Definition: gnc-date.cpp:126
UTC: 2004-12-12T23:39:11Z.
Definition: gnc-date.h:127
gsize qof_strftime(gchar *buf, gsize max, const gchar *format, const struct tm *tm)
qof_strftime calls qof_format_time to print a given time and afterwards tries to put the result into ...
Definition: gnc-date.cpp:1141
time64 gnc_time(time64 *tbuf)
get the current local time
Definition: gnc-date.cpp:250
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:83
Take from locale information.
Definition: gnc-date.h:126
Britain: dd/mm/yyyy.
Definition: gnc-date.h:123
United states: mm/dd/yyyy.
Definition: gnc-date.h:122

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

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

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

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

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

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

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

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

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

1461 {
1462  struct tm my_tm;
1463  int i;
1464 
1465  memset(buf, 0, buf_len);
1466  memset(&my_tm, 0, sizeof(struct tm));
1467  my_tm.tm_wday = dow;
1468  i = qof_strftime(buf, buf_len, "%a", &my_tm);
1469  buf[i] = 0;
1470 }
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:1141

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

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

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

1368 {
1369  struct tm tm;
1370  gnc_localtime_r(&time, &tm);
1371  g_date_set_dmy (gd, tm.tm_mday,
1372  static_cast<GDateMonth>(tm.tm_mon + 1),
1373  tm.tm_year + 1900);
1374 
1375 }
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:126

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

1360 {
1361  GDate *today = gnc_g_date_new_today ();
1362  g_date_set_julian (gd, g_date_get_julian (today));
1363  g_date_free (today);
1364 }
GDate * gnc_g_date_new_today()
Returns a newly allocated date of the current clock time, taken from time(2).
Definition: gnc-date.cpp:1349

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

199 {
200  try
201  {
202  auto time = static_cast<struct tm*>(calloc(1, sizeof(struct tm)));
203  GncDateTime gncdt(*secs);
204  *time = gncdt.utc_tm();
205  return time;
206  }
207  catch(std::invalid_argument)
208  {
209  return NULL;
210  }
211 
212 }
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 114 of file gnc-date.cpp.

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

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

127 {
128  try
129  {
130  *time = static_cast<struct tm>(GncDateTime(*secs));
131  return time;
132  }
133  catch(std::invalid_argument)
134  {
135  return NULL;
136  }
137 }
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 215 of file gnc-date.cpp.

216 {
217  try
218  {
219  normalize_struct_tm (time);
220  GncDateTime gncdt(*time);
221  return static_cast<time64>(gncdt) - gncdt.offset();
222  }
223  catch(std::invalid_argument)
224  {
225  return 0;
226  }
227 }
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:83

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

699 {
700  static char buff[MAX_DATE_LENGTH];
701  time64 t;
702 
703  memset (buff, 0, sizeof (buff));
704  t = ts.tv_sec + (time64)(ts.tv_nsec / 1000000000.0);
705 
707 
708  return buff;
709 }
#define MAX_DATE_LENGTH
The maximum length of a string created by the date printers.
Definition: gnc-date.h:106
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:83
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:653

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

359 {
360  try
361  {
362  GncDateTime gncdt(time);
363  auto sstr = gncdt.format(format);
364  //ugly C allocation so that the ptr can be freed at the other end
365  char* cstr = static_cast<char*>(malloc(sstr.length() + 1));
366  memset(cstr, 0, sstr.length() + 1);
367  strncpy(cstr, sstr.c_str(), sstr.length());
368  return cstr;
369  }
370  catch(std::runtime_error& err)
371  {
372  PWARN("Error processing time64 %" PRId64 ": %s", time, err.what());
373  return nullptr;
374  }
375  catch(std::logic_error& err)
376  {
377  PWARN("Error processing time64 %" PRId64 ": %s", time, err.what());
378  return nullptr;
379  }
380 }
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 250 of file gnc-date.cpp.

251 {
252  GncDateTime gncdt;
253  auto time = static_cast<time64>(gncdt);
254  if (tbuf != NULL)
255  *tbuf = time;
256  return time;
257 }
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:83

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

1417 {
1418  struct tm tm;
1419  time64 new_time;
1420 
1421  gnc_tm_get_day_end(&tm, time_val);
1422  new_time = gnc_mktime(&tm);
1423  return new_time;
1424 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:215
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:83

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

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

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

1452 {
1453  struct tm tm;
1454 
1455  gnc_tm_get_day_end(&tm, time(NULL));
1456  return gnc_mktime(&tm);
1457 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:215

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

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

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

231 {
232  try
233  {
234  normalize_struct_tm(time);
235  return static_cast<time64>(GncDateTime(*time));
236  }
237  catch(std::invalid_argument)
238  {
239  return 0;
240  }
241 }
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:83

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

109 {
110  free(time);
111 }

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

1437 {
1438  gnc_tm_get_day_end(tm, time(NULL));
1439 }

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

1431 {
1432  gnc_tm_get_day_start(tm, time(NULL));
1433 }

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

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

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

511 {
512  return dateFormat;
513 }

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

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

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

516 {
517  if (df >= DATE_FORMAT_FIRST && df <= DATE_FORMAT_LAST)
518  {
519  prevQofDateFormat = dateFormat;
520  dateFormat = df;
521  }
522  else
523  {
524  /* hack alert - Use a neutral default. */
525  PERR("non-existent date format set attempted. Setting ISO default");
526  prevQofDateFormat = dateFormat;
527  dateFormat = QOF_DATE_FORMAT_ISO;
528  }
529 
530  return;
531 }
ISO: yyyy-mm-dd.
Definition: gnc-date.h:125
#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 605 of file gnc-date.cpp.

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

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

690 {
691  char buff[MAX_DATE_LENGTH];
692  memset (buff, 0, sizeof (buff));
694  return g_strdup (buff);
695 }
#define MAX_DATE_LENGTH
The maximum length of a string created by the date printers.
Definition: gnc-date.h:106
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:653

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

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

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

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

1005 {
1006  return qof_scan_date_internal(buff, day, month, year, dateFormat);
1007 }

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

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

◆ timespec_now()

Timespec timespec_now ( void  )

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

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

1319 {
1320  Timespec ts;
1321  ts.tv_sec = gnc_time(NULL);
1322  ts.tv_nsec = 0;
1323  return ts;
1324 }
time64 gnc_time(time64 *tbuf)
get the current local time
Definition: gnc-date.cpp:250

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

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

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