GnuCash  5.6-150-g038405b370+
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  Time64
 

Macros

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

Typedefs

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

Enumerations

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

Functions

struct tm * gnc_localtime (const time64 *secs)
 fill out a time struct from a 64-bit time value. More...
 
struct tm * gnc_localtime_r (const time64 *secs, struct tm *time)
 fill out a time struct from a 64-bit time value adjusted for the current time zone. More...
 
struct tm * gnc_gmtime (const time64 *secs)
 fill out a time struct from a 64-bit time value More...
 
gint gnc_start_of_week (void)
 returns an integer corresponding to locale start of week More...
 
time64 gnc_mktime (struct tm *time)
 calculate seconds from the epoch given a time struct More...
 
time64 gnc_timegm (struct tm *time)
 calculate seconds from the epoch given a time struct More...
 
gchar * gnc_ctime (const time64 *secs)
 Return a string representation of a date from a 64-bit time value. More...
 
time64 gnc_time (time64 *tbuf)
 get the current time More...
 
gdouble gnc_difftime (const time64 secs1, const time64 secs2)
 Find the difference in seconds between two time values (deprecated) More...
 
void gnc_tm_free (struct tm *time)
 free a struct tm* created with gnc_localtime() or gnc_gmtime() More...
 
time64 time64CanonicalDayTime (time64 t)
 convert a time64 on a certain day (localtime) to the time64 representing midday on that day. More...
 
time64 gdate_to_time64 (GDate d)
 Turns a GDate into a time64, returning the first second of the day.
 
time64 gnc_dmy2time64 (gint day, gint month, gint year)
 Convert a day, month, and year to a time64, returning the first second of the day.
 
time64 gnc_dmy2time64_neutral (gint day, gint month, gint year)
 Converts a day, month, and year to a time64 representing 11:00:00 UTC 11:00:00 UTC falls on the same time in almost all timezones, the exceptions being the +13, +14, and -12 timezones used by countries along the International Date Line. More...
 
time64 gnc_dmy2time64_end (gint day, gint month, gint year)
 Same as gnc_dmy2time64, but last second of the day.
 
time64 gnc_iso8601_to_time64_gmt (const gchar *)
 The gnc_iso8601_to_time64_gmt() routine converts an ISO-8601 style date/time string to time64. More...
 
gchar * gnc_time64_to_iso8601_buff (time64, char *buff)
 The gnc_time64_to_iso8601_buff() routine takes the input UTC time64 value and prints it as an ISO-8601 style string. More...
 

Variables

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

GValue

GType time64_get_type (void)
 
#define GNC_TYPE_TIME64   (time64_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...
 

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.

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_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 119 of file gnc-date.h.

Typedef Documentation

◆ time64

typedef gint64 time64

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

See https://en.wikipedia.org/wiki/Year_2038_problem

For practical reasons – as not all have made the transition to 64-bit – we define our own 64-bit time type.

Definition at line 87 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 138 of file gnc-date.h.

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

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

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

Function Documentation

◆ 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:00 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 256 of file gnc-date.cpp.

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

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

gdouble gnc_difftime ( const time64  secs1,
const time64  secs2 
)

Find the difference in seconds between two time values (deprecated)

Parameters
secs1The first time value, in Seconds since 00:00:00 UTC 01 January 1970 (negative values are seconds before that moment)
secs2The second time value, in Seconds since 00:00:00 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 272 of file gnc-date.cpp.

273 {
274  PWARN ("gnc_difftime is deprecated");
275  return (double)secs1 - (double)secs2;
276 }
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250

◆ gnc_dmy2time64_neutral()

time64 gnc_dmy2time64_neutral ( gint  day,
gint  month,
gint  year 
)

Converts a day, month, and year to a time64 representing 11:00:00 UTC 11:00:00 UTC falls on the same time in almost all timezones, the exceptions being the +13, +14, and -12 timezones used by countries along the International Date Line.

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

1299 {
1300  GDate* rv = g_date_new ();
1301  gnc_gdate_set_today (rv);
1302  return rv;
1303 }
void gnc_gdate_set_today(GDate *gd)
Set a GDate to the current day.
Definition: gnc-date.cpp:1306

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

1315 {
1316  struct tm tm;
1317  gnc_localtime_r(&time, &tm);
1318  g_date_set_dmy (gd, tm.tm_mday,
1319  static_cast<GDateMonth>(tm.tm_mon + 1),
1320  tm.tm_year + 1900);
1321 }
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:115

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

1307 {
1308  g_return_if_fail (gd != nullptr);
1309  auto ymd = GncDate().year_month_day();
1310  g_date_set_dmy (gd, ymd.day, static_cast<GDateMonth>(ymd.month), ymd.year);
1311 }
gnc_ymd year_month_day() const
Get the year, month, and day from the date as a gnc_ymd.
GnuCash Date class.

◆ 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:00 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 178 of file gnc-date.cpp.

