Skip to content

Fiscal/Financial Quarter Localisation #127

@simon-curtis

Description

@simon-curtis

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch d3-time-format@4.1.0 for the project I'm working on.

We are using "time" scales in Vega charts which as you are aware uses your library for formatting. However when using %q (i.e. the formatQuarter function) it doesn't allow for UK locale where our quarters start in Apr.

You will need to update the Utc version of this function too

Here is the diff that solved my problem:

diff --git a/node_modules/d3-time-format/dist/d3-time-format.js b/node_modules/d3-time-format/dist/d3-time-format.js
index 8fc0998..990f4f3 100644
--- a/node_modules/d3-time-format/dist/d3-time-format.js
+++ b/node_modules/d3-time-format/dist/d3-time-format.js
@@ -328,7 +328,26 @@ function formatLocale(locale) {
   }
 
   function formatQuarter(d) {
-    return 1 + ~~(d.getMonth() / 3);
+      // Retrieve the user's preferred locale from the browser
+      const locale = navigator.languages && navigator.languages.length
+        ? navigator.languages[0]
+        : navigator.language;
+
+      // Create an Intl.Locale object to parse the locale; default to 'US' if undefined
+      const region = new Intl.Locale(locale).region || 'US';
+
+      // Define fiscal year start months for different regions
+      const fiscalYearStartMonths = {
+        'GB': 3, // UK: April (month index 3)
+        // Add other regions and their fiscal start months as needed
+      };
+
+      // Get the fiscal year start month for the detected region
+      const fiscalStartMonth = fiscalYearStartMonths[region] || 0; // Default to January if region not listed
+
+      // Calculate the fiscal month index
+      const fiscalMonth = (d.getMonth() - fiscalStartMonth + 12) % 12;
+      return Math.floor(fiscalMonth / 3) + 1;
   }
 
   function formatUTCShortWeekday(d) {
diff --git a/node_modules/d3-time-format/src/locale.js b/node_modules/d3-time-format/src/locale.js
index cc0dac2..5f53b88 100644
--- a/node_modules/d3-time-format/src/locale.js
+++ b/node_modules/d3-time-format/src/locale.js
@@ -334,7 +334,26 @@ export default function formatLocale(locale) {
   }
 
   function formatQuarter(d) {
-    return 1 + ~~(d.getMonth() / 3);
+    // Retrieve the user's preferred locale from the browser
+    const locale = navigator.languages && navigator.languages.length
+      ? navigator.languages[0]
+      : navigator.language;
+
+    // Create an Intl.Locale object to parse the locale; default to 'US' if undefined
+    const region = new Intl.Locale(locale).region || 'US';
+
+    // Define fiscal year start months for different regions
+    const fiscalYearStartMonths = {
+      'GB': 3, // UK: April (month index 3)
+      // Add other regions and their fiscal start months as needed
+    };
+
+    // Get the fiscal year start month for the detected region
+    const fiscalStartMonth = fiscalYearStartMonths[region] || 0; // Default to January if region not listed
+
+    // Calculate the fiscal month index
+    const fiscalMonth = (d.getMonth() - fiscalStartMonth + 12) % 12;
+    return Math.floor(fiscalMonth / 3) + 1;
   }
 
   function formatUTCShortWeekday(d) {

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions