GnuCash c935c2f+
Loading...
Searching...
No Matches
Files | Data Structures | Macros | Typedefs | Enumerations | Enumerator | 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.
 
#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.
 

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

Functions

struct tm * gnc_localtime (const time64 *secs)
 fill out a time struct from a 64-bit time value.
 
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.
 
struct tm * gnc_gmtime (const time64 *secs)
 fill out a time struct from a 64-bit time value
 
gint gnc_start_of_week (void)
 returns an integer corresponding to locale start of week
 
time64 gnc_mktime (struct tm *time)
 calculate seconds from the epoch given a time struct
 
time64 gnc_timegm (struct tm *time)
 calculate seconds from the epoch given a time struct
 
gchar * gnc_ctime (const time64 *secs)
 Return a string representation of a date from a 64-bit time value.
 
time64 gnc_time (time64 *tbuf)
 get the current time
 
gdouble gnc_difftime (const time64 secs1, const time64 secs2)
 Find the difference in seconds between two time values (deprecated)
 
void gnc_tm_free (struct tm *time)
 free a struct tm* created with gnc_localtime() or gnc_gmtime()
 
time64 time64CanonicalDayTime (time64 t)
 convert a time64 on a certain day (localtime) to the time64 representing midday on that 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)
 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.
 
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.
 
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.
 

Variables

time64 Time64::t
 
const char * gnc_default_strftime_date_format
 The default date format for use with strftime.
 

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.
 
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.
 
char * gnc_print_time64 (time64 time, const char *format)
 print a time64 as a date string per format
 

GDate time64 setters

GDate * gnc_g_date_new_today (void)
 Returns a newly allocated date of the current clock time, taken from time(2).
 
void gnc_gdate_set_today (GDate *gd)
 Set a GDate to the current day.
 
void gnc_gdate_set_time64 (GDate *gd, time64 time)
 Set a GDate to a time64.
 

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.
 
void qof_date_format_set (QofDateFormat df)
 The qof_date_format_set() routine sets date format to one of US, UK, CE, OR ISO.
 
const gchar * qof_date_format_get_string (QofDateFormat df)
 This function returns a strftime formatting string for printing an all numeric date (e.g.
 
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.
 
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).
 
gchar dateSeparator (void)
 dateSeparator Return the field separator for the current date format
 

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.
 
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
 
size_t qof_print_date_buff (char *buff, size_t buflen, time64 secs)
 Convenience: calls through to qof_print_date_dmy_buff().
 
size_t qof_print_gdate (char *buf, size_t bufflen, const GDate *gd)
 Convenience; calls through to qof_print_date_dmy_buff().
 
char * qof_print_date (time64 secs)
 Convenience; calls through to qof_print_date_dmy_buff().
 
gchar * gnc_date_interval_format (time64 from_date, time64 to_date)
 
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.
 

Date Start/End Adjustment routines

Given a time value, adjust it to be the beginning or end of that day.

void gnc_tm_set_day_neutral (struct tm *tm)
 The gnc_tm_set_day_neutral() inline routine will set the appropriate fields in the struct tm to indicate 10:59am 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 first second of that day.
 
time64 gnc_time64_get_day_neutral (time64 time_val)
 The gnc_time64_get_day_neutral() routine will take the given time in seconds and adjust it to 10:59:00Z of that day.
 
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.
 
int gnc_date_get_last_mday (int month, int year)
 Get the numerical last date of the month.
 

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.
 
void gnc_tm_get_today_neutral (struct tm *tm)
 The gnc_tm_get_today_start() routine takes a pointer to a struct tm and fills it in with the timezone neutral time (10:59:00Z).
 
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.
 
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.
 
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.
 
char * gnc_date_timestamp (void)
 Make a timestamp in YYYYMMDDHHMMSS format.
 
void gnc_dow_abbrev (gchar *buf, int buf_len, int dow)
 Localized DOW abbreviation.
 
#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.
 
guint gnc_gdate_hash (gconstpointer gd)
 Provides a "hash" of a GDate* value; useful for using GDate*'s as GHashTable keys.
 

GDate to time64 conversions

GDate time64_to_gdate (time64 t)
 Returns the GDate in which the time64 occurs.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 

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

◆ DATE_FORMAT_FIRST

#define DATE_FORMAT_FIRST   QOF_DATE_FORMAT_US

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

◆ DATE_FORMAT_LAST

#define DATE_FORMAT_LAST   QOF_DATE_FORMAT_UTC

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

◆ GNC_TYPE_TIME64

#define GNC_TYPE_TIME64   (time64_get_type ())

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

◆ MAX_DATE_LENGTH

#define MAX_DATE_LENGTH   34

The maximum length of a string created by the date printers.

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

◆ MIN_BUF_LEN

#define MIN_BUF_LEN   10

Definition at line 613 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 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

◆ GNCDateMonthFormat

This is how to format the month, as a number, an abbreviated string, or the full name.

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

149{
150 GNCDATE_MONTH_NUMBER,
151 GNCDATE_MONTH_ABBREV,
152 GNCDATE_MONTH_NAME
GNCDateMonthFormat
This is how to format the month, as a number, an abbreviated string, or the full name.
Definition gnc-date.h:149

◆ 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:139
@ QOF_DATE_COMPLETION_THISYEAR
use current year
Definition gnc-date.h:140
@ QOF_DATE_COMPLETION_SLIDING
use sliding 12-month window
Definition gnc-date.h:141

◆ 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{
QofDateFormat
Enum for determining a date format.
Definition gnc-date.h:123
@ QOF_DATE_FORMAT_US
United states: mm/dd/yyyy.
Definition gnc-date.h:124
@ QOF_DATE_FORMAT_LOCALE
Take from locale information.
Definition gnc-date.h:128
@ QOF_DATE_FORMAT_UNSET
No Fancy Date Format, use Global.
Definition gnc-date.h:131
@ QOF_DATE_FORMAT_UTC
UTC: 2004-12-12T23:39:11Z.
Definition gnc-date.h:129
@ QOF_DATE_FORMAT_CE
Continental Europe: dd.mm.yyyy.
Definition gnc-date.h:126
@ QOF_DATE_FORMAT_UK
Britain: dd/mm/yyyy.
Definition gnc-date.h:125
@ QOF_DATE_FORMAT_CUSTOM
Used by the check printing code.
Definition gnc-date.h:130
@ QOF_DATE_FORMAT_ISO
ISO: yyyy-mm-dd.
Definition gnc-date.h:127

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

1000{
1001 static char locale_separator = '\0';
1002
1003 switch (dateFormat)
1004 {
1005 case QOF_DATE_FORMAT_CE:
1006 return '.';
1009 return '-';
1010 case QOF_DATE_FORMAT_US:
1011 case QOF_DATE_FORMAT_UK:
1012 default:
1013 return '/';
1015 if (locale_separator != '\0')
1016 return locale_separator;
1017 else
1018 {
1019 /* Make a guess */
1020 gchar string[256];
1021 struct tm tm;
1022 time64 secs;
1023 gchar *s;
1024
1025 secs = gnc_time (nullptr);
1026 gnc_localtime_r(&secs, &tm);
1027 auto normalized_fmt =
1028 normalize_format(qof_date_format_get_string(dateFormat));
1029 qof_strftime(string, sizeof(string), normalized_fmt.c_str(), &tm);
1030
1031 for (s = string; *s != '\0'; s++)
1032 if (!isdigit(*s))
1033 return (locale_separator = *s);
1034 }
1035 break;
1036 }
1037 return '\0';
1038}
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
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition gnc-date.h:87
time64 gnc_time(time64 *tbuf)
get the current time
Definition gnc-date.cpp:262
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:502
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 ...

◆ gdate_to_time64()

time64 gdate_to_time64 ( GDate  d)

Turns a GDate into a time64, returning the first second of the day.

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

1324{
1325 return gnc_dmy2time64_neutral (g_date_get_day(&d),
1326 g_date_get_month(&d),
1327 g_date_get_year(&d));
1328}

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

const gchar * gnc_date_dateformat_to_string ( QofDateFormat  format)

The string->value versions return FALSE on success and TRUE on failure.

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

282{
283 switch (format)
284 {
286 return "us";
288 return "uk";
290 return "ce";
292 return "iso";
294 return "utc";
296 return "locale";
298 return "custom";
300 return "unset";
301 default:
302 return nullptr;
303 }
304}

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

414{
415 static int last_day_of_month[12] =
416 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
417
418 g_assert(month >= 0 && month < 12);
419
420 // To be a leap year, the year number must be divisible by four,
421 // except for end-of-century years, which must be divisible by 400.
422
423 return last_day_of_month[month] +
424 (month == 1 && year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0) ?
425 1 : 0);
426}

◆ gnc_date_interval_format()

gchar * gnc_date_interval_format ( time64  from_date,
time64  to_date 
)

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

665{
666 gchar* rv = nullptr;
667
669 rv = icu_date_interval_format (from_date, to_date);
670
671 // not using locale, or icu failure
672 if (!rv)
673 {
674 gchar from_buff[MAX_DATE_LENGTH+1], to_buff[MAX_DATE_LENGTH+1];
675 qof_print_date_buff (from_buff, MAX_DATE_LENGTH, from_date);
676 qof_print_date_buff (to_buff, MAX_DATE_LENGTH, to_date);
677 rv = g_strdup_printf (gettext("%s to %s"), from_buff, to_buff);
678 }
679
680 return rv;
681}
#define MAX_DATE_LENGTH
The maximum length of a string created by the date printers.
Definition gnc-date.h:108
QofDateFormat qof_date_format_get(void)
The qof_date_format_get routine returns the date format that the date printing will use when printing...
Definition gnc-date.cpp:428
size_t qof_print_date_buff(char *buff, const size_t len, time64 t)
Convenience: calls through to qof_print_date_dmy_buff().
Definition gnc-date.cpp:574

◆ gnc_date_monthformat_to_string()

const gchar * gnc_date_monthformat_to_string ( GNCDateMonthFormat  format)

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

336{
337 //avoid UB if format is out of range
338 switch (static_cast<uint8_t>(format))
339 {
340 case GNCDATE_MONTH_NUMBER:
341 return "number";
342 case GNCDATE_MONTH_ABBREV:
343 return "abbrev";
344 case GNCDATE_MONTH_NAME:
345 return "name";
346 default:
347 return nullptr;
348 }
349}

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

1175{
1176 auto timestamp = GncDateTime::timestamp();
1177 return g_strdup(timestamp.c_str());
1178}
static std::string timestamp()
Get an undelimited string representing the current date and time.

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

1436{
1437 struct tm my_tm;
1438 int i;
1439
1440 memset(buf, 0, buf_len);
1441 memset(&my_tm, 0, sizeof(struct tm));
1442 my_tm.tm_wday = dow;
1443 i = qof_strftime(buf, buf_len, "%a", &my_tm);
1444 buf[i] = 0;
1445}

◆ 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 ();
1302 return rv;
1303}
void gnc_gdate_set_today(GDate *gd)
Set a GDate to the current day.

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

