chronal 0.0.10 → 0.0.13

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.
Files changed (40) hide show
  1. package/README.md +61 -6
  2. package/esm/chainable/add.js +1 -1
  3. package/esm/chainable/chronal.d.ts +10 -1
  4. package/esm/chainable/chronal.d.ts.map +1 -1
  5. package/esm/chainable/chronal.js +5 -3
  6. package/esm/chainable/clamp.js +1 -1
  7. package/esm/chainable/dates-until.js +1 -1
  8. package/esm/chainable/end-of.js +2 -2
  9. package/esm/chainable/format.d.ts.map +1 -1
  10. package/esm/chainable/format.js +3 -0
  11. package/esm/chainable/set-unit.js +1 -1
  12. package/esm/chainable/start-of.js +2 -2
  13. package/esm/chainable/subtract.js +1 -1
  14. package/esm/core/rtf.d.ts +2 -0
  15. package/esm/core/rtf.d.ts.map +1 -0
  16. package/esm/core/rtf.js +10 -0
  17. package/esm/lib/from-now.d.ts.map +1 -1
  18. package/esm/lib/from-now.js +9 -3
  19. package/esm/lib/to-now.d.ts.map +1 -1
  20. package/esm/lib/to-now.js +9 -3
  21. package/package.json +1 -1
  22. package/script/chainable/add.js +1 -1
  23. package/script/chainable/chronal.d.ts +10 -1
  24. package/script/chainable/chronal.d.ts.map +1 -1
  25. package/script/chainable/chronal.js +5 -3
  26. package/script/chainable/clamp.js +1 -1
  27. package/script/chainable/dates-until.js +1 -1
  28. package/script/chainable/end-of.js +2 -2
  29. package/script/chainable/format.d.ts.map +1 -1
  30. package/script/chainable/format.js +3 -0
  31. package/script/chainable/set-unit.js +1 -1
  32. package/script/chainable/start-of.js +2 -2
  33. package/script/chainable/subtract.js +1 -1
  34. package/script/core/rtf.d.ts +2 -0
  35. package/script/core/rtf.d.ts.map +1 -0
  36. package/script/core/rtf.js +13 -0
  37. package/script/lib/from-now.d.ts.map +1 -1
  38. package/script/lib/from-now.js +9 -3
  39. package/script/lib/to-now.d.ts.map +1 -1
  40. package/script/lib/to-now.js +9 -3
package/README.md CHANGED
@@ -126,6 +126,12 @@ const date = chronal("2024-06-15T14:35:22Z")
126
126
 
127
127
  console.log(date); // "2024-07-01"
128
128
 
129
+ // Create with timezone - all operations use this timezone by default
130
+ const spDate = chronal("2024-06-15T14:30:00", { tz: "America/Sao_Paulo" });
131
+ spDate.format("YYYY-MM-DD HH:mm"); // Uses São Paulo time
132
+ spDate.startOf("day"); // Start of day in São Paulo
133
+ spDate.add({ days: 1 }); // Timezone is preserved
134
+
129
135
  // All methods available
130
136
  const result = chronal("2024-01-15")
131
137
  .add({ days: 10 })
@@ -135,7 +141,7 @@ const result = chronal("2024-01-15")
135
141
  // Query methods
136
142
  chronal("2024-06-15").isLeapYear(); // true
137
143
  chronal().isToday(); // true
138
- chronal("2024-01-01").fromNow(); // "last year"
144
+ chronal("2024-01-01").fromNow(); // "1 year ago"
139
145
 
140
146
  // Get values
141
147
  chronal("2024-06-15").get("month"); // 5 (0-indexed)
@@ -169,7 +175,7 @@ Set the default configuration for all date operations.
169
175
  **Example:**
170
176
 
171
177
  ```typescript
172
- import { formatDate, months, setChronalConfig } from "chronal";
178
+ import { formatDate, months, setChronalConfig, chronal } from "chronal";
173
179
 
174
180
  // Default is 'en-US' and 'UTC'
175
181
  formatDate(new Date("2024-06-15"), "MMMM"); // 'June'
@@ -186,22 +192,71 @@ setChronalConfig({ timezone: "America/Sao_Paulo" });
186
192
  formatDate(new Date("2024-06-15"), "MMMM", { locale: "fr-FR" }); // 'juin'
187
193
  ```
