@opendata-ai/openchart-core 6.2.0 → 6.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +5 -1
- package/dist/index.js +13 -9
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/locale/format.ts +23 -9
package/dist/index.d.ts
CHANGED
|
@@ -2401,8 +2401,12 @@ type DateGranularity = 'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' |
|
|
|
2401
2401
|
* @param value - Date object, ISO string, or timestamp number.
|
|
2402
2402
|
* @param locale - Locale string (currently unused, reserved for i18n).
|
|
2403
2403
|
* @param granularity - Time granularity for format selection.
|
|
2404
|
+
* @param useUtc - Whether to infer granularity and format using UTC methods.
|
|
2405
|
+
* Pass `false` when formatting ticks from a local-time scale (d3 scaleTime),
|
|
2406
|
+
* so that e.g. midnight local isn't misread as an intra-day UTC time.
|
|
2407
|
+
* Defaults to `true` for backward compatibility.
|
|
2404
2408
|
*/
|
|
2405
|
-
declare function formatDate(value: Date | string | number, _locale?: string, granularity?: DateGranularity): string;
|
|
2409
|
+
declare function formatDate(value: Date | string | number, _locale?: string, granularity?: DateGranularity, useUtc?: boolean): string;
|
|
2406
2410
|
/**
|
|
2407
2411
|
* Build a formatter for temporal values using a d3-time-format string (e.g. "%Y", "%b %Y").
|
|
2408
2412
|
* Returns a function that accepts a Date, string, or number and returns the formatted string.
|
package/dist/index.js
CHANGED
|
@@ -2403,16 +2403,16 @@ var GRANULARITY_FORMATS = {
|
|
|
2403
2403
|
hour: "%b %d %H:%M",
|
|
2404
2404
|
minute: "%H:%M"
|
|
2405
2405
|
};
|
|
2406
|
-
function formatDate(value, _locale, granularity) {
|
|
2406
|
+
function formatDate(value, _locale, granularity, useUtc = true) {
|
|
2407
2407
|
const date = value instanceof Date ? value : new Date(value);
|
|
2408
2408
|
if (Number.isNaN(date.getTime())) return String(value);
|
|
2409
|
-
const gran = granularity ?? inferGranularity(date);
|
|
2409
|
+
const gran = granularity ?? inferGranularity(date, useUtc);
|
|
2410
2410
|
if (gran === "quarter") {
|
|
2411
|
-
const q = Math.ceil((date.getMonth() + 1) / 3);
|
|
2411
|
+
const q = useUtc ? Math.ceil((date.getUTCMonth() + 1) / 3) : Math.ceil((date.getMonth() + 1) / 3);
|
|
2412
2412
|
return `Q${q} ${date.getFullYear()}`;
|
|
2413
2413
|
}
|
|
2414
2414
|
const formatStr = GRANULARITY_FORMATS[gran];
|
|
2415
|
-
if (
|
|
2415
|
+
if (useUtc) {
|
|
2416
2416
|
return utcFormat(formatStr)(date);
|
|
2417
2417
|
}
|
|
2418
2418
|
return timeFormat(formatStr)(date);
|
|
@@ -2426,12 +2426,16 @@ function buildTemporalFormatter(formatStr) {
|
|
|
2426
2426
|
return fmt(date);
|
|
2427
2427
|
};
|
|
2428
2428
|
}
|
|
2429
|
-
function inferGranularity(date) {
|
|
2430
|
-
|
|
2431
|
-
|
|
2429
|
+
function inferGranularity(date, useUtc = true) {
|
|
2430
|
+
const hours = useUtc ? date.getUTCHours() : date.getHours();
|
|
2431
|
+
const minutes = useUtc ? date.getUTCMinutes() : date.getMinutes();
|
|
2432
|
+
const day = useUtc ? date.getUTCDate() : date.getDate();
|
|
2433
|
+
const month = useUtc ? date.getUTCMonth() : date.getMonth();
|
|
2434
|
+
if (hours !== 0 || minutes !== 0) {
|
|
2435
|
+
return minutes !== 0 ? "minute" : "hour";
|
|
2432
2436
|
}
|
|
2433
|
-
if (
|
|
2434
|
-
if (
|
|
2437
|
+
if (day !== 1) return "day";
|
|
2438
|
+
if (month !== 0) return "month";
|
|
2435
2439
|
return "year";
|
|
2436
2440
|
}
|
|
2437
2441
|
|