1472{
1473 return (g_date_compare( (GDate*)gda, (GDate*)gdb ) == 0 ? TRUE : FALSE);
1474}

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

1478{
1479 gint val = (g_date_get_year( (GDate*)gd ) * 10000)
1480 + (g_date_get_month( (GDate*)gd ) * 100)
1481 + g_date_get_day( (GDate*)gd );
1482 return g_int_hash( &val );
1483}

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

1676{
1677 GDate temp;
1678 gboolean new_fy;
1679
1680 g_return_if_fail(date);
1681 g_return_if_fail(fy_end);
1682
1683 /* Compute the FY end that occurred this CY */
1684 temp = *fy_end;
1685 g_date_set_year(&temp, g_date_get_year(date));
1686
1687 /* Has it already passed? */
1688 new_fy = (g_date_compare(date, &temp) > 0);
1689
1690 /* Set end date */
1691 *date = temp;
1692 if (new_fy)
1693 g_date_add_years(date, 1);
1694}

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

1652{
1653 GDate temp;
1654 gboolean new_fy;
1655
1656 g_return_if_fail(date);
1657 g_return_if_fail(fy_end);
1658
1659 /* Compute the FY end that occurred this CY */
1660 temp = *fy_end;
1661 g_date_set_year(&temp, g_date_get_year(date));
1662
1663 /* Has it already passed? */
1664 new_fy = (g_date_compare(date, &temp) > 0);
1665
1666 /* Set start date */
1667 *date = temp;
1668 g_date_add_days(date, 1);
1669 if (!new_fy)
1670 g_date_subtract_years(date, 1);
1671}

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

1538{
1539 /* First set the start of next month. */
1540 g_date_set_day(date, 1);
1541 g_date_add_months(date, 1);
1542
1543 /* Then back up one day */
1544 g_date_subtract_days(date, 1);
1545}

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

1525{
1526 g_date_set_day(date, 1);
1527}

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

1710{
1711 g_return_if_fail(date);
1712 g_return_if_fail(fy_end);
1713
1714 gnc_gdate_set_fiscal_year_end(date, fy_end);
1715 g_date_subtract_years(date, 1);
1716}
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.

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

1699{
1700 g_return_if_fail(date);
1701 g_return_if_fail(fy_end);
1702
1703 gnc_gdate_set_fiscal_year_start(date, fy_end);
1704 g_date_subtract_years(date, 1);
1705}
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.

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

1570{
1571 /* This will correctly handle the varying month lengths */
1572 g_date_set_day(date, 1);
1573 g_date_subtract_days(date, 1);
1574}

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