188
194
 
195
+ ### Per-Instance Timezone (Chainable API)
196
+
197
+ When using the chainable API, you can set a timezone for a specific instance. This timezone will be used for all operations on that instance:
198
+
199
+ ```typescript
200
+ import { chronal } from "chronal";
201
+
202
+ // Create instance with timezone
203
+ const spDate = chronal("2024-06-15T14:30:00", { tz: "America/Sao_Paulo" });
204
+
205
+ // All operations use the instance timezone
206
+ spDate.format("YYYY-MM-DD HH:mm"); // Uses São Paulo time (UTC-3)
207
+ spDate.startOf("day"); // Start of day in São Paulo
208
+ spDate.endOf("month"); // End of month in São Paulo
209
+
210
+ // Timezone is preserved across operations
211
+ const nextDay = spDate.add({ days: 1 });
212
+ nextDay.timezone; // "America/Sao_Paulo"
213
+
214
+ // Override timezone for specific operation
215
+ spDate.format("HH:mm", { tz: "UTC" }); // Use UTC instead
216
+
217
+ // Generated dates preserve timezone
218
+ const dates = spDate.until(new Date("2024-06-20"));
219
+ dates[0].timezone; // "America/Sao_Paulo"
220
+ ```
221
+
222
+ **Benefits:**
223
+ - ✅ Set timezone once, use everywhere in the chain
224
+ - ✅ Prevents accidental timezone mixing
225
+ - ✅ Clean API - no need to pass timezone to every method
226
+ - ✅ Can still override per method when needed
227
+
189
228
  ## API Reference
190
229
 
191
230
  ### Chainable Object API
192
231
 
193
232
  The `chronal` object provides a chainable API for convenient date manipulation.
194
233
 
195
- #### `chronal(date?)`
234
+ #### `chronal(date?, options?)`
196
235
 
197
236
  Creates a Chronal instance with chainable methods.
198
237
 
199
238
  **Parameters:**
200
239
 
201
- - `date` (Date | string | number, optional) - Initial date (defaults to current
202
- date)
240
+ - `date` (Date | string | number, optional) - Initial date (defaults to current date)
241
+ - `options` (object, optional) - Configuration options:
242
+ - `tz` (string) - IANA timezone for this instance (e.g., 'America/Sao_Paulo')
243
+
244
+ **Returns:** Chronal instance with optional timezone
245
+
246
+ **Examples:**
247
+
248
+ ```typescript
249
+ // Create with current time
250
+ const now = chronal();
203
251
 
204
- **Returns:** Chronal instance
252
+ // Create from string
253
+ const date = chronal("2024-06-15");
254
+
255
+ // Create with timezone - all operations will use this timezone
256
+ const spDate = chronal("2024-06-15", { tz: "America/Sao_Paulo" });
257
+ spDate.format("YYYY-MM-DD HH:mm"); // Uses São Paulo time
258
+ spDate.startOf("day").timezone; // "America/Sao_Paulo" (preserved)
259
+ ```
205
260
 
206
261
  **API Reference:**
207
262
 
@@ -12,5 +12,5 @@ import { chronal } from "./chronal.js";
12
12
  */
13
13
  export function add(opt) {
14
14
  const newDate = _addTime(this.date, opt);
15
- return chronal(newDate);
15
+ return chronal(newDate, { tz: this.timezone });
16
16
  }