179 {
180  try
181  {
182  GncDateTime gncdt(*secs);
183  auto time = static_cast<struct tm*>(calloc(1, sizeof(struct tm)));
184  *time = gncdt.utc_tm();
185  return time;
186  }
187  catch(std::invalid_argument&)
188  {
189  return nullptr;
190  }
191 
192 }
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:00 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 103 of file gnc-date.cpp.

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

◆ 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:00 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 115 of file gnc-date.cpp.

116 {
117  try
118  {
119  *time = static_cast<struct tm>(GncDateTime(*secs));
120  return time;
121  }
122  catch(std::invalid_argument&)
123  {
124  return nullptr;
125  }
126 }
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:00 UTC 01 January 1970 (negative values are seconds before that moment).

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

220 {
221  try
222  {
223  normalize_struct_tm (time);
224  GncDateTime gncdt(*time);
225  *time = static_cast<struct tm>(gncdt);
226  return static_cast<time64>(gncdt);
227  }
228  catch(std::invalid_argument&)
229  {
230  return 0;
231  }
232 }
GnuCash DateTime class.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87

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

371 {
372  try
373  {
374  GncDateTime gncdt(time);
375  auto sstr = gncdt.format(format);
376  //ugly C allocation so that the ptr can be freed at the other end
377  char* cstr = static_cast<char*>(malloc(sstr.length() + 1));
378  memset(cstr, 0, sstr.length() + 1);
379  strncpy(cstr, sstr.c_str(), sstr.length());
380  return cstr;
381  }
382  catch(std::runtime_error& err)
383  {
384  PWARN("Error processing time64 %" PRId64 ": %s", time, err.what());
385  return nullptr;
386  }
387  catch(std::logic_error& err)
388  {
389  PWARN("Error processing time64 %" PRId64 ": %s", time, err.what());
390  return nullptr;
391  }
392 }
GnuCash DateTime class.
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250

◆ gnc_start_of_week()

gint gnc_start_of_week ( void  )

returns an integer corresponding to locale start of week

Returns
An integer 1=Sunday, 2=Monday etc. If error, return 0.

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

196 {
197  /* icu's day of week is 1 based. Using 0 here to mean unset or error while setting */
198  static int cached_result = 0;
199 
200  if (!cached_result)
201  {
202  UErrorCode err = U_ZERO_ERROR;
203  auto cal = icu::Calendar::createInstance (err);
204  if (!cal)
205  {
206  PERR("ICU error: %s\n", u_errorName (err));
207  return 0;
208  }
209 
210  /* 1 for sunday, 2 for monday, etc. */
211  cached_result = cal->getFirstDayOfWeek (err);
212  delete cal;
213  }
214 
215  return cached_result;
216 }
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244

◆ gnc_time()

time64 gnc_time ( time64 tbuf)

get the current time

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

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

263 {
264  GncDateTime gncdt;
265  auto time = static_cast<time64>(gncdt);
266  if (tbuf != nullptr)
267  *tbuf = time;
268  return time;
269 }
GnuCash DateTime class.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87

◆ gnc_time64_to_iso8601_buff()

gchar* gnc_time64_to_iso8601_buff ( time64  ,
char *  buff 
)

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

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.

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

1214 {
1215  if (!buff) return nullptr;
1216  try
1217  {
1218  GncDateTime gncdt(time);
1219  auto sstr = gncdt.format_iso8601();
1220 
1221  memset(buff, 0, sstr.length() + 1);
1222  strncpy(buff, sstr.c_str(), sstr.length());
1223  return buff + sstr.length();
1224  }
1225  catch(std::logic_error& err)
1226  {
1227  PWARN("Error processing time64 %" PRId64 ": %s", time, err.what());
1228  return buff;
1229  }
1230  catch(std::runtime_error& err)
1231  {
1232  PWARN("Error processing time64 %" PRId64 ": %s", time, err.what());
1233  return buff;
1234  }
1235 }
GnuCash DateTime class.
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250

◆ 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:00 UTC 01 January 1970 (negative values are seconds before that moment).

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

236 {
237  try
238  {
239  normalize_struct_tm(time);
240  GncDateTime gncdt(*time);
241  *time = static_cast<struct tm>(gncdt);
242  time->tm_sec -= gncdt.offset();
243  normalize_struct_tm(time);
244 #ifdef HAVE_STRUcT_TM_GMTOFF
245  time->tm_gmtoff = 0;
246 #endif
247  return static_cast<time64>(gncdt) - gncdt.offset();
248  }
249  catch(std::invalid_argument&)
250  {
251  return 0;
252  }
253 }
GnuCash DateTime class.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87

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

98 {
99  free(time);
100 }

◆ time64CanonicalDayTime()

time64 time64CanonicalDayTime ( time64  t)

convert a time64 on a certain day (localtime) to the time64 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 time64.

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

405 {
406  struct tm tm;
407  gnc_localtime_r(&t, &tm);
408  gnc_tm_set_day_middle(&tm);
409  return gnc_mktime (&tm);
410 }
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:115
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition: gnc-date.cpp:219

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