1556{
1557 g_date_set_day(date, 1);
1558 g_date_subtract_months(date, 1);
1559}

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

Parameters
dateThe GDate to modify.

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

1612{
1613 g_date_subtract_months(date, 3);
1615}
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.

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

Parameters
dateThe GDate to modify.

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

1605{
1606 g_date_subtract_months(date, 3);
1608}
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.

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

1642{
1644 g_date_subtract_years(date, 1);
1645}
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.

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

1635{
1637 g_date_subtract_years(date, 1);
1638}
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.

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

Parameters
dateThe GDate to modify.

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

1593{
1594 const GDateMonth months[] = {G_DATE_MARCH, G_DATE_JUNE,
1595 G_DATE_SEPTEMBER, G_DATE_DECEMBER};
1596 const GDateDay days[] = {31, 30, 30, 31};
1597 int quarter = (g_date_get_month (date) - 1) / 3;
1598
1599 g_date_set_month (date, months[quarter]);
1600 g_date_set_day (date, days[quarter]);
1601}

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

Parameters
dateThe GDate to modify.

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

1580{
1581 gint months;
1582
1583 /* Set the date to the first day of the specified month. */
1584 g_date_set_day(date, 1);
1585
1586 /* Back up 0-2 months */
1587 months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
1588 g_date_subtract_months(date, months);
1589}

◆ 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}

◆ 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}
GnuCash Date class.
gnc_ymd year_month_day() const
Get the year, month, and day from the date as a gnc_ymd.

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

1628{
1629 g_date_set_month(date, G_DATE_DECEMBER);
1630 g_date_set_day(date, 31);
1631}

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

1621{
1622 g_date_set_month(date, G_DATE_JANUARY);
1623 g_date_set_day(date, 1);
1624}

◆ 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}
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}

◆ 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}

◆ 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}

◆ 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}

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

1387{
1388 struct tm tm;
1389 time64 new_time;
1390
1391 gnc_tm_get_day_end(&tm, time_val);
1392 new_time = gnc_mktime(&tm);
1393 return new_time;
1394}
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
Definition gnc-date.cpp:219

◆ gnc_time64_get_day_end_gdate()

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.

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

1503{
1504 struct tm stm;
1505 time64 secs;
1506
1507 /* First convert to a 'struct tm' */
1508 g_date_to_struct_tm(date, &stm);
1509
1510 /* Force to th last second of the day */
1511 stm.tm_hour = 23;
1512 stm.tm_min = 59;
1513 stm.tm_sec = 59;
1514 stm.tm_isdst = -1;
1515
1516 /* Then convert to number of seconds */
1517 secs = gnc_mktime (&stm);
1518 return secs;
1519}

◆ gnc_time64_get_day_neutral()

time64 gnc_time64_get_day_neutral ( time64  time_val)

The gnc_time64_get_day_neutral() routine will take the given time in seconds and adjust it to 10:59:00Z of that day.

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

1378{
1379 struct tm tm;
1380 gnc_localtime_r(&time_val, &tm);
1381 return gnc_dmy2time64_internal(tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900,
1382 DayPart::neutral);
1383}

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

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

1367{
1368 struct tm tm;
1369 time64 new_time;
1370
1371 gnc_tm_get_day_start(&tm, time_val);
1372 new_time = gnc_mktime(&tm);
1373 return new_time;
1374}

◆ gnc_time64_get_day_start_gdate()

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.

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

1489{
1490 struct tm stm;
1491 time64 secs;
1492
1493 /* First convert to a 'struct tm' */
1494 g_date_to_struct_tm (date, &stm);
1495
1496 /* Then convert to number of seconds */
1497 secs = gnc_mktime (&stm);
1498 return secs;
1499}

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

1427{
1428 struct tm tm;
1429
1430 gnc_tm_get_day_end(&tm, gnc_time(nullptr));
1431 return gnc_mktime(&tm);
1432}

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

1418{
1419 struct tm tm;
1420
1421 gnc_tm_get_day_start(&tm, gnc_time(nullptr));
1422 return gnc_mktime(&tm);
1423}

◆ gnc_time64_to_iso8601_buff()

gchar * gnc_time64_to_iso8601_buff ( time64  time,
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}

◆ 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}

◆ 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}

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

1412{
1413 gnc_tm_get_day_end(tm, gnc_time(nullptr));
1414}

◆ gnc_tm_get_today_neutral()

void gnc_tm_get_today_neutral ( struct tm *  tm)

The gnc_tm_get_today_start() routine takes a pointer to a struct tm and fills it in with the timezone neutral time (10:59:00Z).

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

