@yuneta/gobj-ui 7.26.52 → 7.26.53

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.
@@ -9682,169 +9682,22 @@ function yui_icons_list() {
9682
9682
  return list;
9683
9683
  }
9684
9684
  //#endregion
9685
- //#region src/yui_time.js
9685
+ //#region src/yui_date_text.js
9686
9686
  /***********************************************************************
9687
- * yui_time.js
9688
- *
9689
- * Time, without a DOM: epochs, and the algebra of PERIODS.
9690
- *
9691
- * Two things live here, and they are the two halves of every date
9692
- * UI the projects keep rewriting:
9693
- *
9694
- * 1) EPOCH <-> the local wall clock. A timestamp travels in the
9695
- * producer's own unit — seconds, or milliseconds when a topic
9696
- * says so — and every conversion crosses that `ms` flag. Getting
9697
- * it wrong puts the same instant on two different clocks in one
9698
- * screen (it did: a picker asking "from 18:55" local returned
9699
- * rows a table labelled 16:55).
9700
- *
9701
- * 2) The PERIOD. A period is not an enum of five names: it is a
9702
- * BUCKET, and a bucket is `count` of a `unit`:
9703
- *
9704
- * {id: "day", unit: "day", count: 1}
9705
- * {id: "week", unit: "week", count: 1} ISO, Mon-first
9706
- * {id: "bimester", unit: "month", count: 2}
9707
- * {id: "quarter", unit: "month", count: 3}
9708
- * {id: "semester", unit: "month", count: 6}
9709
- * {id: "decade", unit: "year", count: 10}
9710
- * {id: "15min", unit: "minute", count: 15}
9711
- *
9712
- * Given that pair, ONE implementation answers every question a
9713
- * date navigator asks — where does the bucket holding this
9714
- * instant start and end, what is the previous/next one, what do
9715
- * I call it — so an app that wants quarters declares a quarter
9716
- * instead of asking for a new component.
9717
- *
9718
- * Buckets are ALIGNED, never "count back from now": months align to
9719
- * the year (which is why 2, 3, 4, 6 and 12 fall on clean calendar
9720
- * boundaries), weeks to Monday, hours to local midnight. "Last 7
9721
- * days" is a different animal — a rolling window — and it is not a
9722
- * period: see `rolling_bounds()`.
9687
+ * yui_date_text.js
9723
9688
  *
9724
- * Everything here is LOCAL time (the user reads a local clock) and
9725
- * DST-safe: it never adds 86400000 ms to cross a day, it builds the
9726
- * next date from calendar fields and lets the platform do it.
9689
+ * A moment as a VIEW shows it as text, and the locale to show it
9690
+ * in. A module of its own, with no i18n key, on purpose: the views
9691
+ * that show a time (C_YUI_FORM, C_YUI_TREEDB_TOPIC_WITH_FORM,
9692
+ * C_YUI_JSON, C_YUI_UPLOT) import it, and validate-locales follows
9693
+ * the modules an app mounts -- importing yui_time.js for this made
9694
+ * every app that shows a time owe the keys of the period picker
9695
+ * (gobj-ui 7.26.52). yui_time.js re-exports both.
9727
9696
  *
9728
9697
  * Copyright (c) 2026, ArtGins.
9729
9698
  * All Rights Reserved.
9730
9699
  ***********************************************************************/