@@ -31,6 +31,8 @@ import { setChronalConfig } from "../lib/config.js";
31
31
  export type Chronal = {
32
32
  /** The underlying Date object */
33
33
  date: Date;
34
+ /** The timezone for this instance (optional, defaults to config.timezone) */
35
+ timezone?: string;
34
36
  /** Adds time units to this date */
35
37
  add: typeof add;
36
38
  /** Subtracts time units from this date */
@@ -87,6 +89,7 @@ export type Chronal = {
87
89
  * All methods return new instances, preserving immutability.
88
90
  *
89
91
  * @param date - Optional Date, string, or timestamp. Defaults to current date/time
92
+ * @param options - Optional configuration including timezone
90
93
  * @returns A Chronal object with the date and chainable methods
91
94
  *
92
95
  * @example
@@ -100,6 +103,9 @@ export type Chronal = {
100
103
  * // Create from string (respects config.timezone)
101
104
  * const c2 = chronal('2024-06-15T12:00:00Z');
102
105
  *
106
+ * // Create with specific timezone
107
+ * const c3 = chronal('2024-06-15', { tz: 'America/Sao_Paulo' });
108
+ *
103
109
  * // Chain operations
104
110
  * chronal('2024-01-15')
105
111
  * .add({ months: 2, days: 10 })
@@ -107,8 +113,11 @@ export type Chronal = {
107
113
  * .format('YYYY-MM-DD'); // '2024-03-25'
108
114
  * ```
109
115
  */
116
+ type ChronalOptions = {
117
+ tz?: string;
118
+ };
110
119
  type ChronalFactory = {
111
- (date?: Date | string | number | null): Chronal;
120
+ (date?: Date | string | number | null, options?: ChronalOptions): Chronal;
112
121
  config: typeof setChronalConfig;
113
122
  };
114
123
  export declare const chronal: ChronalFactory;
@@ -1 +1 @@
1
- {"version":3,"file":"chronal.d.ts","sourceRoot":"","sources":["../../src/chainable/chronal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,E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,OAAO,EAAE,OAAO,OAAO,CAAC;IACxB,kDAAkD;IAClD,MAAM,EAAE,OAAO,MAAM,CAAC;IACtB,8BAA8B;IAC9B,OAAO,EAAE,OAAO,OAAO,CAAC;IACxB,iCAAiC;IACjC,UAAU,EAAE,OAAO,UAAU,CAAC;IAC9B,kCAAkC;IAClC,WAAW,EAAE,OAAO,WAAW,CAAC;IAChC,oCAAoC;IACpC,UAAU,EAAE,OAAO,UAAU,CAAC;IAC9B,8BAA8B;IAC9B,OAAO,EAAE,OAAO,OAAO,CAAC;IAGxB,sCAAsC;IACtC,GAAG,EAAE,OAAO,OAAO,CAAC;IACpB,yCAAyC;IACzC,OAAO,EAAE,OAAO,UAAU,CAAC;IAC3B,2CAA2C;IAC3C,WAAW,EAAE,OAAO,WAAW,CAAC;IAChC,uCAAuC;IACvC,IAAI,EAAE,OAAO,UAAU,CAAC;IAGxB,iEAAiE;IACjE,KAAK,EAAE,OAAO,KAAK,CAAC;CACrB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,KAAK,cAAc,GAAG;IACpB,EAAE,CAAC,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,KAAK,cAAc,GAAG;IACpB,CAAC,IAAI,CAAC,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC;IAC1E,MAAM,EAAE,OAAO,gBAAgB,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,OAAO,EAAE,cAoGrB,CAAC"}
@@ -25,20 +25,22 @@ import { clamp } from "./clamp.js";
25
25
  import { until } from "./dates-until.js";
26
26
  import { parseDate } from "../lib/parse-date.js";
27
27
  import { setChronalConfig } from "../lib/config.js";
28
- export const chronal = (date) => {
28
+ export const chronal = (date, options) => {
29
29
  let d;
30
+ const timezone = options?.tz;
30
31
  if (date === null || date === undefined) {
31
32
  d = new Date();
32
33
  }
33
34
  else if (typeof date === "string") {
34
- // Parse string with timezone awareness from config
35
- d = parseDate(date);
35
+ // Parse string with timezone awareness from options or config
36
+ d = parseDate(date, { tz: timezone });
36
37
  }
37
38
  else {
38
39
  d = new Date(date);
39
40
  }
40
41
  return {
41
42
  date: d,
43
+ timezone,
42
44
  // Manipulation
43
45
  add: function (opt) {
44
46
  return add.call(this, opt);
@@ -19,5 +19,5 @@ export function clamp(min, max) {
19
19
  const minDate = min instanceof Date ? min : min.date;
20
20
  const maxDate = max instanceof Date ? max : max.date;
21
21
  const newDate = clampDate(this.date, minDate, maxDate);
22
- return chronal(newDate);
22
+ return chronal(newDate, { tz: this.timezone });
23
23
  }
@@ -22,5 +22,5 @@ import { chronal } from "./chronal.js";
22
22
  export function until(end, step) {
23
23
  const endDate = end instanceof Date ? end : end.date;
24
24
  const dates = _datesUntil(this.date, endDate, step);
25
- return dates.map((date) => chronal(date));
25
+ return dates.map((date) => chronal(date, { tz: this.timezone }));
26
26
  }
@@ -17,8 +17,8 @@ import { config } from "../lib/config.js";
17
17
  export function endOf(unit, opt) {
18
18
  const options = opt || {};
19
19
  if (!options.tz) {
20
- options.tz = config.timezone;
20
+ options.tz = this.timezone || config.timezone;
21
21
  }
22
22
  const newDate = _endOf(this.date, unit, options);
23
- return chronal(newDate);
23
+ return chronal(newDate, { tz: this.timezone });
24
24
  }
@@ -1 +1 @@
1
- {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../src/chainable/format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,MAAM,CACpB,IAAI,EAAE,OAAO,EACb,GAAG,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC,EACrC,OAAO,GAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,CAAC,CAAM,GAC7C,MAAM,CAER"}
1
+ {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../src/chainable/format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,MAAM,CACpB,IAAI,EAAE,OAAO,EACb,GAAG,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC,EACrC,OAAO,GAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,CAAC,CAAM,GAC7C,MAAM,CAKR"}
@@ -15,5 +15,8 @@ import { formatDate } from "../lib/format-date.js";
15
15
  * ```
16
16
  */
17
17
  export function format(fmt, options = {}) {
18
+ if (!options.tz) {
19
+ options.tz = this.timezone;
20
+ }
18
21
  return formatDate(this.date, fmt, options);
19
22
  }
@@ -13,5 +13,5 @@ import { chronal } from "./chronal.js";
13
13
  */
14
14
  export function setUnit(opt) {
15
15
  const newDate = _setUnit(this.date, opt);
16
- return chronal(newDate);
16
+ return chronal(newDate, { tz: this.timezone });
17
17
  }
@@ -17,8 +17,8 @@ import { config } from "../lib/config.js";
17
17
  export function startOf(unit, opt) {
18
18
  const options = opt || {};
19
19
  if (!options.tz) {
20
- options.tz = config.timezone;
20
+ options.tz = this.timezone || config.timezone;
21
21
  }
22
22
  const newDate = _startOf(this.date, unit, options);
23
- return chronal(newDate);
23
+ return chronal(newDate, { tz: this.timezone });
24
24
  }
@@ -13,5 +13,5 @@ import { chronal } from "./chronal.js";
13
13
  */
14
14
  export function subtract(opt) {
15
15
  const newDate = _subtract(this.date, opt);
16
- return chronal(newDate);
16
+ return chronal(newDate, { tz: this.timezone });
17
17
  }
@@ -0,0 +1,2 @@
1
+ export declare function getRTF(locale: string, numeric: "auto" | "always"): Intl.RelativeTimeFormat;
2
+ //# sourceMappingURL=rtf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rtf.d.ts","sourceRoot":"","sources":["../../src/core/rtf.ts"],"names":[],"mappings":"AAEA,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ,2BAQhE"}
@@ -0,0 +1,10 @@
1
+ const rtfCache = new Map();
2
+ export function getRTF(locale, numeric) {
3
+ const key = `${locale}|${numeric}`;
4
+ let rtf = rtfCache.get(key);
5
+ if (!rtf) {
6
+ rtf = new Intl.RelativeTimeFormat(locale, { numeric });
7
+ rtfCache.set(key, rtf);
8
+ }
9
+ return rtf;
10
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"from-now.d.ts","sourceRoot":"","sources":["../../src/lib/from-now.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAmC1E"}
1
+ {"version":3,"file":"from-now.d.ts","sourceRoot":"","sources":["../../src/lib/from-now.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAyC1E"}
@@ -1,3 +1,4 @@
1
+ import { getRTF } from "../core/rtf.js";
1
2
  import { config } from "./config.js";
2
3
  /**
3
4
  * Returns a string representing how long ago the date was from now.
@@ -25,21 +26,26 @@ export function fromNow(date, locale = config.locale) {
25
26
  const minutes = Math.floor(seconds / 60);
26
27
  const hours = Math.floor(minutes / 60);
27
28
  const days = Math.floor(hours / 24);
29
+ const weeks = Math.floor(days / 7);
28
30
  const months = Math.floor(days / 30);
29
31
  const years = Math.floor(days / 365);
30
- const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
32
+ // Use auto for "now", always for everything else
31
33
  if (seconds < 60) {
32
- return rtf.format(0, "second");
34
+ return getRTF(locale, "auto").format(0, "second");
33
35
  }
36
+ const rtf = getRTF(locale, "always");
34
37
  if (minutes < 60) {
35
38
  return rtf.format(diff < 0 ? minutes : -minutes, "minute");
36
39
  }
37
40
  if (hours < 24) {
38
41
  return rtf.format(diff < 0 ? hours : -hours, "hour");
39
42
  }
40
- if (days < 30) {
43
+ if (days < 7) {
41
44
  return rtf.format(diff < 0 ? days : -days, "day");
42
45
  }
46
+ if (days < 30) {
47
+ return rtf.format(diff < 0 ? weeks : -weeks, "week");
48
+ }
43
49
  if (months < 12) {
44
50
  return rtf.format(diff < 0 ? months : -months, "month");
45
51
  }
@@ -1 +1 @@
1
- {"version":3,"file":"to-now.d.ts","sourceRoot":"","sources":["../../src/lib/to-now.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAmCxE"}
1
+ {"version":3,"file":"to-now.d.ts","sourceRoot":"","sources":["../../src/lib/to-now.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAyCxE"}
package/esm/lib/to-now.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { getRTF } from "../core/rtf.js";
1
2
  import { config } from "./config.js";
2
3
  /**
3
4
  * Returns a string representing the time from now to the date.
@@ -26,21 +27,26 @@ export function toNow(date, locale = config.locale) {
26
27
  const minutes = Math.floor(seconds / 60);
27
28
  const hours = Math.floor(minutes / 60);
28
29
  const days = Math.floor(hours / 24);
30
+ const weeks = Math.floor(days / 7);
29
31
  const months = Math.floor(days / 30);
30
32
  const years = Math.floor(days / 365);
31
- const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
33
+ // Use auto for "now", always for everything else
32
34
  if (seconds < 60) {
33
- return rtf.format(0, "second");
35
+ return getRTF(locale, "auto").format(0, "second");
34
36
  }
37
+ const rtf = getRTF(locale, "always");
35
38
  if (minutes < 60) {
36
39
  return rtf.format(diff > 0 ? minutes : -minutes, "minute");
37
40
  }
38
41
  if (hours < 24) {
39
42
  return rtf.format(diff > 0 ? hours : -hours, "hour");
40
43
  }
41
- if (days < 30) {
44
+ if (days < 7) {
42
45
  return rtf.format(diff > 0 ? days : -days, "day");
43
46
  }
47
+ if (days < 30) {
48
+ return rtf.format(diff > 0 ? weeks : -weeks, "week");
49
+ }
44
50
  if (months < 12) {
45
51
  return rtf.format(diff > 0 ? months : -months, "month");
46
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chronal",
3
- "version": "0.0.10",
3
+ "version": "0.0.13",
4
4
  "description": "A tiny and fast date utility library for modern JavaScript runtimes.",
5
5
  "keywords": [
6
6
  "date",
@@ -15,5 +15,5 @@ const chronal_js_1 = require("./chronal.js");
15
15
  */
16
16
  function add(opt) {
17
17
  const newDate = (0, add_time_js_1.addTime)(this.date, opt);
18
- return (0, chronal_js_1.chronal)(newDate);
18
+ return (0, chronal_js_1.chronal)(newDate, { tz: this.timezone });
19
19
  }
@@ -31,6 +31,8 @@ import { setChronalConfig } from "../lib/config.js";
31
31
  export type Chronal = {
32
32
  /** The underlying Date object */
33
33
  date: Date;
34
+ /** The timezone for this instance (optional, defaults to config.timezone) */
35
+ timezone?: string;
34
36
  /** Adds time units to this date */
35
37
  add: typeof add;
36
38
  /** Subtracts time units from this date */
@@ -87,6 +89,7 @@ export type Chronal = {
87
89
  * All methods return new instances, preserving immutability.
88
90
  *
89
91
  * @param date - Optional Date, string, or timestamp. Defaults to current date/time
92
+ * @param options - Optional configuration including timezone
90
93
  * @returns A Chronal object with the date and chainable methods
91
94
  *
92
95
  * @example
@@ -100,6 +103,9 @@ export type Chronal = {
100
103
  * // Create from string (respects config.timezone)
101
104
  * const c2 = chronal('2024-06-15T12:00:00Z');
102
105
  *
106
+ * // Create with specific timezone
107
+ * const c3 = chronal('2024-06-15', { tz: 'America/Sao_Paulo' });
108
+ *
103
109
  * // Chain operations
104
110
  * chronal('2024-01-15')
105
111
  * .add({ months: 2, days: 10 })
@@ -107,8 +113,11 @@ export type Chronal = {
107
113
  * .format('YYYY-MM-DD'); // '2024-03-25'
108
114
  * ```
109
115
  */
116
+ type ChronalOptions = {
117
+ tz?: string;
118
+ };
110
119
  type ChronalFactory = {
111
- (date?: Date | string | number | null): Chronal;
120
+ (date?: Date | string | number | null, options?: ChronalOptions): Chronal;
112
121
  config: typeof setChronalConfig;
113
122
  };
114
123
  export declare const chronal: ChronalFactory;
@@ -1 +1 @@
1
- {"version":3,"file":"chronal.d.ts","sourceRoot":"","sources":["../../src/chainable/chronal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC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AAE,OAAO,KAAK,CAAC;CACrB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,KAAK,cAAc,GAAG;IACpB,EAAE,CAAC,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,KAAK,cAAc,GAAG;IACpB,CAAC,IAAI,CAAC,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC;IAC1E,MAAM,EAAE,OAAO,gBAAgB,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,OAAO,EAAE,cAoGrB,CAAC"}
@@ -28,20 +28,22 @@ const clamp_js_1 = require("./clamp.js");
28
28
  const dates_until_js_1 = require("./dates-until.js");
29
29
  const parse_date_js_1 = require("../lib/parse-date.js");
30
30
  const config_js_1 = require("../lib/config.js");
31
- const chronal = (date) => {
31
+ const chronal = (date, options) => {
32
32
  let d;
33
+ const timezone = options?.tz;
33
34
  if (date === null || date === undefined) {
34
35
  d = new Date();
35
36
  }
36
37
  else if (typeof date === "string") {
37
- // Parse string with timezone awareness from config
38
- d = (0, parse_date_js_1.parseDate)(date);
38
+ // Parse string with timezone awareness from options or config
39
+ d = (0, parse_date_js_1.parseDate)(date, { tz: timezone });
39
40
  }
40
41
  else {
41
42
  d = new Date(date);
42
43
  }
43
44
  return {
44
45
  date: d,
46
+ timezone,
45
47
  // Manipulation
46
48
  add: function (opt) {
47
49
  return add_js_1.add.call(this, opt);
@@ -22,5 +22,5 @@ function clamp(min, max) {
22
22
  const minDate = min instanceof Date ? min : min.date;
23
23
  const maxDate = max instanceof Date ? max : max.date;
24
24
  const newDate = (0, clamp_date_js_1.clampDate)(this.date, minDate, maxDate);
25
- return (0, chronal_js_1.chronal)(newDate);
25
+ return (0, chronal_js_1.chronal)(newDate, { tz: this.timezone });
26
26
  }
@@ -25,5 +25,5 @@ const chronal_js_1 = require("./chronal.js");
25
25
  function until(end, step) {
26
26
  const endDate = end instanceof Date ? end : end.date;
27
27
  const dates = (0, dates_until_js_1.datesUntil)(this.date, endDate, step);
28
- return dates.map((date) => (0, chronal_js_1.chronal)(date));
28
+ return dates.map((date) => (0, chronal_js_1.chronal)(date, { tz: this.timezone }));
29
29
  }
@@ -20,8 +20,8 @@ const config_js_1 = require("../lib/config.js");
20
20
  function endOf(unit, opt) {
21
21
  const options = opt || {};
22
22
  if (!options.tz) {
23
- options.tz = config_js_1.config.timezone;
23
+ options.tz = this.timezone || config_js_1.config.timezone;
24
24
  }
25
25
  const newDate = (0, end_of_js_1.endOf)(this.date, unit, options);
26
- return (0, chronal_js_1.chronal)(newDate);
26
+ return (0, chronal_js_1.chronal)(newDate, { tz: this.timezone });
27
27
  }
@@ -1 +1 @@
1
- {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../src/chainable/format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,MAAM,CACpB,IAAI,EAAE,OAAO,EACb,GAAG,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC,EACrC,OAAO,GAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,CAAC,CAAM,GAC7C,MAAM,CAER"}
1
+ {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../src/chainable/format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,MAAM,CACpB,IAAI,EAAE,OAAO,EACb,GAAG,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC,EACrC,OAAO,GAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,CAAC,CAAM,GAC7C,MAAM,CAKR"}
@@ -18,5 +18,8 @@ const format_date_js_1 = require("../lib/format-date.js");
18
18
  * ```
19
19
  */
20
20
  function format(fmt, options = {}) {
21
+ if (!options.tz) {
22
+ options.tz = this.timezone;
23
+ }
21
24
  return (0, format_date_js_1.formatDate)(this.date, fmt, options);
22
25
  }
@@ -16,5 +16,5 @@ const chronal_js_1 = require("./chronal.js");
16
16
  */
17
17
  function setUnit(opt) {
18
18
  const newDate = (0, set_unit_js_1.setUnit)(this.date, opt);
19
- return (0, chronal_js_1.chronal)(newDate);
19
+ return (0, chronal_js_1.chronal)(newDate, { tz: this.timezone });
20
20
  }
@@ -20,8 +20,8 @@ const config_js_1 = require("../lib/config.js");
20
20
  function startOf(unit, opt) {
21
21
  const options = opt || {};
22
22
  if (!options.tz) {
23
- options.tz = config_js_1.config.timezone;
23
+ options.tz = this.timezone || config_js_1.config.timezone;
24
24
  }
25
25
  const newDate = (0, start_of_js_1.startOf)(this.date, unit, options);
26
- return (0, chronal_js_1.chronal)(newDate);
26
+ return (0, chronal_js_1.chronal)(newDate, { tz: this.timezone });
27
27
  }
@@ -16,5 +16,5 @@ const chronal_js_1 = require("./chronal.js");
16
16
  */
17
17
  function subtract(opt) {
18
18
  const newDate = (0, subtract_time_js_1.subtractTime)(this.date, opt);
19
- return (0, chronal_js_1.chronal)(newDate);
19
+ return (0, chronal_js_1.chronal)(newDate, { tz: this.timezone });
20
20
  }
@@ -0,0 +1,2 @@
1
+ export declare function getRTF(locale: string, numeric: "auto" | "always"): Intl.RelativeTimeFormat;
2
+ //# sourceMappingURL=rtf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rtf.d.ts","sourceRoot":"","sources":["../../src/core/rtf.ts"],"names":[],"mappings":"AAEA,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ,2BAQhE"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRTF = getRTF;
4
+ const rtfCache = new Map();
5
+ function getRTF(locale, numeric) {
6
+ const key = `${locale}|${numeric}`;
7
+ let rtf = rtfCache.get(key);
8
+ if (!rtf) {
9
+ rtf = new Intl.RelativeTimeFormat(locale, { numeric });
10
+ rtfCache.set(key, rtf);
11
+ }
12
+ return rtf;
13
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"from-now.d.ts","sourceRoot":"","sources":["../../src/lib/from-now.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAmC1E"}
1
+ {"version":3,"file":"from-now.d.ts","sourceRoot":"","sources":["../../src/lib/from-now.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAyC1E"}
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fromNow = fromNow;
4
+ const rtf_js_1 = require("../core/rtf.js");
4
5
  const config_js_1 = require("./config.js");
5
6
  /**
6
7
  * Returns a string representing how long ago the date was from now.
@@ -28,21 +29,26 @@ function fromNow(date, locale = config_js_1.config.locale) {
28
29
  const minutes = Math.floor(seconds / 60);
29
30
  const hours = Math.floor(minutes / 60);
30
31
  const days = Math.floor(hours / 24);
32
+ const weeks = Math.floor(days / 7);
31
33
  const months = Math.floor(days / 30);
32
34
  const years = Math.floor(days / 365);
33
- const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
35
+ // Use auto for "now", always for everything else
34
36
  if (seconds < 60) {
35
- return rtf.format(0, "second");
37
+ return (0, rtf_js_1.getRTF)(locale, "auto").format(0, "second");
36
38
  }
39
+ const rtf = (0, rtf_js_1.getRTF)(locale, "always");
37
40
  if (minutes < 60) {
38
41
  return rtf.format(diff < 0 ? minutes : -minutes, "minute");
39
42
  }
40
43
  if (hours < 24) {
41
44
  return rtf.format(diff < 0 ? hours : -hours, "hour");
42
45
  }
43
- if (days < 30) {
46
+ if (days < 7) {
44
47
  return rtf.format(diff < 0 ? days : -days, "day");
45
48
  }
49
+ if (days < 30) {
50
+ return rtf.format(diff < 0 ? weeks : -weeks, "week");
51
+ }
46
52
  if (months < 12) {
47
53
  return rtf.format(diff < 0 ? months : -months, "month");
48
54
  }
@@ -1 +1 @@
1
- {"version":3,"file":"to-now.d.ts","sourceRoot":"","sources":["../../src/lib/to-now.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAmCxE"}
1
+ {"version":3,"file":"to-now.d.ts","sourceRoot":"","sources":["../../src/lib/to-now.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAyCxE"}
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.toNow = toNow;
4
+ const rtf_js_1 = require("../core/rtf.js");
4
5
  const config_js_1 = require("./config.js");
5
6
  /**
6
7
  * Returns a string representing the time from now to the date.
@@ -29,21 +30,26 @@ function toNow(date, locale = config_js_1.config.locale) {
29
30
  const minutes = Math.floor(seconds / 60);
30
31
  const hours = Math.floor(minutes / 60);
31
32
  const days = Math.floor(hours / 24);
33
+ const weeks = Math.floor(days / 7);
32
34
  const months = Math.floor(days / 30);
33
35
  const years = Math.floor(days / 365);
34
- const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
36
+ // Use auto for "now", always for everything else
35
37
  if (seconds < 60) {
36
- return rtf.format(0, "second");
38
+ return (0, rtf_js_1.getRTF)(locale, "auto").format(0, "second");
37
39
  }
40
+ const rtf = (0, rtf_js_1.getRTF)(locale, "always");
38
41
  if (minutes < 60) {
39
42
  return rtf.format(diff > 0 ? minutes : -minutes, "minute");
40
43
  }
41
44
  if (hours < 24) {
42
45
  return rtf.format(diff > 0 ? hours : -hours, "hour");
43
46
  }
44
- if (days < 30) {
47
+ if (days < 7) {
45
48
  return rtf.format(diff > 0 ? days : -days, "day");
46
49
  }
50
+ if (days < 30) {
51
+ return rtf.format(diff > 0 ? weeks : -weeks, "week");
52
+ }
47
53
  if (months < 12) {
48
54
  return rtf.format(diff > 0 ? months : -months, "month");
49
55
  }