From 0f2b613d46d0135b538bf461d6b897352841fcc6 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sat, 14 Sep 2024 21:52:24 +0800 Subject: [PATCH 1/2] [gnc-report.cpp] gnc_date_interval_format (from_date, to_date) If Date Format is QOF_DATE_FORMAT_LOCALE, it will attempt to format date ranges using ICU date interval formatter. Otherwise fall back to translator localisation "%s to %s". --- gnucash/report/gnc-report.cpp | 71 +++++++++++++++++++++++++++++++++++ gnucash/report/gnc-report.h | 3 ++ gnucash/report/report.i | 3 ++ 3 files changed, 77 insertions(+) diff --git a/gnucash/report/gnc-report.cpp b/gnucash/report/gnc-report.cpp index 4181ecc0390..5625acf27a7 100644 --- a/gnucash/report/gnc-report.cpp +++ b/gnucash/report/gnc-report.cpp @@ -43,6 +43,9 @@ #include #include "gnc-report.h" +#include "unicode/dtitvfmt.h" +#include "unicode/smpdtfmt.h" + extern "C" SCM scm_init_sw_report_module(void); static QofLogModule log_module = GNC_MOD_GUI; @@ -428,3 +431,71 @@ gnc_get_optiondb_from_dispatcher(SCM dispatcher) return u_ptr->get(); } +static icu::DateIntervalFormat* +get_date_interval_format () +{ + static std::unique_ptr difmt; + if (!difmt) + { + icu::Locale locale; + if (auto lc_time_locale = setlocale (LC_TIME, nullptr)) + { + std::string localeStr(lc_time_locale); + if (size_t dotPos = localeStr.find('.'); dotPos != std::string::npos) + localeStr = localeStr.substr(0, dotPos); + + locale = icu::Locale::createCanonical (localeStr.c_str()); + } + UErrorCode status = U_ZERO_ERROR; + difmt.reset(icu::DateIntervalFormat::createInstance(UDAT_YEAR_NUM_MONTH_DAY, locale, status)); + if (U_FAILURE(status)) + return nullptr; + } + return difmt.get(); +} + +static gchar* +date_interval_format (time64 from_date, time64 to_date) +{ + auto difmt = get_date_interval_format(); + if (!difmt) + { + PWARN("couldn't create DateIntervalFormat"); + return nullptr; + } + + auto interval = new icu::DateInterval (from_date * 1000, to_date * 1000); + icu::UnicodeString result; + UErrorCode status = U_ZERO_ERROR; + difmt->format(interval, result, status); + if (U_FAILURE(status)) + { + PWARN("Error formatting interval"); + return nullptr; + } + + std::string interval_string; + result.toUTF8String(interval_string); + + return g_strdup (interval_string.c_str()); +} + +gchar* +gnc_date_interval_format (time64 from_date, time64 to_date) +{ + gchar* rv = nullptr; + + if (qof_date_format_get() == QOF_DATE_FORMAT_LOCALE) + rv = date_interval_format (from_date, to_date); + + // not using locale, or icu failure + if (!rv) + { + gchar from_buff[MAX_DATE_LENGTH+1], to_buff[MAX_DATE_LENGTH+1]; + qof_print_date_buff (from_buff, MAX_DATE_LENGTH, from_date); + qof_print_date_buff (to_buff, MAX_DATE_LENGTH, to_date); + rv = g_strdup_printf (_("%s to %s"), from_buff, to_buff); + } + + return rv; +} diff --git a/gnucash/report/gnc-report.h b/gnucash/report/gnc-report.h index 0b517eeeff4..619aa5e8665 100644 --- a/gnucash/report/gnc-report.h +++ b/gnucash/report/gnc-report.h @@ -27,6 +27,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { @@ -74,6 +75,8 @@ gboolean gnc_saved_reports_backup(void); gboolean gnc_saved_reports_write_to_file(const gchar* report_def, gboolean overwrite); +gchar* gnc_date_interval_format (time64 from_date, time64 to_date); + #ifdef __cplusplus } //extern "C" /** diff --git a/gnucash/report/report.i b/gnucash/report/report.i index e11655a3cad..6769602154f 100644 --- a/gnucash/report/report.i +++ b/gnucash/report/report.i @@ -42,3 +42,6 @@ gchar* gnc_get_default_report_font_family(); void gnc_saved_reports_backup (void); gboolean gnc_saved_reports_write_to_file (const gchar* report_def, gboolean overwrite); + +%newobject gnc_date_interval_format; +gchar* gnc_date_interval_format (time64 from_date, time64 to_date); From d634902c1f2d118434a036b3b52125b13e45b100 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sat, 14 Sep 2024 21:57:52 +0800 Subject: [PATCH 2/2] [reports] use gnc_date_interval_format --- gnucash/report/reports/example/daily-reports.scm | 5 +---- .../report/reports/standard/account-piecharts.scm | 5 +---- .../report/reports/standard/balance-forecast.scm | 4 +--- gnucash/report/reports/standard/balsheet-pnl.scm | 3 +-- gnucash/report/reports/standard/budget.scm | 6 +++--- gnucash/report/reports/standard/cash-flow.scm | 3 +-- .../report/reports/standard/cashflow-barchart.scm | 6 +----- .../report/reports/standard/category-barchart.scm | 8 ++------ .../report/reports/standard/investment-lots.scm | 11 +++-------- gnucash/report/reports/standard/net-charts.scm | 5 +---- .../report/reports/standard/new-owner-report.scm | 4 ++-- gnucash/report/reports/standard/price-scatter.scm | 5 +---- gnucash/report/reports/standard/txn-columns.scm | 2 +- gnucash/report/trep-engine.scm | 14 ++++++++------ 14 files changed, 27 insertions(+), 54 deletions(-) diff --git a/gnucash/report/reports/example/daily-reports.scm b/gnucash/report/reports/example/daily-reports.scm index 5219a421f7a..815e994dbbc 100644 --- a/gnucash/report/reports/example/daily-reports.scm +++ b/gnucash/report/reports/example/daily-reports.scm @@ -256,10 +256,7 @@ chart (list report-title (string-append - (format #f - (G_ "~a to ~a") - (qof-print-date from-date) - (qof-print-date to-date)) + (gnc-date-interval-format from-date to-date) (if show-total? (let ((total (apply + daily-totals))) (format diff --git a/gnucash/report/reports/standard/account-piecharts.scm b/gnucash/report/reports/standard/account-piecharts.scm index 439c6333e33..912fb6d3005 100644 --- a/gnucash/report/reports/standard/account-piecharts.scm +++ b/gnucash/report/reports/standard/account-piecharts.scm @@ -510,10 +510,7 @@ balance at a given time")) chart (list report-title (string-append (if do-intervals? - (format #f - (G_ "~a to ~a") - (qof-print-date from-date) - (qof-print-date to-date)) + (gnc-date-interval-format from-date to-date) (format #f (G_ "Balance at ~a") (qof-print-date to-date))) diff --git a/gnucash/report/reports/standard/balance-forecast.scm b/gnucash/report/reports/standard/balance-forecast.scm index 75f598ca11a..b840d6dcd2b 100644 --- a/gnucash/report/reports/standard/balance-forecast.scm +++ b/gnucash/report/reports/standard/balance-forecast.scm @@ -270,9 +270,7 @@ date point, a projected minimum balance including scheduled transactions.")) (gnc:html-chart-set-type! chart 'line) ;; Set the chart titles (gnc:html-chart-set-title! - chart (list report-title - (format #f (G_ "~a to ~a") - (qof-print-date from-date) (qof-print-date to-date)))) + chart (list report-title (gnc-date-interval-format from-date to-date))) ;; Set the chart size (gnc:html-chart-set-width! chart plot-width) (gnc:html-chart-set-height! chart plot-height) diff --git a/gnucash/report/reports/standard/balsheet-pnl.scm b/gnucash/report/reports/standard/balsheet-pnl.scm index da80ad300e5..94811618e9e 100644 --- a/gnucash/report/reports/standard/balsheet-pnl.scm +++ b/gnucash/report/reports/standard/balsheet-pnl.scm @@ -859,8 +859,7 @@ also show overall period profit & loss.")) (display report-title) (display " ") (if (or (not (eq? incr 'disabled)) (eq? report-type 'pnl)) - (format #t (G_ "~a to ~a") - (qof-print-date startdate) (qof-print-date enddate)) + (gnc-date-interval-format startdate enddate) (display (qof-print-date enddate)))))) (if (eq? (get-option gnc:pagename-general optname-options-summary) 'always) diff --git a/gnucash/report/reports/standard/budget.scm b/gnucash/report/reports/standard/budget.scm index ff03c435e9c..1aa44e26199 100644 --- a/gnucash/report/reports/standard/budget.scm +++ b/gnucash/report/reports/standard/budget.scm @@ -465,9 +465,9 @@ ((eq? (car column-list) 'total) (G_ "Total")) ((list? (car column-list)) - (format #f (G_ "~a to ~a") - (period-to-date-string (car (car column-list))) - (period-to-date-string (last (car column-list))))) + (gnc-date-interval-format + (gnc-budget-get-period-start-date budget (car (car column-list))) + (gnc-budget-get-period-start-date budget (last (car column-list))))) (else (period-to-date-string (car column-list))))) diff --git a/gnucash/report/reports/standard/cash-flow.scm b/gnucash/report/reports/standard/cash-flow.scm index 3ec0d0f3aff..69904a0f585 100644 --- a/gnucash/report/reports/standard/cash-flow.scm +++ b/gnucash/report/reports/standard/cash-flow.scm @@ -180,8 +180,7 @@ doc (string-append (get-option gnc:pagename-general gnc:optname-reportname) " - " - (format #f (G_ "~a to ~a") - (qof-print-date from-date-t64) (qof-print-date to-date-t64)))) + (gnc-date-interval-format from-date-t64 to-date-t64))) (if (not (null? accounts)) diff --git a/gnucash/report/reports/standard/cashflow-barchart.scm b/gnucash/report/reports/standard/cashflow-barchart.scm index 2189bcfd77a..a72df612d51 100644 --- a/gnucash/report/reports/standard/cashflow-barchart.scm +++ b/gnucash/report/reports/standard/cashflow-barchart.scm @@ -254,11 +254,7 @@ (gnc:report-percent-done 90) (gnc:html-chart-set-title! - chart (list report-title - (format #f - (G_ "~a to ~a") - (qof-print-date from-date-t64) - (qof-print-date to-date-t64)))) + chart (list report-title (gnc-date-interval-format from-date-t64 to-date-t64))) (gnc:html-chart-set-width! chart width) (gnc:html-chart-set-height! chart height) (gnc:html-chart-set-y-axis-label! diff --git a/gnucash/report/reports/standard/category-barchart.scm b/gnucash/report/reports/standard/category-barchart.scm index 4adbccf4a17..ee63e271a3a 100644 --- a/gnucash/report/reports/standard/category-barchart.scm +++ b/gnucash/report/reports/standard/category-barchart.scm @@ -518,12 +518,8 @@ Please deselect the accounts with negative balances.")) (gnc:html-chart-set-title! chart (list report-title - (format #f - (if do-intervals? - (G_ "~a to ~a") - (G_ "Balances ~a to ~a")) - (qof-print-date from-date-t64) - (qof-print-date to-date-t64)))) + (format #f (if do-intervals? "~a" (G_ "Balances ~a")) + (gnc-date-interval-format from-date-t64 to-date-t64)))) (gnc:html-chart-set-width! chart width) (gnc:html-chart-set-height! chart height) diff --git a/gnucash/report/reports/standard/investment-lots.scm b/gnucash/report/reports/standard/investment-lots.scm index ae23c33cd7a..40a897fcd3d 100644 --- a/gnucash/report/reports/standard/investment-lots.scm +++ b/gnucash/report/reports/standard/investment-lots.scm @@ -1960,10 +1960,9 @@ (gnc:html-document-set-title! document (format #f - (G_ "~a, ~a to ~a") + (G_ "~a, ~a") (get-option gnc:pagename-general gnc:optname-reportname) - (qof-print-date from-date) - (qof-print-date to-date))) + (gnc-date-interval-format from-date to-date))) (cond ((not (null? accounts)) @@ -1986,11 +1985,7 @@ (list colname-unrealized-gain)) '())))) (gnc:html-chart-set-title! chart - (list (N_ "Account Lot Gains") - (format #f - (G_ "~a to ~a") - (qof-print-date from-date) - (qof-print-date to-date)))) + (list (N_ "Account Lot Gains") (gnc-date-interval-format from-date to-date))) (gnc:html-chart-set-type! chart 'bar) (gnc:html-chart-set-width! chart chart-width) (gnc:html-chart-set-height! chart chart-height) diff --git a/gnucash/report/reports/standard/net-charts.scm b/gnucash/report/reports/standard/net-charts.scm index b5e1e5df997..d3695c750a9 100644 --- a/gnucash/report/reports/standard/net-charts.scm +++ b/gnucash/report/reports/standard/net-charts.scm @@ -312,10 +312,7 @@ (gnc:html-chart-set-width! chart width) (gnc:html-chart-set-height! chart height) (gnc:html-chart-set-title! - chart (list report-title - (format #f (G_ "~a to ~a") - (qof-print-date from-date-t64) - (qof-print-date to-date-t64)))) + chart (list report-title (gnc-date-interval-format from-date-t64 to-date-t64))) (gnc:html-chart-set-y-axis-label! chart (gnc-commodity-get-mnemonic report-currency)) (gnc:html-chart-set-grid?! chart y-grid) diff --git a/gnucash/report/reports/standard/new-owner-report.scm b/gnucash/report/reports/standard/new-owner-report.scm index 190f0b3b88d..42474c37e10 100644 --- a/gnucash/report/reports/standard/new-owner-report.scm +++ b/gnucash/report/reports/standard/new-owner-report.scm @@ -1128,8 +1128,8 @@ and do not match the transaction.")))))))) (gnc:html-document-add-object! document (gnc:make-html-text - (string-append (G_ "Date Range") ": " (qof-print-date start-date) - " - " (qof-print-date end-date)))) + (string-append (G_ "Date Range") ": " + (gnc-date-interval-format start-date end-date)))) (make-break! document) diff --git a/gnucash/report/reports/standard/price-scatter.scm b/gnucash/report/reports/standard/price-scatter.scm index 4c14c69e6b9..193632bfe10 100644 --- a/gnucash/report/reports/standard/price-scatter.scm +++ b/gnucash/report/reports/standard/price-scatter.scm @@ -182,10 +182,7 @@ (string-append (gnc-commodity-get-mnemonic base-commodity) " - " - (format #f - (G_ "~a to ~a") - (qof-print-date from-date) - (qof-print-date to-date))))) + (gnc-date-interval-format from-date to-date)))) (gnc:html-chart-set-width! chart width) (gnc:html-chart-set-height! chart height) (gnc:html-chart-set! chart diff --git a/gnucash/report/reports/standard/txn-columns.scm b/gnucash/report/reports/standard/txn-columns.scm index 16f9cafd9d0..a44fa75d68a 100644 --- a/gnucash/report/reports/standard/txn-columns.scm +++ b/gnucash/report/reports/standard/txn-columns.scm @@ -183,7 +183,7 @@ Select a different subset of transactions, or increase the limit in the options. 1 3 "total-label-cell" (gnc:make-html-text (G_ "Total For ") - (format #f "~a to ~a" (qof-print-date from-date) (qof-print-date to-date)))) + (gnc-date-interval-format from-date to-date))) (map (lambda (acc total) (gnc:make-html-table-cell/markup "total-number-cell" diff --git a/gnucash/report/trep-engine.scm b/gnucash/report/trep-engine.scm index 548115a7dfd..2971842c3a8 100644 --- a/gnucash/report/trep-engine.scm +++ b/gnucash/report/trep-engine.scm @@ -291,6 +291,12 @@ in the Options panel.")) (define (split->time64 s) (xaccTransGetDate (xaccSplitGetParent s))) + +(define (date-get-week-year-string date) + (let ((beginweekt64 (* (gnc:time64-get-week (gnc-mktime date)) 7 86400))) + (gnc-date-interval-format (+ beginweekt64 (* 3 86400)) + (+ beginweekt64 (* 9 86400))))) + (define date-subtotal-list ;; List for date option. ;; Defines the different date sorting keys, as an association-list. Each entry: @@ -316,7 +322,7 @@ in the Options panel.")) (cons 'split-sortvalue (lambda (s) (time64-week (split->time64 s)))) (cons 'date-sortvalue time64-week) (cons 'text (G_ "Weekly")) - (cons 'renderer-fn (compose gnc:date-get-week-year-string + (cons 'renderer-fn (compose date-get-week-year-string gnc-localtime split->time64))) @@ -2542,11 +2548,7 @@ be excluded from periodic reporting.") document (gnc:make-html-text (gnc:html-markup-h3 - (format #f - ;; Translators: Both ~a's are dates - (G_ "From ~a to ~a") - (qof-print-date begindate) - (qof-print-date enddate))))) + (gnc-date-interval-format begindate enddate)))) (when (eq? infobox-display 'always) (gnc:html-document-add-object!