9731
9700
  /***************************************************************
9732
- * Constants
9733
- ***************************************************************/
9734
- var MS_DAY = 864e5;
9735
- var PERIOD_UNITS = [
9736
- "minute",
9737
- "hour",
9738
- "day",
9739
- "week",
9740
- "month",
9741
- "year"
9742
- ];
9743
- var YUI_PERIODS = {
9744
- minute: {
9745
- id: "minute",
9746
- unit: "minute",
9747
- count: 1
9748
- },
9749
- "5min": {
9750
- id: "5min",
9751
- unit: "minute",
9752
- count: 5
9753
- },
9754
- "15min": {
9755
- id: "15min",
9756
- unit: "minute",
9757
- count: 15
9758
- },
9759
- hour: {
9760
- id: "hour",
9761
- unit: "hour",
9762
- count: 1
9763
- },
9764
- day: {
9765
- id: "day",
9766
- unit: "day",
9767
- count: 1
9768
- },
9769
- week: {
9770
- id: "week",
9771
- unit: "week",
9772
- count: 1
9773
- },
9774
- fortnight: {
9775
- id: "fortnight",
9776
- unit: "week",
9777
- count: 2
9778
- },
9779
- month: {
9780
- id: "month",
9781
- unit: "month",
9782
- count: 1
9783
- },
9784
- bimester: {
9785
- id: "bimester",
9786
- unit: "month",
9787
- count: 2
9788
- },
9789
- quarter: {
9790
- id: "quarter",
9791
- unit: "month",
9792
- count: 3
9793
- },
9794
- semester: {
9795
- id: "semester",
9796
- unit: "month",
9797
- count: 6
9798
- },
9799
- year: {
9800
- id: "year",
9801
- unit: "year",
9802
- count: 1
9803
- },
9804
- decade: {
9805
- id: "decade",
9806
- unit: "year",
9807
- count: 10
9808
- }
9809
- };
9810
- var YUI_PERIODS_DEFAULT = [
9811
- "hour",
9812
- "day",
9813
- "week",
9814
- "month",
9815
- "year"
9816
- ];
9817
- var YUI_ROLLING = {
9818
- "1h": {
9819
- id: "1h",
9820
- secs: 3600,
9821
- label: "last hour"
9822
- },
9823
- "6h": {
9824
- id: "6h",
9825
- secs: 21600,
9826
- label: "last 6h"
9827
- },
9828
- "24h": {
9829
- id: "24h",
9830
- secs: 86400,
9831
- label: "last 24h"
9832
- },
9833
- "7d": {
9834
- id: "7d",
9835
- secs: 604800,
9836
- label: "last 7 days"
9837
- },
9838
- "30d": {
9839
- id: "30d",
9840
- secs: 2592e3,
9841
- label: "last 30 days"
9842
- }
9843
- };
9844
- /***************************
9845
- * Locale / i18n
9846
- ***************************/
9847
- /***************************************************************
9848
9701
  * The locale to format with.
9849
9702
  *
9850
9703
  * NEVER at module top level, and never `navigator.language` raw:
@@ -9859,78 +9712,6 @@ function safe_locale(locale) {
9859
9712
  if (typeof nav === "string" && nav && nav !== "undefined") return nav;
9860
9713
  return "en";
9861
9714
  }
9862
- /***************************************************************
9863
- * Translate through the APP's translator when it gave us one, and
9864
- * degrade to the key itself otherwise — interpolating `{{x}}` by hand,
9865
- * so a label never renders raw mustaches at a caller that has no i18n.
9866
- ***************************************************************/
9867
- function tr(t, key, params) {
9868
- if (typeof t === "function") return t(key, params);
9869
- let s = key;
9870
- for (let k in params || {}) s = s.replaceAll(`{{${k}}}`, String(params[k]));
9871
- return s;
9872
- }
9873
- /***************************************************************
9874
- * Intl formatters, built PER CALL (see safe_locale). They are cheap
9875
- * next to a repaint, and caching them would need a locale-change hook
9876
- * the library has no business owning.
9877
- ***************************************************************/
9878
- function fmt_intl(date, locale, opts) {
9879
- try {
9880
- return new Intl.DateTimeFormat(safe_locale(locale), opts).format(date);
9881
- } catch (e) {
9882
- return date.toDateString();
9883
- }
9884
- }
9885
- /***************************
9886
- * Epoch <-> clock
9887
- ***************************/
9888
- /***************************************************************
9889
- * Epoch in the producer's unit -> milliseconds, and back.
9890
- * `ms` true means the value already IS milliseconds.
9891
- ***************************************************************/
9892
- function epoch_to_ms(value, ms) {
9893
- if (!value) return 0;
9894
- return ms ? value : value * 1e3;
9895
- }
9896
- function ms_to_epoch(value_ms, ms) {
9897
- if (!value_ms) return 0;
9898
- return ms ? value_ms : Math.floor(value_ms / 1e3);
9899
- }
9900
- /***************************************************************
9901
- * The LOCAL wall-clock string an `<input type="datetime-local">` takes
9902
- * ("YYYY-MM-DDTHH:MM:SS", which needs step=1 to keep the seconds), and
9903
- * the parse back. Empty / unparseable -> 0, which is exactly how a
9904
- * match condition reads an absent bound.
9905
- ***************************************************************/
9906
- function epoch_to_local_input(value, ms) {
9907
- if (!value) return "";
9908
- let d = new Date(epoch_to_ms(value, ms));
9909
- let pad = (n) => String(n).padStart(2, "0");
9910
- return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
9911
- }
9912
- function local_input_to_epoch(v, ms) {
9913
- if (!v) return 0;
9914
- let parsed = Date.parse(v);
9915
- if (Number.isNaN(parsed)) return 0;
9916
- return ms_to_epoch(parsed, ms);
9917
- }
9918
- /***************************************************************
9919
- * A timestamp for a table cell: the local wall clock, space-separated,
9920
- * keeping the milliseconds when the producer bothered to send them (a
9921
- * topic that sets sf_t_ms usually appends several records inside the
9922
- * same second, and a column that hides that shows a column of ties).
9923
- ***************************************************************/
9924
- function fmt_epoch(value, ms) {
9925
- if (!value) return "";
9926
- try {
9927
- let s = epoch_to_local_input(value, ms).replace("T", " ");
9928
- if (ms) s += `.${String(value % 1e3).padStart(3, "0")}`;
9929
- return s;
9930
- } catch (e) {
9931
- return String(value);
9932
- }
9933
- }
9934
9715
  var DATE_TIME_OPTIONS = {
9935
9716
  day: "numeric",
9936
9717
  month: "short",
@@ -9961,439 +9742,105 @@ function fmt_date_time(date, locale) {
9961
9742
  try {
9962
9743
  return new Intl.DateTimeFormat(safe_locale(locale), DATE_TIME_OPTIONS).format(date);
9963
9744
  } catch (e) {
9964
- return epoch_to_local_input(date.getTime(), true).replace("T", " ");
9745
+ let pad = (n) => String(n).padStart(2, "0");
9746
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
9965
9747
  }
9966
9748
  }
9967
- /***************************
9968
- * Calendar atoms
9969
- ***************************/
9970
- /***************************************************************
9971
- * The day number of a date, counting local calendar days from
9972
- * 1970-01-01. Built from the FIELDS (Date.UTC of y/m/d), never from
9973
- * the timestamp: a DST day is 23 or 25 hours long, so dividing a local
9974
- * epoch by 86400000 drifts a day twice a year.
9975
- ***************************************************************/
9976
- function day_number(d) {
9977
- return Math.floor(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) / MS_DAY);
9978
- }
9979
- function date_from_day_number(n) {
9980
- let d = /* @__PURE__ */ new Date(n * MS_DAY);
9981
- return new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
9982
- }
9983
- /***************************************************************
9984
- * The Monday of the week a date falls in (ISO: the week starts on
9985
- * Monday, and Sunday closes it).
9986
- ***************************************************************/
9987
- function start_of_iso_week(d) {
9988
- let day = d.getDay();
9989
- let back = day === 0 ? 6 : day - 1;
9990
- return new Date(d.getFullYear(), d.getMonth(), d.getDate() - back);
9991
- }
9992
- /***************************************************************
9993
- * The ISO week a date belongs to: {week, year}. The YEAR is the ISO
9994
- * week-year, not the calendar year — 2026-12-31 lives in week 1 of
9995
- * 2027, and a label that said "week 1 2026" would name a week six
9996
- * months away.
9997
- ***************************************************************/
9998
- function iso_week(d) {
9999
- let monday = start_of_iso_week(d);
10000
- let thursday = new Date(monday.getFullYear(), monday.getMonth(), monday.getDate() + 3);
10001
- let jan1 = new Date(thursday.getFullYear(), 0, 1);
10002
- return {
10003
- week: Math.floor((day_number(thursday) - day_number(jan1)) / 7) + 1,
10004
- year: thursday.getFullYear()
10005
- };
10006
- }
10007
- /***************************************************************
10008
- * The index of the ISO week a date falls in, counting weeks from the
10009
- * Monday of the epoch week. It is what aligns a MULTI-week bucket (a
10010
- * fortnight) to a Monday instead of to whatever day 1970-01-01 was.
10011
- ***************************************************************/
10012
- function week_index(d) {
10013
- return Math.floor((day_number(start_of_iso_week(d)) + 3) / 7);
10014
- }
10015
- /***************************
10016
- * Period algebra
10017
- ***************************/
10018
- /***************************************************************
10019
- * Normalize whatever a caller hands us into a spec: an id of the
10020
- * catalog, or a spec of its own. Returns null for anything that is not
10021
- * a bucket (a "span" / "custom" mode of a navigator has no algebra).
10022
- ***************************************************************/
10023
- function period_spec(period) {
10024
- if (!period) return null;
10025
- let spec = typeof period === "string" ? YUI_PERIODS[period] : period;
10026
- if (!spec || !spec.unit) return null;
10027
- if (PERIOD_UNITS.indexOf(spec.unit) < 0) return null;
10028
- let count = parseInt(spec.count, 10);
10029
- if (!(count > 0)) count = 1;
10030
- return {
10031
- id: spec.id || spec.unit,
10032
- unit: spec.unit,
10033
- count
10034
- };
9749
+ /************************************************************
9750
+ * JSON type discriminator (viewer vocabulary)
9751
+ ************************************************************/
9752
+ function json_type(value) {
9753
+ if (value === null) return "null";
9754
+ if (Array.isArray(value)) return "array";
9755
+ switch (typeof value) {
9756
+ case "string": return "string";
9757
+ case "number": return "number";
9758
+ case "boolean": return "boolean";
9759
+ case "object": return "object";
9760
+ default: return "unknown";
9761
+ }
10035
9762
  }
10036
- /***************************************************************
10037
- * The START of the bucket that holds `anchor_ms`, as a local Date.
9763
+ /************************************************************
9764
+ * Detect a kw_collapse() sentinel.
10038
9765
  *
10039
- * Alignment is what makes a bucket a bucket. Each unit floors against
10040
- * the natural origin of the unit above it, so the boundaries are the
10041
- * ones a human already has in their head:
9766
+ * The kernel replaces an over-limit dict with:
9767
+ * { "__collapsed__": { "path": ..., "size": N } }
9768
+ * and an over-limit array with:
9769
+ * [ { "__collapsed__": { "path": ..., "size": N } } ]
10042
9770
  *
10043
- * minute -> the hour (a 15min bucket starts at :00/:15/:30/:45)
10044
- * hour -> local midnight (a 6h bucket starts at 00/06/12/18)
10045
- * day -> 1970-01-01 (count=1 is the plain day; a 10-day
10046
- * bucket has no calendar origin to
10047
- * align to, so it aligns to the epoch)
10048
- * week -> the epoch week, Monday-first (ISO)
10049
- * month -> January (which is why 2/3/4/6/12 give calendar
10050
- * bimesters, quarters and semesters)
10051
- * year -> year 0 (a decade starts at 2020, not 2021)
10052
- ***************************************************************/
10053
- function period_start(period, anchor_ms) {
10054
- let spec = period_spec(period);
10055
- if (!spec) return null;
10056
- let d = new Date(anchor_ms);
10057
- let n = spec.count;
10058
- switch (spec.unit) {
10059
- case "minute": {
10060
- let m = Math.floor(d.getMinutes() / n) * n;
10061
- return new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), m);
10062
- }
10063
- case "hour": {
10064
- let h = Math.floor(d.getHours() / n) * n;
10065
- return new Date(d.getFullYear(), d.getMonth(), d.getDate(), h);
10066
- }
10067
- case "day": return date_from_day_number(Math.floor(day_number(d) / n) * n);
10068
- case "week": return date_from_day_number(Math.floor(week_index(d) / n) * n * 7 - 3);
10069
- case "month": {
10070
- let mi = d.getFullYear() * 12 + d.getMonth();
10071
- let s = Math.floor(mi / n) * n;
10072
- return new Date(Math.floor(s / 12), s % 12, 1);
9771
+ * Returns { size, path, is_array } or null.
9772
+ ************************************************************/
9773
+ function is_collapsed(value) {
9774
+ if (value && typeof value === "object" && !Array.isArray(value)) {
9775
+ let keys = Object.keys(value);
9776
+ if (keys.length === 1 && keys[0] === "__collapsed__") {
9777
+ let c = value.__collapsed__ || {};
9778
+ return {
9779
+ size: c.size,
9780
+ path: c.path,
9781
+ is_array: false
9782
+ };
10073
9783
  }
10074
- case "year": {
10075
- let y = Math.floor(d.getFullYear() / n) * n;
10076
- return new Date(y, 0, 1);
9784
+ return null;
9785
+ }
9786
+ if (Array.isArray(value) && value.length === 1) {
9787
+ let e = value[0];
9788
+ if (e && typeof e === "object" && !Array.isArray(e) && Object.keys(e).length === 1 && e.__collapsed__) {
9789
+ let c = e.__collapsed__ || {};
9790
+ return {
9791
+ size: c.size,
9792
+ path: c.path,
9793
+ is_array: true
9794
+ };
10077
9795
  }
10078
- default: return null;
10079
9796
  }
9797
+ return null;
10080
9798
  }
10081
- /***************************************************************
10082
- * The start of the bucket `delta` buckets away (delta may be negative).
10083
- * Calendar arithmetic, never millisecond arithmetic: `new Date(y, m+3, 1)`
10084
- * overflows the year by itself, and stepping a day across a DST change
10085
- * keeps landing on midnight.
10086
- ***************************************************************/
10087
- function period_shift(period, anchor_ms, delta) {
10088
- let spec = period_spec(period);
10089
- if (!spec) return anchor_ms;
10090
- let s = period_start(spec, anchor_ms);
10091
- let step = spec.count * (delta || 0);
10092
- switch (spec.unit) {
10093
- case "minute": return new Date(s.getFullYear(), s.getMonth(), s.getDate(), s.getHours(), s.getMinutes() + step).getTime();
10094
- case "hour": return new Date(s.getFullYear(), s.getMonth(), s.getDate(), s.getHours() + step).getTime();
10095
- case "day": return new Date(s.getFullYear(), s.getMonth(), s.getDate() + step).getTime();
10096
- case "week": return new Date(s.getFullYear(), s.getMonth(), s.getDate() + step * 7).getTime();
10097
- case "month": return new Date(s.getFullYear(), s.getMonth() + step, 1).getTime();
10098
- case "year": return new Date(s.getFullYear() + step, 0, 1).getTime();
10099
- default: return anchor_ms;
10100
- }
9799
+ /************************************************************
9800
+ * Join / split absolute path segments (kernel convention)
9801
+ ************************************************************/
9802
+ function seg_join(segments) {
9803
+ return segments.map(function(s) {
9804
+ return String(s);
9805
+ }).join("`");
10101
9806
  }
10102
- /***************************************************************
10103
- * The bucket holding `anchor_ms`, as {from, to} in MILLISECONDS.
10104
- *
10105
- * `to` is INCLUSIVE — the last millisecond of the bucket, not the first
10106
- * of the next one. Both ends of a match condition are inclusive, and an
10107
- * exclusive end handed to one silently swallows the record that landed
10108
- * exactly on the boundary.
10109
- ***************************************************************/
10110
- function period_bounds(period, anchor_ms) {
10111
- let spec = period_spec(period);
10112
- if (!spec) return {
10113
- from: 0,
10114
- to: 0
10115
- };
10116
- return {
10117
- from: period_start(spec, anchor_ms).getTime(),
10118
- to: period_shift(spec, anchor_ms, 1) - 1
10119
- };
9807
+ function seg_split(path) {
9808
+ if (path === "" || path === null || path === void 0) return [];
9809
+ return path.split("`");
10120
9810
  }
10121
- /***************************************************************
10122
- * The same bucket, in the unit the CONSUMER speaks (a tranger topic
10123
- * keeps its timestamps in seconds, unless its system_flag says
10124
- * milliseconds). This is the function a query builder calls.
10125
- ***************************************************************/
10126
- function period_bounds_epoch(period, anchor_ms, ms) {
10127
- let b = period_bounds(period, anchor_ms);
10128
- if (!b.from) return {
10129
- from: 0,
10130
- to: 0
10131
- };
10132
- return {
10133
- from: ms_to_epoch(b.from, ms),
10134
- to: ms_to_epoch(b.to, ms)
10135
- };
9811
+ /************************************************************
9812
+ * Walk a JSON tree by absolute segments; undefined if the
9813
+ * path does not resolve.
9814
+ ************************************************************/
9815
+ function get_by_segments(root, segments) {
9816
+ let v = root;
9817
+ for (let seg of segments) {
9818
+ if (v === null || v === void 0) return;
9819
+ if (Array.isArray(v)) v = v[Number(seg)];
9820
+ else if (typeof v === "object") v = v[seg];
9821
+ else return;
9822
+ }
9823
+ return v;
10136
9824
  }
10137
- /***************************************************************
10138
- * A ROLLING window: `secs` back from now. Not a bucket — it has no
10139
- * previous and no next, it just ends at `now`.
9825
+ /************************************************************
9826
+ * Set a value at absolute segments (mutates root).
10140
9827
  *
10141
- * The `to` end is left OPEN (0). An iterator with no upper bound keeps
10142
- * matching the records that land while the card is on screen; pinning
10143
- * it to "now" would freeze the window at the instant the user clicked.
10144
- ***************************************************************/
10145
- function rolling_bounds(rolling, ms, now_ms) {
10146
- let r = typeof rolling === "string" ? YUI_ROLLING[rolling] : rolling;
10147
- if (!r || !r.secs) return {
10148
- from: 0,
10149
- to: 0
10150
- };
10151
- return {
10152
- from: ms_to_epoch((now_ms === void 0 ? Date.now() : now_ms) - r.secs * 1e3, ms),
10153
- to: 0
10154
- };
10155
- }
10156
- /***************************************************************
10157
- * Is this the LAST bucket — the one `now` falls in? It is what greys
10158
- * out the "next" arrow, and what tells the navigator it is already home.
10159
- ***************************************************************/
10160
- function is_current_period(period, anchor_ms, now_ms) {
10161
- let spec = period_spec(period);
10162
- if (!spec) return false;
10163
- let now = now_ms === void 0 ? Date.now() : now_ms;
10164
- let a = period_start(spec, anchor_ms);
10165
- let b = period_start(spec, now);
10166
- return !!a && !!b && a.getTime() === b.getTime();
9828
+ * segments == [] means "replace the whole root" — the caller
9829
+ * must use the RETURNED value in that case (a primitive/array
9830
+ * root cannot be mutated in place).
9831
+ ************************************************************/
9832
+ function set_by_segments(root, segments, new_value) {
9833
+ if (segments.length === 0) return new_value;
9834
+ let parent = get_by_segments(root, segments.slice(0, -1));
9835
+ if (parent === null || parent === void 0 || typeof parent !== "object") return root;
9836
+ let last = segments[segments.length - 1];
9837
+ if (Array.isArray(parent)) parent[Number(last)] = new_value;
9838
+ else parent[last] = new_value;
9839
+ return root;
10167
9840
  }
10168
- /***************************************************************
10169
- * Recognize the bucket a {from, to} pair came from, so a range that
10170
- * travelled through a URL, a saved view or a backend answer comes back
10171
- * as the period the user picked instead of as a hand-typed range.
10172
- *
10173
- * `from`/`to` are in the CONSUMER's unit (`ms`), and so is the
10174
- * comparison — that is the whole trap: a bucket ends on the last
10175
- * MILLISECOND (…23:59:59.999), and a consumer that keeps seconds stored
10176
- * it truncated (…23:59:59). Comparing in milliseconds, a week saved by a
10177
- * seconds-based topic came back as "no bucket at all", every time.
10178
- *
10179
- * Only an EXACT match counts: both ends must land on the bucket's own
10180
- * boundaries, as that consumer would have written them. `candidates` are
10181
- * ids/specs, tried in order. Returns {period, anchor} (anchor in ms) or
10182
- * null.
10183
- ***************************************************************/
10184
- function infer_period(from, to, candidates, ms) {
10185
- if (!from || !to || to <= from) return null;
10186
- let from_ms = epoch_to_ms(from, ms);
10187
- for (let candidate of candidates || Object.keys(YUI_PERIODS)) {
10188
- let spec = period_spec(candidate);
10189
- if (!spec) continue;
10190
- let b = period_bounds_epoch(spec, from_ms, ms);
10191
- if (b.from === from && b.to === to) return {
10192
- period: spec,
10193
- anchor: from_ms
10194
- };
10195
- }
10196
- return null;
10197
- }
10198
- /***************************
10199
- * Period labels
10200
- ***************************/
10201
- /***************************************************************
10202
- * The name of a GRANULARITY (what the segmented control shows):
10203
- * the spec's own id as an i18n key, so an app that declares
10204
- * {id: "quarter"} gets "Trimestre" the moment it adds the key.
10205
- ***************************************************************/
10206
- function period_name(period, t) {
10207
- let spec = period_spec(period);
10208
- if (!spec) return tr(t, String(period && period.id || period || ""));
10209
- return tr(t, spec.id);
10210
- }
10211
- /***************************************************************
10212
- * The name of the bucket a navigator is PARKED on: what goes between
10213
- * the two arrows.
10214
- *
10215
- * day -> "Today" / "Yesterday" / "13 jul 2026"
10216
- * week -> "Week 27" (+ year when it is not this one)
10217
- * month -> "July" (+ year when it is not this one)
10218
- * quarter -> "Q3 2026" (unit month, count 3)
10219
- * semester -> "H2 2026" (unit month, count 6)
10220
- * year -> "2026"
10221
- * decade -> "2020 – 2029"
10222
- * any other -> the bucket's own edges: "1 jul – 31 aug 2026"
10223
- *
10224
- * The named ones are named because a range reads worse than a name;
10225
- * everything an app invents falls back to the range, which is always
10226
- * true and never wrong.
10227
- ***************************************************************/
10228
- function period_label(period, anchor_ms, t, locale) {
10229
- let spec = period_spec(period);
10230
- if (!spec) return "";
10231
- let loc = safe_locale(locale);
10232
- let start = period_start(spec, anchor_ms);
10233
- let end = new Date(period_bounds(spec, anchor_ms).to);
10234
- let now = /* @__PURE__ */ new Date();
10235
- let this_year = start.getFullYear() === now.getFullYear();
10236
- if (spec.unit === "day" && spec.count === 1) {
10237
- let diff = day_number(start) - day_number(now);
10238
- if (diff === 0) return tr(t, "today");
10239
- if (diff === -1) return tr(t, "yesterday");
10240
- if (diff === 1) return tr(t, "tomorrow");
10241
- return fmt_intl(start, loc, this_year ? {
10242
- day: "numeric",
10243
- month: "short"
10244
- } : {
10245
- day: "numeric",
10246
- month: "short",
10247
- year: "numeric"
10248
- });
10249
- }
10250
- if (spec.unit === "hour" && spec.count === 1) {
10251
- let hh = fmt_intl(start, loc, {
10252
- hour: "2-digit",
10253
- minute: "2-digit"
10254
- });
10255
- if (day_number(start) === day_number(now)) return hh;
10256
- return `${hh} · ${fmt_intl(start, loc, {
10257
- day: "numeric",
10258
- month: "short"
10259
- })}`;
10260
- }
10261
- if (spec.unit === "week" && spec.count === 1) {
10262
- let away = week_index(start) - week_index(now);
10263
- if (away === 0) return tr(t, "this week");
10264
- if (away === -1) return tr(t, "last week");
10265
- let w = iso_week(start);
10266
- return w.year === now.getFullYear() ? tr(t, "week {{n}}", { n: w.week }) : tr(t, "week {{n}} {{y}}", {
10267
- n: w.week,
10268
- y: w.year
10269
- });
10270
- }
10271
- if (spec.unit === "month" && spec.count === 1) return fmt_intl(start, loc, this_year ? { month: "long" } : {
10272
- month: "long",
10273
- year: "numeric"
10274
- });
10275
- if (spec.unit === "month" && (spec.count === 3 || spec.count === 6)) {
10276
- let n = Math.floor(start.getMonth() / spec.count) + 1;
10277
- return tr(t, spec.count === 3 ? "quarter {{n}} {{y}}" : "semester {{n}} {{y}}", {
10278
- n,
10279
- y: start.getFullYear()
10280
- });
10281
- }
10282
- if (spec.unit === "year") {
10283
- if (spec.count === 1) return String(start.getFullYear());
10284
- return `${start.getFullYear()} – ${end.getFullYear()}`;
10285
- }
10286
- let opts = spec.unit === "minute" || spec.unit === "hour" ? {
10287
- day: "numeric",
10288
- month: "short",
10289
- hour: "2-digit",
10290
- minute: "2-digit"
10291
- } : {
10292
- day: "numeric",
10293
- month: "short"
10294
- };
10295
- if (start.getFullYear() !== end.getFullYear()) {
10296
- let y_opts = Object.assign({ year: "numeric" }, opts);
10297
- return `${fmt_intl(start, loc, y_opts)} – ${fmt_intl(end, loc, y_opts)}`;
10298
- }
10299
- let span = `${fmt_intl(start, loc, opts)} – ${fmt_intl(end, loc, opts)}`;
10300
- return this_year ? span : `${span} ${start.getFullYear()}`;
10301
- }
10302
- /************************************************************
10303
- * JSON type discriminator (viewer vocabulary)
10304
- ************************************************************/
10305
- function json_type(value) {
10306
- if (value === null) return "null";
10307
- if (Array.isArray(value)) return "array";
10308
- switch (typeof value) {
10309
- case "string": return "string";
10310
- case "number": return "number";
10311
- case "boolean": return "boolean";
10312
- case "object": return "object";
10313
- default: return "unknown";
10314
- }
10315
- }
10316
- /************************************************************
10317
- * Detect a kw_collapse() sentinel.
10318
- *
10319
- * The kernel replaces an over-limit dict with:
10320
- * { "__collapsed__": { "path": ..., "size": N } }
10321
- * and an over-limit array with:
10322
- * [ { "__collapsed__": { "path": ..., "size": N } } ]
10323
- *
10324
- * Returns { size, path, is_array } or null.
10325
- ************************************************************/
10326
- function is_collapsed(value) {
10327
- if (value && typeof value === "object" && !Array.isArray(value)) {
10328
- let keys = Object.keys(value);
10329
- if (keys.length === 1 && keys[0] === "__collapsed__") {
10330
- let c = value.__collapsed__ || {};
10331
- return {
10332
- size: c.size,
10333
- path: c.path,
10334
- is_array: false
10335
- };
10336
- }
10337
- return null;
10338
- }
10339
- if (Array.isArray(value) && value.length === 1) {
10340
- let e = value[0];
10341
- if (e && typeof e === "object" && !Array.isArray(e) && Object.keys(e).length === 1 && e.__collapsed__) {
10342
- let c = e.__collapsed__ || {};
10343
- return {
10344
- size: c.size,
10345
- path: c.path,
10346
- is_array: true
10347
- };
10348
- }
10349
- }
10350
- return null;
10351
- }
10352
- /************************************************************
10353
- * Join / split absolute path segments (kernel convention)
10354
- ************************************************************/
10355
- function seg_join(segments) {
10356
- return segments.map(function(s) {
10357
- return String(s);
10358
- }).join("`");
10359
- }
10360
- function seg_split(path) {
10361
- if (path === "" || path === null || path === void 0) return [];
10362
- return path.split("`");
10363
- }
10364
- /************************************************************
10365
- * Walk a JSON tree by absolute segments; undefined if the
10366
- * path does not resolve.
10367
- ************************************************************/
10368
- function get_by_segments(root, segments) {
10369
- let v = root;
10370
- for (let seg of segments) {
10371
- if (v === null || v === void 0) return;
10372
- if (Array.isArray(v)) v = v[Number(seg)];
10373
- else if (typeof v === "object") v = v[seg];
10374
- else return;
10375
- }
10376
- return v;
10377
- }
10378
- /************************************************************
10379
- * Set a value at absolute segments (mutates root).
10380
- *
10381
- * segments == [] means "replace the whole root" — the caller
10382
- * must use the RETURNED value in that case (a primitive/array
10383
- * root cannot be mutated in place).
10384
- ************************************************************/
10385
- function set_by_segments(root, segments, new_value) {
10386
- if (segments.length === 0) return new_value;
10387
- let parent = get_by_segments(root, segments.slice(0, -1));
10388
- if (parent === null || parent === void 0 || typeof parent !== "object") return root;
10389
- let last = segments[segments.length - 1];
10390
- if (Array.isArray(parent)) parent[Number(last)] = new_value;
10391
- else parent[last] = new_value;
10392
- return root;
10393
- }
10394
- /************************************************************
10395
- * Does `value` (or any loaded descendant / its own key)
10396
- * contain the lower-cased search term?
9841
+ /************************************************************
9842
+ * Does `value` (or any loaded descendant / its own key)
9843
+ * contain the lower-cased search term?
10397
9844
  *
10398
9845
  * Collapsed (not-yet-loaded) subtrees can't be searched, so
10399
9846
  * they never match on content — only their key can match,
@@ -83545,6 +82992,576 @@ function register_c_yui_schema_editor() {
83545
82992
  return create_gclass$1(GCLASS_NAME$1);
83546
82993
  }
83547
82994
  //#endregion
82995
+ //#region src/yui_time.js
82996
+ /***********************************************************************
82997
+ * yui_time.js
82998
+ *
82999
+ * Time, without a DOM: epochs, and the algebra of PERIODS.
83000
+ *
83001
+ * Two things live here, and they are the two halves of every date
83002
+ * UI the projects keep rewriting:
83003
+ *
83004
+ * 1) EPOCH <-> the local wall clock. A timestamp travels in the
83005
+ * producer's own unit — seconds, or milliseconds when a topic
83006
+ * says so — and every conversion crosses that `ms` flag. Getting
83007
+ * it wrong puts the same instant on two different clocks in one
83008
+ * screen (it did: a picker asking "from 18:55" local returned
83009
+ * rows a table labelled 16:55).
83010
+ *
83011
+ * 2) The PERIOD. A period is not an enum of five names: it is a
83012
+ * BUCKET, and a bucket is `count` of a `unit`:
83013
+ *
83014
+ * {id: "day", unit: "day", count: 1}
83015
+ * {id: "week", unit: "week", count: 1} ISO, Mon-first
83016
+ * {id: "bimester", unit: "month", count: 2}
83017
+ * {id: "quarter", unit: "month", count: 3}
83018
+ * {id: "semester", unit: "month", count: 6}
83019
+ * {id: "decade", unit: "year", count: 10}
83020
+ * {id: "15min", unit: "minute", count: 15}
83021
+ *
83022
+ * Given that pair, ONE implementation answers every question a
83023
+ * date navigator asks — where does the bucket holding this
83024
+ * instant start and end, what is the previous/next one, what do
83025
+ * I call it — so an app that wants quarters declares a quarter
83026
+ * instead of asking for a new component.
83027
+ *
83028
+ * Buckets are ALIGNED, never "count back from now": months align to
83029
+ * the year (which is why 2, 3, 4, 6 and 12 fall on clean calendar
83030
+ * boundaries), weeks to Monday, hours to local midnight. "Last 7
83031
+ * days" is a different animal — a rolling window — and it is not a
83032
+ * period: see `rolling_bounds()`.
83033
+ *
83034
+ * Everything here is LOCAL time (the user reads a local clock) and
83035
+ * DST-safe: it never adds 86400000 ms to cross a day, it builds the
83036
+ * next date from calendar fields and lets the platform do it.
83037
+ *
83038
+ * Copyright (c) 2026, ArtGins.
83039
+ * All Rights Reserved.
83040
+ ***********************************************************************/
83041
+ /***************************************************************
83042
+ * Constants
83043
+ ***************************************************************/
83044
+ var MS_DAY = 864e5;
83045
+ var PERIOD_UNITS = [
83046
+ "minute",
83047
+ "hour",
83048
+ "day",
83049
+ "week",
83050
+ "month",
83051
+ "year"
83052
+ ];
83053
+ var YUI_PERIODS = {
83054
+ minute: {
83055
+ id: "minute",
83056
+ unit: "minute",
83057
+ count: 1
83058
+ },
83059
+ "5min": {
83060
+ id: "5min",
83061
+ unit: "minute",
83062
+ count: 5
83063
+ },
83064
+ "15min": {
83065
+ id: "15min",
83066
+ unit: "minute",
83067
+ count: 15
83068
+ },
83069
+ hour: {
83070
+ id: "hour",
83071
+ unit: "hour",
83072
+ count: 1
83073
+ },
83074
+ day: {
83075
+ id: "day",
83076
+ unit: "day",
83077
+ count: 1
83078
+ },
83079
+ week: {
83080
+ id: "week",
83081
+ unit: "week",
83082
+ count: 1
83083
+ },
83084
+ fortnight: {
83085
+ id: "fortnight",
83086
+ unit: "week",
83087
+ count: 2
83088
+ },
83089
+ month: {
83090
+ id: "month",
83091
+ unit: "month",
83092
+ count: 1
83093
+ },
83094
+ bimester: {
83095
+ id: "bimester",
83096
+ unit: "month",
83097
+ count: 2
83098
+ },
83099
+ quarter: {
83100
+ id: "quarter",
83101
+ unit: "month",
83102
+ count: 3
83103
+ },
83104
+ semester: {
83105
+ id: "semester",
83106
+ unit: "month",
83107
+ count: 6
83108
+ },
83109
+ year: {
83110
+ id: "year",
83111
+ unit: "year",
83112
+ count: 1
83113
+ },
83114
+ decade: {
83115
+ id: "decade",
83116
+ unit: "year",
83117
+ count: 10
83118
+ }
83119
+ };
83120
+ var YUI_PERIODS_DEFAULT = [
83121
+ "hour",
83122
+ "day",
83123
+ "week",
83124
+ "month",
83125
+ "year"
83126
+ ];
83127
+ var YUI_ROLLING = {
83128
+ "1h": {
83129
+ id: "1h",
83130
+ secs: 3600,
83131
+ label: "last hour"
83132
+ },
83133
+ "6h": {
83134
+ id: "6h",
83135
+ secs: 21600,
83136
+ label: "last 6h"
83137
+ },
83138
+ "24h": {
83139
+ id: "24h",
83140
+ secs: 86400,
83141
+ label: "last 24h"
83142
+ },
83143
+ "7d": {
83144
+ id: "7d",
83145
+ secs: 604800,
83146
+ label: "last 7 days"
83147
+ },
83148
+ "30d": {
83149
+ id: "30d",
83150
+ secs: 2592e3,
83151
+ label: "last 30 days"
83152
+ }
83153
+ };
83154
+ /***************************
83155
+ * Locale / i18n
83156
+ ***************************/
83157
+ /***************************************************************
83158
+ * Translate through the APP's translator when it gave us one, and
83159
+ * degrade to the key itself otherwise — interpolating `{{x}}` by hand,
83160
+ * so a label never renders raw mustaches at a caller that has no i18n.
83161
+ ***************************************************************/
83162
+ function tr(t, key, params) {
83163
+ if (typeof t === "function") return t(key, params);
83164
+ let s = key;
83165
+ for (let k in params || {}) s = s.replaceAll(`{{${k}}}`, String(params[k]));
83166
+ return s;
83167
+ }
83168
+ /***************************************************************
83169
+ * Intl formatters, built PER CALL (see safe_locale). They are cheap
83170
+ * next to a repaint, and caching them would need a locale-change hook
83171
+ * the library has no business owning.
83172
+ ***************************************************************/
83173
+ function fmt_intl(date, locale, opts) {
83174
+ try {
83175
+ return new Intl.DateTimeFormat(safe_locale(locale), opts).format(date);
83176
+ } catch (e) {
83177
+ return date.toDateString();
83178
+ }
83179
+ }
83180
+ /***************************
83181
+ * Epoch <-> clock
83182
+ ***************************/
83183
+ /***************************************************************
83184
+ * Epoch in the producer's unit -> milliseconds, and back.
83185
+ * `ms` true means the value already IS milliseconds.
83186
+ ***************************************************************/
83187
+ function epoch_to_ms(value, ms) {
83188
+ if (!value) return 0;
83189
+ return ms ? value : value * 1e3;
83190
+ }
83191
+ function ms_to_epoch(value_ms, ms) {
83192
+ if (!value_ms) return 0;
83193
+ return ms ? value_ms : Math.floor(value_ms / 1e3);
83194
+ }
83195
+ /***************************************************************
83196
+ * The LOCAL wall-clock string an `<input type="datetime-local">` takes
83197
+ * ("YYYY-MM-DDTHH:MM:SS", which needs step=1 to keep the seconds), and
83198
+ * the parse back. Empty / unparseable -> 0, which is exactly how a
83199
+ * match condition reads an absent bound.
83200
+ ***************************************************************/
83201
+ function epoch_to_local_input(value, ms) {
83202
+ if (!value) return "";
83203
+ let d = new Date(epoch_to_ms(value, ms));
83204
+ let pad = (n) => String(n).padStart(2, "0");
83205
+ return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
83206
+ }
83207
+ function local_input_to_epoch(v, ms) {
83208
+ if (!v) return 0;
83209
+ let parsed = Date.parse(v);
83210
+ if (Number.isNaN(parsed)) return 0;
83211
+ return ms_to_epoch(parsed, ms);
83212
+ }
83213
+ /***************************************************************
83214
+ * A timestamp for a table cell: the local wall clock, space-separated,
83215
+ * keeping the milliseconds when the producer bothered to send them (a
83216
+ * topic that sets sf_t_ms usually appends several records inside the
83217
+ * same second, and a column that hides that shows a column of ties).
83218
+ ***************************************************************/
83219
+ function fmt_epoch(value, ms) {
83220
+ if (!value) return "";
83221
+ try {
83222
+ let s = epoch_to_local_input(value, ms).replace("T", " ");
83223
+ if (ms) s += `.${String(value % 1e3).padStart(3, "0")}`;
83224
+ return s;
83225
+ } catch (e) {
83226
+ return String(value);
83227
+ }
83228
+ }
83229
+ /***************************
83230
+ * Calendar atoms
83231
+ ***************************/
83232
+ /***************************************************************
83233
+ * The day number of a date, counting local calendar days from
83234
+ * 1970-01-01. Built from the FIELDS (Date.UTC of y/m/d), never from
83235
+ * the timestamp: a DST day is 23 or 25 hours long, so dividing a local
83236
+ * epoch by 86400000 drifts a day twice a year.
83237
+ ***************************************************************/
83238
+ function day_number(d) {
83239
+ return Math.floor(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) / MS_DAY);
83240
+ }
83241
+ function date_from_day_number(n) {
83242
+ let d = /* @__PURE__ */ new Date(n * MS_DAY);
83243
+ return new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
83244
+ }
83245
+ /***************************************************************
83246
+ * The Monday of the week a date falls in (ISO: the week starts on
83247
+ * Monday, and Sunday closes it).
83248
+ ***************************************************************/
83249
+ function start_of_iso_week(d) {
83250
+ let day = d.getDay();
83251
+ let back = day === 0 ? 6 : day - 1;
83252
+ return new Date(d.getFullYear(), d.getMonth(), d.getDate() - back);
83253
+ }
83254
+ /***************************************************************
83255
+ * The ISO week a date belongs to: {week, year}. The YEAR is the ISO
83256
+ * week-year, not the calendar year — 2026-12-31 lives in week 1 of
83257
+ * 2027, and a label that said "week 1 2026" would name a week six
83258
+ * months away.
83259
+ ***************************************************************/
83260
+ function iso_week(d) {
83261
+ let monday = start_of_iso_week(d);
83262
+ let thursday = new Date(monday.getFullYear(), monday.getMonth(), monday.getDate() + 3);
83263
+ let jan1 = new Date(thursday.getFullYear(), 0, 1);
83264
+ return {
83265
+ week: Math.floor((day_number(thursday) - day_number(jan1)) / 7) + 1,
83266
+ year: thursday.getFullYear()
83267
+ };
83268
+ }
83269
+ /***************************************************************
83270
+ * The index of the ISO week a date falls in, counting weeks from the
83271
+ * Monday of the epoch week. It is what aligns a MULTI-week bucket (a
83272
+ * fortnight) to a Monday instead of to whatever day 1970-01-01 was.
83273
+ ***************************************************************/
83274
+ function week_index(d) {
83275
+ return Math.floor((day_number(start_of_iso_week(d)) + 3) / 7);
83276
+ }
83277
+ /***************************
83278
+ * Period algebra
83279
+ ***************************/
83280
+ /***************************************************************
83281
+ * Normalize whatever a caller hands us into a spec: an id of the
83282
+ * catalog, or a spec of its own. Returns null for anything that is not
83283
+ * a bucket (a "span" / "custom" mode of a navigator has no algebra).
83284
+ ***************************************************************/
83285
+ function period_spec(period) {
83286
+ if (!period) return null;
83287
+ let spec = typeof period === "string" ? YUI_PERIODS[period] : period;
83288
+ if (!spec || !spec.unit) return null;
83289
+ if (PERIOD_UNITS.indexOf(spec.unit) < 0) return null;
83290
+ let count = parseInt(spec.count, 10);
83291
+ if (!(count > 0)) count = 1;
83292
+ return {
83293
+ id: spec.id || spec.unit,
83294
+ unit: spec.unit,
83295
+ count
83296
+ };
83297
+ }
83298
+ /***************************************************************
83299
+ * The START of the bucket that holds `anchor_ms`, as a local Date.
83300
+ *
83301
+ * Alignment is what makes a bucket a bucket. Each unit floors against
83302
+ * the natural origin of the unit above it, so the boundaries are the
83303
+ * ones a human already has in their head:
83304
+ *
83305
+ * minute -> the hour (a 15min bucket starts at :00/:15/:30/:45)
83306
+ * hour -> local midnight (a 6h bucket starts at 00/06/12/18)
83307
+ * day -> 1970-01-01 (count=1 is the plain day; a 10-day
83308
+ * bucket has no calendar origin to
83309
+ * align to, so it aligns to the epoch)
83310
+ * week -> the epoch week, Monday-first (ISO)
83311
+ * month -> January (which is why 2/3/4/6/12 give calendar
83312
+ * bimesters, quarters and semesters)
83313
+ * year -> year 0 (a decade starts at 2020, not 2021)
83314
+ ***************************************************************/
83315
+ function period_start(period, anchor_ms) {
83316
+ let spec = period_spec(period);
83317
+ if (!spec) return null;
83318
+ let d = new Date(anchor_ms);
83319
+ let n = spec.count;
83320
+ switch (spec.unit) {
83321
+ case "minute": {
83322
+ let m = Math.floor(d.getMinutes() / n) * n;
83323
+ return new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), m);
83324
+ }
83325
+ case "hour": {
83326
+ let h = Math.floor(d.getHours() / n) * n;
83327
+ return new Date(d.getFullYear(), d.getMonth(), d.getDate(), h);
83328
+ }
83329
+ case "day": return date_from_day_number(Math.floor(day_number(d) / n) * n);
83330
+ case "week": return date_from_day_number(Math.floor(week_index(d) / n) * n * 7 - 3);
83331
+ case "month": {
83332
+ let mi = d.getFullYear() * 12 + d.getMonth();
83333
+ let s = Math.floor(mi / n) * n;
83334
+ return new Date(Math.floor(s / 12), s % 12, 1);
83335
+ }
83336
+ case "year": {
83337
+ let y = Math.floor(d.getFullYear() / n) * n;
83338
+ return new Date(y, 0, 1);
83339
+ }
83340
+ default: return null;
83341
+ }
83342
+ }
83343
+ /***************************************************************
83344
+ * The start of the bucket `delta` buckets away (delta may be negative).
83345
+ * Calendar arithmetic, never millisecond arithmetic: `new Date(y, m+3, 1)`
83346
+ * overflows the year by itself, and stepping a day across a DST change
83347
+ * keeps landing on midnight.
83348
+ ***************************************************************/
83349
+ function period_shift(period, anchor_ms, delta) {
83350
+ let spec = period_spec(period);
83351
+ if (!spec) return anchor_ms;
83352
+ let s = period_start(spec, anchor_ms);
83353
+ let step = spec.count * (delta || 0);
83354
+ switch (spec.unit) {
83355
+ case "minute": return new Date(s.getFullYear(), s.getMonth(), s.getDate(), s.getHours(), s.getMinutes() + step).getTime();
83356
+ case "hour": return new Date(s.getFullYear(), s.getMonth(), s.getDate(), s.getHours() + step).getTime();
83357
+ case "day": return new Date(s.getFullYear(), s.getMonth(), s.getDate() + step).getTime();
83358
+ case "week": return new Date(s.getFullYear(), s.getMonth(), s.getDate() + step * 7).getTime();
83359
+ case "month": return new Date(s.getFullYear(), s.getMonth() + step, 1).getTime();
83360
+ case "year": return new Date(s.getFullYear() + step, 0, 1).getTime();
83361
+ default: return anchor_ms;
83362
+ }
83363
+ }
83364
+ /***************************************************************
83365
+ * The bucket holding `anchor_ms`, as {from, to} in MILLISECONDS.
83366
+ *
83367
+ * `to` is INCLUSIVE — the last millisecond of the bucket, not the first
83368
+ * of the next one. Both ends of a match condition are inclusive, and an
83369
+ * exclusive end handed to one silently swallows the record that landed
83370
+ * exactly on the boundary.
83371
+ ***************************************************************/
83372
+ function period_bounds(period, anchor_ms) {
83373
+ let spec = period_spec(period);
83374
+ if (!spec) return {
83375
+ from: 0,
83376
+ to: 0
83377
+ };
83378
+ return {
83379
+ from: period_start(spec, anchor_ms).getTime(),
83380
+ to: period_shift(spec, anchor_ms, 1) - 1
83381
+ };
83382
+ }
83383
+ /***************************************************************
83384
+ * The same bucket, in the unit the CONSUMER speaks (a tranger topic
83385
+ * keeps its timestamps in seconds, unless its system_flag says
83386
+ * milliseconds). This is the function a query builder calls.
83387
+ ***************************************************************/
83388
+ function period_bounds_epoch(period, anchor_ms, ms) {
83389
+ let b = period_bounds(period, anchor_ms);
83390
+ if (!b.from) return {
83391
+ from: 0,
83392
+ to: 0
83393
+ };
83394
+ return {
83395
+ from: ms_to_epoch(b.from, ms),
83396
+ to: ms_to_epoch(b.to, ms)
83397
+ };
83398
+ }
83399
+ /***************************************************************
83400
+ * A ROLLING window: `secs` back from now. Not a bucket — it has no
83401
+ * previous and no next, it just ends at `now`.
83402
+ *
83403
+ * The `to` end is left OPEN (0). An iterator with no upper bound keeps
83404
+ * matching the records that land while the card is on screen; pinning
83405
+ * it to "now" would freeze the window at the instant the user clicked.
83406
+ ***************************************************************/
83407
+ function rolling_bounds(rolling, ms, now_ms) {
83408
+ let r = typeof rolling === "string" ? YUI_ROLLING[rolling] : rolling;
83409
+ if (!r || !r.secs) return {
83410
+ from: 0,
83411
+ to: 0
83412
+ };
83413
+ return {
83414
+ from: ms_to_epoch((now_ms === void 0 ? Date.now() : now_ms) - r.secs * 1e3, ms),
83415
+ to: 0
83416
+ };
83417
+ }
83418
+ /***************************************************************
83419
+ * Is this the LAST bucket — the one `now` falls in? It is what greys
83420
+ * out the "next" arrow, and what tells the navigator it is already home.
83421
+ ***************************************************************/
83422
+ function is_current_period(period, anchor_ms, now_ms) {
83423
+ let spec = period_spec(period);
83424
+ if (!spec) return false;
83425
+ let now = now_ms === void 0 ? Date.now() : now_ms;
83426
+ let a = period_start(spec, anchor_ms);
83427
+ let b = period_start(spec, now);
83428
+ return !!a && !!b && a.getTime() === b.getTime();
83429
+ }
83430
+ /***************************************************************
83431
+ * Recognize the bucket a {from, to} pair came from, so a range that
83432
+ * travelled through a URL, a saved view or a backend answer comes back
83433
+ * as the period the user picked instead of as a hand-typed range.
83434
+ *
83435
+ * `from`/`to` are in the CONSUMER's unit (`ms`), and so is the
83436
+ * comparison — that is the whole trap: a bucket ends on the last
83437
+ * MILLISECOND (…23:59:59.999), and a consumer that keeps seconds stored
83438
+ * it truncated (…23:59:59). Comparing in milliseconds, a week saved by a
83439
+ * seconds-based topic came back as "no bucket at all", every time.
83440
+ *
83441
+ * Only an EXACT match counts: both ends must land on the bucket's own
83442
+ * boundaries, as that consumer would have written them. `candidates` are
83443
+ * ids/specs, tried in order. Returns {period, anchor} (anchor in ms) or
83444
+ * null.
83445
+ ***************************************************************/
83446
+ function infer_period(from, to, candidates, ms) {
83447
+ if (!from || !to || to <= from) return null;
83448
+ let from_ms = epoch_to_ms(from, ms);
83449
+ for (let candidate of candidates || Object.keys(YUI_PERIODS)) {
83450
+ let spec = period_spec(candidate);
83451
+ if (!spec) continue;
83452
+ let b = period_bounds_epoch(spec, from_ms, ms);
83453
+ if (b.from === from && b.to === to) return {
83454
+ period: spec,
83455
+ anchor: from_ms
83456
+ };
83457
+ }
83458
+ return null;
83459
+ }
83460
+ /***************************
83461
+ * Period labels
83462
+ ***************************/
83463
+ /***************************************************************
83464
+ * The name of a GRANULARITY (what the segmented control shows):
83465
+ * the spec's own id as an i18n key, so an app that declares
83466
+ * {id: "quarter"} gets "Trimestre" the moment it adds the key.
83467
+ ***************************************************************/
83468
+ function period_name(period, t) {
83469
+ let spec = period_spec(period);
83470
+ if (!spec) return tr(t, String(period && period.id || period || ""));
83471
+ return tr(t, spec.id);
83472
+ }
83473
+ /***************************************************************
83474
+ * The name of the bucket a navigator is PARKED on: what goes between
83475
+ * the two arrows.
83476
+ *
83477
+ * day -> "Today" / "Yesterday" / "13 jul 2026"
83478
+ * week -> "Week 27" (+ year when it is not this one)
83479
+ * month -> "July" (+ year when it is not this one)
83480
+ * quarter -> "Q3 2026" (unit month, count 3)
83481
+ * semester -> "H2 2026" (unit month, count 6)
83482
+ * year -> "2026"
83483
+ * decade -> "2020 – 2029"
83484
+ * any other -> the bucket's own edges: "1 jul – 31 aug 2026"
83485
+ *
83486
+ * The named ones are named because a range reads worse than a name;
83487
+ * everything an app invents falls back to the range, which is always
83488
+ * true and never wrong.
83489
+ ***************************************************************/
83490
+ function period_label(period, anchor_ms, t, locale) {
83491
+ let spec = period_spec(period);
83492
+ if (!spec) return "";
83493
+ let loc = safe_locale(locale);
83494
+ let start = period_start(spec, anchor_ms);
83495
+ let end = new Date(period_bounds(spec, anchor_ms).to);
83496
+ let now = /* @__PURE__ */ new Date();
83497
+ let this_year = start.getFullYear() === now.getFullYear();
83498
+ if (spec.unit === "day" && spec.count === 1) {
83499
+ let diff = day_number(start) - day_number(now);
83500
+ if (diff === 0) return tr(t, "today");
83501
+ if (diff === -1) return tr(t, "yesterday");
83502
+ if (diff === 1) return tr(t, "tomorrow");
83503
+ return fmt_intl(start, loc, this_year ? {
83504
+ day: "numeric",
83505
+ month: "short"
83506
+ } : {
83507
+ day: "numeric",
83508
+ month: "short",
83509
+ year: "numeric"
83510
+ });
83511
+ }
83512
+ if (spec.unit === "hour" && spec.count === 1) {
83513
+ let hh = fmt_intl(start, loc, {
83514
+ hour: "2-digit",
83515
+ minute: "2-digit"
83516
+ });
83517
+ if (day_number(start) === day_number(now)) return hh;
83518
+ return `${hh} · ${fmt_intl(start, loc, {
83519
+ day: "numeric",
83520
+ month: "short"
83521
+ })}`;
83522
+ }
83523
+ if (spec.unit === "week" && spec.count === 1) {
83524
+ let away = week_index(start) - week_index(now);
83525
+ if (away === 0) return tr(t, "this week");
83526
+ if (away === -1) return tr(t, "last week");
83527
+ let w = iso_week(start);
83528
+ return w.year === now.getFullYear() ? tr(t, "week {{n}}", { n: w.week }) : tr(t, "week {{n}} {{y}}", {
83529
+ n: w.week,
83530
+ y: w.year
83531
+ });
83532
+ }
83533
+ if (spec.unit === "month" && spec.count === 1) return fmt_intl(start, loc, this_year ? { month: "long" } : {
83534
+ month: "long",
83535
+ year: "numeric"
83536
+ });
83537
+ if (spec.unit === "month" && (spec.count === 3 || spec.count === 6)) {
83538
+ let n = Math.floor(start.getMonth() / spec.count) + 1;
83539
+ return tr(t, spec.count === 3 ? "quarter {{n}} {{y}}" : "semester {{n}} {{y}}", {
83540
+ n,
83541
+ y: start.getFullYear()
83542
+ });
83543
+ }
83544
+ if (spec.unit === "year") {
83545
+ if (spec.count === 1) return String(start.getFullYear());
83546
+ return `${start.getFullYear()} – ${end.getFullYear()}`;
83547
+ }
83548
+ let opts = spec.unit === "minute" || spec.unit === "hour" ? {
83549
+ day: "numeric",
83550
+ month: "short",
83551
+ hour: "2-digit",
83552
+ minute: "2-digit"
83553
+ } : {
83554
+ day: "numeric",
83555
+ month: "short"
83556
+ };
83557
+ if (start.getFullYear() !== end.getFullYear()) {
83558
+ let y_opts = Object.assign({ year: "numeric" }, opts);
83559
+ return `${fmt_intl(start, loc, y_opts)} – ${fmt_intl(end, loc, y_opts)}`;
83560
+ }
83561
+ let span = `${fmt_intl(start, loc, opts)} – ${fmt_intl(end, loc, opts)}`;
83562
+ return this_year ? span : `${span} ${start.getFullYear()}`;
83563
+ }
83564
+ //#endregion
83548
83565
  //#region src/c_yui_period.js
83549
83566
  /***********************************************************************
83550
83567
  * c_yui_period.js