1406{
1407 gnc_tm_get_day_neutral(tm, gnc_time(nullptr));
1408}

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

1400{
1401 gnc_tm_get_day_start(tm, gnc_time(nullptr));
1402}

◆ gnc_tm_set_day_neutral()

void gnc_tm_set_day_neutral ( struct tm *  tm)

The gnc_tm_set_day_neutral() inline routine will set the appropriate fields in the struct tm to indicate 10:59am of that day.

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

1341{
1342 auto time_val{gnc_dmy2time64_internal(tm->tm_mday, tm->tm_mon + 1,
1343 tm->tm_year + 1900, DayPart::neutral)};
1344 gnc_localtime_r(&time_val, tm);
1345}

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

469{
472 {
473 dateCompletion = dc;
474 }
475 else
476 {
477 /* hack alert - Use a neutral default. */
478 PERR("non-existent date completion set attempted. Setting current year completion as default");
479 dateCompletion = QOF_DATE_COMPLETION_THISYEAR;
480 }
481
482 if (backmonths < 0)
483 {
484 backmonths = 0;
485 }
486 else if (backmonths > 11)
487 {
488 backmonths = 11;
489 }
490 dateCompletionBackMonths = backmonths;
491
492 return;
493}

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

429{
430 return dateFormat;
431}

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

503{
504 switch (df)
505 {
507 return "%m/%d/%Y";
509 return "%d/%m/%Y";
511 return "%d.%m.%Y";
513 return "%Y-%m-%dT%H:%M:%SZ";
515 return "%Y-%m-%d";
516 case QOF_DATE_FORMAT_UNSET: // use global
517 return qof_date_format_get_string (dateFormat);
519 default:
520 break;
521 };
522 return GNC_D_FMT;
523}

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

434{
435//avoid UB if df is out of range
436 auto dfi{static_cast<uint8_t>(df)};
437 if (dfi >= DATE_FORMAT_FIRST && dfi <= DATE_FORMAT_LAST)
438 {
439 prevQofDateFormat = dateFormat;
440 dateFormat = df;
441 }
442 else
443 {
444 /* hack alert - Use a neutral default. */
445 PERR("non-existent date format set attempted. Setting ISO default");
446 prevQofDateFormat = dateFormat;
447 dateFormat = QOF_DATE_FORMAT_ISO;
448 }
449
450 return;
451}

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

526{
527 switch (df)
528 {
530 return "%b %d, %Y";
533 return "%d %b %Y";
535 return "%Y-%m-%dT%H:%M:%SZ";
537 return "%Y-%b-%d";
538 case QOF_DATE_FORMAT_UNSET: // use global
539 return qof_date_text_format_get_string (dateFormat);
541 default:
542 break;
543 };
544 return GNC_D_FMT;
545}
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:525

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

611{
612 char buff[MAX_DATE_LENGTH + 1];
613 memset (buff, 0, sizeof (buff));
615 return g_strdup (buff);
616}

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

575{
576 if (!buff) return 0;
577
578 try
579 {
580 GncDateTime gncdt(t);
581 std::string str = gncdt.format(qof_date_format_get_string(dateFormat));
582 strncpy(buff, str.c_str(), len);
583 if (str.length() >= len)
584 buff[len - 1] = '\0';
585 }
586 catch(std::logic_error& err)
587 {
588 PWARN("Error processing time64 %" PRId64 ": %s", t, err.what());
589 }
590 catch(std::runtime_error& err)
591 {
592 PWARN("Error processing time64 %" PRId64 ": %s", t, err.what());
593 }
594 return strlen(buff);
595}

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

599{
600 GDate date;
601 g_date_clear (&date, 1);
602 date = *gd;
603 return qof_print_date_dmy_buff( buf, len,
604 g_date_get_day(&date),
605 g_date_get_month(&date),
606 g_date_get_year(&date) );
607}

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

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

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

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

◆ time64_to_gdate()

GDate time64_to_gdate ( time64  t)

Returns the GDate in which the time64 occurs.

Parameters
tThe time64
Returns
A GDate for the day in which the time64 occurs.

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

1285{
1286 GDate result;
1287
1288 g_date_clear (&result, 1);
1289 GncDateTime time(t);
1290 auto date = time.date().year_month_day();
1291 g_date_set_dmy (&result, date.day, static_cast<GDateMonth>(date.month),
1292 date.year);
1293 g_assert(g_date_valid (&result));
1294
1295 return result;
1296}

◆ 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}

Variable Documentation

◆ gnc_default_strftime_date_format

const char* gnc_default_strftime_date_format
extern

The default date format for use with strftime.

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

◆ t

time64 Time64::t

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