chronal 0.0.1 → 0.0.4

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 (70) hide show
  1. package/README.md +11 -7
  2. package/esm/chainable/end-of.d.ts +4 -2
  3. package/esm/chainable/end-of.d.ts.map +1 -1
  4. package/esm/chainable/end-of.js +4 -2
  5. package/esm/chainable/from-now.d.ts +1 -1
  6. package/esm/chainable/from-now.js +1 -1
  7. package/esm/chainable/start-of.d.ts +3 -1
  8. package/esm/chainable/start-of.d.ts.map +1 -1
  9. package/esm/chainable/start-of.js +4 -2
  10. package/esm/chainable/to-now.d.ts +1 -1
  11. package/esm/chainable/to-now.js +1 -1
  12. package/esm/lib/config.d.ts +8 -0
  13. package/esm/lib/config.d.ts.map +1 -0
  14. package/esm/lib/config.js +14 -0
  15. package/esm/lib/end-of.d.ts +13 -1
  16. package/esm/lib/end-of.d.ts.map +1 -1
  17. package/esm/lib/end-of.js +116 -38
  18. package/esm/lib/format-date.js +3 -3
  19. package/esm/lib/from-now.d.ts +1 -1
  20. package/esm/lib/from-now.d.ts.map +1 -1
  21. package/esm/lib/from-now.js +3 -3
  22. package/esm/lib/months.js +2 -2
  23. package/esm/lib/start-of.d.ts +13 -1
  24. package/esm/lib/start-of.d.ts.map +1 -1
  25. package/esm/lib/start-of.js +132 -32
  26. package/esm/lib/to-now.d.ts +1 -1
  27. package/esm/lib/to-now.d.ts.map +1 -1
  28. package/esm/lib/to-now.js +3 -3
  29. package/esm/lib/weekdays.js +2 -2
  30. package/esm/mod.d.ts +1 -1
  31. package/esm/mod.d.ts.map +1 -1
  32. package/esm/mod.js +1 -1
  33. package/package.json +1 -1
  34. package/script/chainable/end-of.d.ts +4 -2
  35. package/script/chainable/end-of.d.ts.map +1 -1
  36. package/script/chainable/end-of.js +4 -2
  37. package/script/chainable/from-now.d.ts +1 -1
  38. package/script/chainable/from-now.js +1 -1
  39. package/script/chainable/start-of.d.ts +3 -1
  40. package/script/chainable/start-of.d.ts.map +1 -1
  41. package/script/chainable/start-of.js +4 -2
  42. package/script/chainable/to-now.d.ts +1 -1
  43. package/script/chainable/to-now.js +1 -1
  44. package/script/lib/config.d.ts +8 -0
  45. package/script/lib/config.d.ts.map +1 -0
  46. package/script/lib/config.js +18 -0
  47. package/script/lib/end-of.d.ts +13 -1
  48. package/script/lib/end-of.d.ts.map +1 -1
  49. package/script/lib/end-of.js +116 -38
  50. package/script/lib/format-date.js +3 -3
  51. package/script/lib/from-now.d.ts +1 -1
  52. package/script/lib/from-now.d.ts.map +1 -1
  53. package/script/lib/from-now.js +3 -3
  54. package/script/lib/months.js +2 -2
  55. package/script/lib/start-of.d.ts +13 -1
  56. package/script/lib/start-of.d.ts.map +1 -1
  57. package/script/lib/start-of.js +132 -32
  58. package/script/lib/to-now.d.ts +1 -1
  59. package/script/lib/to-now.d.ts.map +1 -1
  60. package/script/lib/to-now.js +3 -3
  61. package/script/lib/weekdays.js +2 -2
  62. package/script/mod.d.ts +1 -1
  63. package/script/mod.d.ts.map +1 -1
  64. package/script/mod.js +3 -3
  65. package/esm/lib/set-default-locale.d.ts +0 -4
  66. package/esm/lib/set-default-locale.d.ts.map +0 -1
  67. package/esm/lib/set-default-locale.js +0 -5
  68. package/script/lib/set-default-locale.d.ts +0 -4
  69. package/script/lib/set-default-locale.d.ts.map +0 -1
  70. package/script/lib/set-default-locale.js +0 -9
package/README.md CHANGED
@@ -157,27 +157,31 @@ chronal("2024-01-01")
157
157
 
158
158
  ## Configuration
159
159
 
160
- ### `setDefaultLocale(locale)`
160
+ ### `setConfig(config)`
161
161
 
162
- Set the default locale for all date formatting operations.
162
+ Set the default configuration for all date operations.
163
163
 
164
164
  **Parameters:**
165
-
166
- - `locale` (string) - The locale code (e.g., 'en-US', 'pt-BR', 'fr-FR')
165
+ - `config` (object) - Configuration object with optional properties:
166
+ - `locale` (string) - The locale code (e.g., 'en-US', 'pt-BR', 'fr-FR')
167
+ - `timezone` (string) - The default IANA timezone (e.g., 'UTC', 'America/Sao_Paulo')
167
168
 
168
169
  **Example:**
169
170
 
170
171
  ```typescript
171
- import { formatDate, months, setDefaultLocale } from "chronal";
172
+ import { formatDate, months, setConfig } from "chronal";
172
173
 
173
- // Default is 'en-US'
174
+ // Default is 'en-US' and 'UTC'
174
175
  formatDate(new Date("2024-06-15"), "MMMM"); // 'June'
175
176
 
176
177
  // Change default locale
177
- setDefaultLocale("pt-BR");
178
+ setConfig({ locale: "pt-BR" });
178
179
  formatDate(new Date("2024-06-15"), "MMMM"); // 'junho'
179
180
  months(); // ['janeiro', 'fevereiro', 'março', ...]
180
181
 
182
+ // Change default timezone
183
+ setConfig({ timezone: "America/Sao_Paulo" });
184
+
181
185
  // You can still override per call
182
186
  formatDate(new Date("2024-06-15"), "MMMM", { locale: "fr-FR" }); // 'juin'
183
187
  ```
@@ -1,15 +1,17 @@
1
+ import { endOf as _endOf } from "../lib/end-of.js";
1
2
  import { type Chronal } from "./chronal.js";
2
- import type { Unit } from "../types/unit.js";
3
3
  /**
4
4
  * Returns the end of the specified time unit.
5
5
  *
6
6
  * @param unit - The time unit ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second')
7
+ * @param timezone - Optional IANA timezone string (e.g., 'America/Sao_Paulo'). Defaults to 'UTC'.
7
8
  * @returns A new Chronal instance set to the end of the specified unit (last millisecond)
8
9
  * @example
9
10
  * ```typescript
10
11
  * const c = chronal('2024-06-15T14:35:22.500Z');
11
12
  * c.endOf('day'); // '2024-06-15T23:59:59.999Z'
13
+ * c.endOf('day', 'America/Sao_Paulo'); // '2024-06-16T02:59:59.999Z'
12
14
  * ```
13
15
  */
14
- export declare function endOf(this: Chronal, unit: Unit): Chronal;
16
+ export declare function endOf(this: Chronal, unit: Parameters<typeof _endOf>[1], opt?: Parameters<typeof _endOf>[2]): Chronal;
15
17
  //# sourceMappingURL=end-of.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"end-of.d.ts","sourceRoot":"","sources":["../../src/chainable/end-of.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,OAAO,EAAW,MAAM,cAAc,CAAC;AACrD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;;;;;;;;;GAUG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAGxD"}
1
+ {"version":3,"file":"end-of.d.ts","sourceRoot":"","sources":["../../src/chainable/end-of.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,MAAM,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,KAAK,OAAO,EAAW,MAAM,cAAc,CAAC;AAErD;;;;;;;;;;;;GAYG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,CAAC,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,GAAE,UAAU,CAAC,OAAO,MAAM,CAAC,CAAC,CAAC,CAAM,GAAG,OAAO,CAGxH"}
@@ -4,14 +4,16 @@ import { chronal } from "./chronal.js";
4
4
  * Returns the end of the specified time unit.
5
5
  *
6
6
  * @param unit - The time unit ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second')
7
+ * @param timezone - Optional IANA timezone string (e.g., 'America/Sao_Paulo'). Defaults to 'UTC'.
7
8
  * @returns A new Chronal instance set to the end of the specified unit (last millisecond)
8
9
  * @example
9
10
  * ```typescript
10
11
  * const c = chronal('2024-06-15T14:35:22.500Z');
11
12
  * c.endOf('day'); // '2024-06-15T23:59:59.999Z'
13
+ * c.endOf('day', 'America/Sao_Paulo'); // '2024-06-16T02:59:59.999Z'
12
14
  * ```
13
15
  */
14
- export function endOf(unit) {
15
- const newDate = _endOf(this.date, unit);
16
+ export function endOf(unit, opt = {}) {
17
+ const newDate = _endOf(this.date, unit, opt);
16
18
  return chronal(newDate);
17
19
  }
@@ -2,7 +2,7 @@ import type { Chronal } from "./chronal.js";
2
2
  /**
3
3
  * Returns a string representing how long ago the date was from now.
4
4
  *
5
- * @param locale - The locale to use for formatting (default: DEFAULT_LOCALE)
5
+ * @param locale - The locale to use for formatting (default: en-US)
6
6
  * @returns A human-readable relative time string
7
7
  * @example
8
8
  * ```typescript
@@ -2,7 +2,7 @@ import { fromNow as _fromNow } from "../lib/from-now.js";
2
2
  /**
3
3
  * Returns a string representing how long ago the date was from now.
4
4
  *
5
- * @param locale - The locale to use for formatting (default: DEFAULT_LOCALE)
5
+ * @param locale - The locale to use for formatting (default: en-US)
6
6
  * @returns A human-readable relative time string
7
7
  * @example
8
8
  * ```typescript
@@ -4,12 +4,14 @@ import { type Chronal } from "./chronal.js";
4
4
  * Returns the start of the specified time unit.
5
5
  *
6
6
  * @param unit - The time unit ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second')
7
+ * @param timezone - Optional IANA timezone string (e.g., 'America/Sao_Paulo'). Defaults to 'UTC'.
7
8
  * @returns A new Chronal instance set to the start of the specified unit
8
9
  * @example
9
10
  * ```typescript
10
11
  * const c = chronal('2024-06-15T14:35:22.500Z');
11
12
  * c.startOf('day'); // '2024-06-15T00:00:00.000Z'
13
+ * c.startOf('day', 'America/Sao_Paulo'); // '2024-06-15T03:00:00.000Z'
12
14
  * ```
13
15
  */
14
- export declare function startOf(this: Chronal, unit: Parameters<typeof _startOf>[1]): Chronal;
16
+ export declare function startOf(this: Chronal, unit: Parameters<typeof _startOf>[1], opt?: Parameters<typeof _startOf>[2]): Chronal;
15
17
  //# sourceMappingURL=start-of.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"start-of.d.ts","sourceRoot":"","sources":["../../src/chainable/start-of.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,KAAK,OAAO,EAAW,MAAM,cAAc,CAAC;AAErD;;;;;;;;;;GAUG;AACH,wBAAgB,OAAO,CACrB,IAAI,EAAE,OAAO,EACb,IAAI,EAAE,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,GACnC,OAAO,CAGT"}
1
+ {"version":3,"file":"start-of.d.ts","sourceRoot":"","sources":["../../src/chainable/start-of.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,KAAK,OAAO,EAAW,MAAM,cAAc,CAAC;AAErD;;;;;;;;;;;;GAYG;AACH,wBAAgB,OAAO,CACrB,IAAI,EAAE,OAAO,EACb,IAAI,EAAE,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,EACpC,GAAG,GAAE,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAM,GACvC,OAAO,CAGT"}
@@ -4,14 +4,16 @@ import { chronal } from "./chronal.js";
4
4
  * Returns the start of the specified time unit.
5
5
  *
6
6
  * @param unit - The time unit ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second')
7
+ * @param timezone - Optional IANA timezone string (e.g., 'America/Sao_Paulo'). Defaults to 'UTC'.
7
8
  * @returns A new Chronal instance set to the start of the specified unit
8
9
  * @example
9
10
  * ```typescript
10
11
  * const c = chronal('2024-06-15T14:35:22.500Z');
11
12
  * c.startOf('day'); // '2024-06-15T00:00:00.000Z'
13
+ * c.startOf('day', 'America/Sao_Paulo'); // '2024-06-15T03:00:00.000Z'
12
14
  * ```
13
15
  */
14
- export function startOf(unit) {
15
- const newDate = _startOf(this.date, unit);
16
+ export function startOf(unit, opt = {}) {
17
+ const newDate = _startOf(this.date, unit, opt);
16
18
  return chronal(newDate);
17
19
  }
@@ -3,7 +3,7 @@ import type { Chronal } from "./chronal.js";
3
3
  * Returns a string representing the time from now to the date.
4
4
  * This is the inverse of fromNow() - it shows how long until/since a date from the perspective of looking forward to it.
5
5
  *
6
- * @param locale - The locale to use for formatting (default: DEFAULT_LOCALE)
6
+ * @param locale - The locale to use for formatting (default: en-US)
7
7
  * @returns A human-readable relative time string
8
8
  * @example
9
9
  * ```typescript
@@ -3,7 +3,7 @@ import { toNow as _toNow } from "../lib/to-now.js";
3
3
  * Returns a string representing the time from now to the date.
4
4
  * This is the inverse of fromNow() - it shows how long until/since a date from the perspective of looking forward to it.
5
5
  *
6
- * @param locale - The locale to use for formatting (default: DEFAULT_LOCALE)
6
+ * @param locale - The locale to use for formatting (default: en-US)
7
7
  * @returns A human-readable relative time string
8
8
  * @example
9
9
  * ```typescript
@@ -0,0 +1,8 @@
1
+ type Config = {
2
+ locale: string;
3
+ timezone: string;
4
+ };
5
+ export declare const config: Config;
6
+ export declare function setConfig(c: Partial<Config>): void;
7
+ export {};
8
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/lib/config.ts"],"names":[],"mappings":"AAGA,KAAK,MAAM,GAAG;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,eAAO,MAAM,MAAM,EAAE,MAGpB,CAAA;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,QAQ3C"}
@@ -0,0 +1,14 @@
1
+ const DEFAULT_LOCALE = "en-US";
2
+ const DEFAULT_TZ = "UTC";
3
+ export const config = {
4
+ locale: DEFAULT_LOCALE,
5
+ timezone: DEFAULT_TZ,
6
+ };
7
+ export function setConfig(c) {
8
+ if (c.locale) {
9
+ config.locale = c.locale;
10
+ }
11
+ if (c.timezone) {
12
+ config.timezone = c.timezone;
13
+ }
14
+ }
@@ -1,14 +1,26 @@
1
1
  import type { Unit } from "../types/unit.js";
2
+ type EndOfOptions = {
3
+ tz?: string;
4
+ };
2
5
  /**
3
6
  * Returns the end of the specified time unit for the given date.
4
7
  *
5
8
  * @param date - The original date.
6
9
  * @param unit - The time unit to get the end of ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second').
10
+ * @param timezone - Optional IANA timezone string (e.g., 'America/Sao_Paulo', 'Europe/London'). Defaults to 'UTC'.
7
11
  * @returns A new Date object set to the end of the specified unit (last millisecond).
8
12
  * @example
13
+ * // UTC timezone (default)
9
14
  * const date = new Date('2024-06-15T14:35:22.500Z');
10
15
  * const endOfDay = endOf(date, 'day');
11
16
  * console.log(endOfDay.toISOString()); // '2024-06-15T23:59:59.999Z'
17
+ *
18
+ * @example
19
+ * // With timezone
20
+ * const date = new Date('2024-06-15T14:35:22.500Z');
21
+ * const endOfDay = endOf(date, 'day', 'America/Sao_Paulo'); // GMT-3
22
+ * console.log(endOfDay.toISOString()); // '2024-06-16T02:59:59.999Z' (23:59:59.999 in Sao Paulo)
12
23
  */
13
- export declare function endOf(date: Date, unit: Unit): Date;
24
+ export declare function endOf(date: Date, unit: Unit, opt?: EndOfOptions): Date;
25
+ export {};
14
26
  //# sourceMappingURL=end-of.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"end-of.d.ts","sourceRoot":"","sources":["../../src/lib/end-of.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;;;;;;;;;GAUG;AAEH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,CA2ClD"}
1
+ {"version":3,"file":"end-of.d.ts","sourceRoot":"","sources":["../../src/lib/end-of.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAI7C,KAAK,YAAY,GAAG;IAClB,EAAE,CAAC,EAAE,MAAM,CAAC;CACb,CAAA;AACD;;;;;;;;;;;;;;;;;;GAkBG;AAEH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,GAAE,YAAiB,GAAG,IAAI,CAmH1E"}
package/esm/lib/end-of.js CHANGED
@@ -1,53 +1,131 @@
1
+ import { config } from "./config.js";
2
+ import { startOf } from "./start-of.js";
1
3
  /**
2
4
  * Returns the end of the specified time unit for the given date.
3
5
  *
4
6
  * @param date - The original date.
5
7
  * @param unit - The time unit to get the end of ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second').
8
+ * @param timezone - Optional IANA timezone string (e.g., 'America/Sao_Paulo', 'Europe/London'). Defaults to 'UTC'.
6
9
  * @returns A new Date object set to the end of the specified unit (last millisecond).
7
10
  * @example
11
+ * // UTC timezone (default)
8
12
  * const date = new Date('2024-06-15T14:35:22.500Z');
9
13
  * const endOfDay = endOf(date, 'day');
10
14
  * console.log(endOfDay.toISOString()); // '2024-06-15T23:59:59.999Z'
15
+ *
16
+ * @example
17
+ * // With timezone
18
+ * const date = new Date('2024-06-15T14:35:22.500Z');
19
+ * const endOfDay = endOf(date, 'day', 'America/Sao_Paulo'); // GMT-3
20
+ * console.log(endOfDay.toISOString()); // '2024-06-16T02:59:59.999Z' (23:59:59.999 in Sao Paulo)
11
21
  */
12
- export function endOf(date, unit) {
13
- const time = new Date(date.getTime());
14
- switch (unit) {
15
- case "year": {
16
- time.setUTCMonth(11, 31);
17
- time.setUTCHours(23, 59, 59, 999);
18
- break;
19
- }
20
- case "month": {
21
- // Set to next month's first day, then subtract 1 millisecond
22
- const month = time.getUTCMonth();
23
- time.setUTCMonth(month + 1, 1);
24
- time.setUTCHours(0, 0, 0, 0);
25
- time.setTime(time.getTime() - 1);
26
- break;
27
- }
28
- case "week": {
29
- const day = time.getUTCDay();
30
- const diff = (day === 0 ? 0 : 7) - day; // Adjust when day is Sunday
31
- time.setUTCDate(time.getUTCDate() + diff);
32
- time.setUTCHours(23, 59, 59, 999);
33
- break;
22
+ export function endOf(date, unit, opt = {}) {
23
+ const timezone = opt.tz || config.timezone;
24
+ if (timezone === "UTC") {
25
+ // Original UTC-based logic
26
+ const time = new Date(date.getTime());
27
+ switch (unit) {
28
+ case "year": {
29
+ time.setUTCMonth(11, 31);
30
+ time.setUTCHours(23, 59, 59, 999);
31
+ break;
32
+ }
33
+ case "month": {
34
+ const month = time.getUTCMonth();
35
+ time.setUTCMonth(month + 1, 1);
36
+ time.setUTCHours(0, 0, 0, 0);
37
+ time.setTime(time.getTime() - 1);
38
+ break;
39
+ }
40
+ case "week": {
41
+ const day = time.getUTCDay();
42
+ const diff = (day === 0 ? 0 : 7) - day;
43
+ time.setUTCDate(time.getUTCDate() + diff);
44
+ time.setUTCHours(23, 59, 59, 999);
45
+ break;
46
+ }
47
+ case "day": {
48
+ time.setUTCHours(23, 59, 59, 999);
49
+ break;
50
+ }
51
+ case "hour": {
52
+ time.setUTCMinutes(59, 59, 999);
53
+ break;
54
+ }
55
+ case "minute": {
56
+ time.setUTCSeconds(59, 999);
57
+ break;
58
+ }
59
+ case "second": {
60
+ time.setUTCMilliseconds(999);
61
+ break;
62
+ }
34
63
  }
35
- case "day": {
36
- time.setUTCHours(23, 59, 59, 999);
37
- break;
38
- }
39
- case "hour": {
40
- time.setUTCMinutes(59, 59, 999);
41
- break;
42
- }
43
- case "minute": {
44
- time.setUTCSeconds(59, 999);
45
- break;
46
- }
47
- case "second": {
48
- time.setUTCMilliseconds(999);
49
- break;
64
+ return time;
65
+ }
66
+ else {
67
+ // Timezone-aware: get start of next unit and subtract 1ms
68
+ const unitMap = {
69
+ "second": "second",
70
+ "minute": "minute",
71
+ "hour": "hour",
72
+ "day": "day",
73
+ "week": "week",
74
+ "month": "month",
75
+ "year": "year",
76
+ };
77
+ // Get the start of the current unit
78
+ const start = startOf(date, unit, { tz: timezone });
79
+ // Calculate the next unit boundary
80
+ let nextBoundary;
81
+ switch (unit) {
82
+ case "year": {
83
+ // Add 1 year
84
+ const formatter = new Intl.DateTimeFormat("en-US", {
85
+ timeZone: timezone,
86
+ year: "numeric",
87
+ });
88
+ const year = parseInt(formatter.format(start));
89
+ const nextYear = new Date(start);
90
+ nextYear.setUTCFullYear(year + 1);
91
+ nextBoundary = startOf(nextYear, "year", { tz: timezone });
92
+ break;
93
+ }
94
+ case "month": {
95
+ // Add 1 month
96
+ const nextMonth = new Date(start.getTime() + 32 * 24 * 60 * 60 * 1000);
97
+ nextBoundary = startOf(nextMonth, "month", { tz: timezone });
98
+ break;
99
+ }
100
+ case "week": {
101
+ // Add 7 days
102
+ const nextWeek = new Date(start.getTime() + 7 * 24 * 60 * 60 * 1000);
103
+ nextBoundary = startOf(nextWeek, "week", { tz: timezone });
104
+ break;
105
+ }
106
+ case "day": {
107
+ // Add 1 day
108
+ const nextDay = new Date(start.getTime() + 25 * 60 * 60 * 1000);
109
+ nextBoundary = startOf(nextDay, "day", { tz: timezone });
110
+ break;
111
+ }
112
+ case "hour": {
113
+ // Add 1 hour
114
+ nextBoundary = new Date(start.getTime() + 60 * 60 * 1000);
115
+ break;
116
+ }
117
+ case "minute": {
118
+ // Add 1 minute
119
+ nextBoundary = new Date(start.getTime() + 60 * 1000);
120
+ break;
121
+ }
122
+ case "second": {
123
+ // Add 1 second
124
+ nextBoundary = new Date(start.getTime() + 1000);
125
+ break;
126
+ }
50
127
  }
128
+ // Return 1ms before the next boundary
129
+ return new Date(nextBoundary.getTime() - 1);
51
130
  }
52
- return time;
53
131
  }
@@ -1,6 +1,6 @@
1
1
  import { getDTF } from "../core/dtf.js";
2
- import { DEFAULT_LOCALE } from "./set-default-locale.js";
3
2
  import { months } from "./months.js";
3
+ import { config } from "./config.js";
4
4
  // Pre-compute pad functions to avoid repeated padStart calls
5
5
  const pad2 = (n) => n < 10 ? "0" + n : String(n);
6
6
  const tokenMap = {
@@ -177,8 +177,8 @@ function getPartsWithTZ(date, locale, tz) {
177
177
  * console.log(formatDate(date, 'MMMM D, YYYY', { locale: 'en-US' })); // 'June 15, 2024'
178
178
  */
179
179
  export function formatDate(date, fmt, options = {}) {
180
- const locale = options.locale ?? DEFAULT_LOCALE;
181
- const tz = options.tz ?? "UTC";
180
+ const locale = options.locale ?? config.locale;
181
+ const tz = options.tz ?? config.timezone;
182
182
  // Get compiled format (cached)
183
183
  const compiled = compileFormat(fmt);
184
184
  // Fast path for UTC
@@ -2,7 +2,7 @@
2
2
  * Returns a string representing how long ago the date was from now.
3
3
  *
4
4
  * @param date - The date to compare with now
5
- * @param locale - The locale to use for formatting (default: DEFAULT_LOCALE)
5
+ * @param locale - The locale to use for formatting (default: en-US)
6
6
  * @returns A human-readable relative time string
7
7
  *
8
8
  * @example
@@ -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,MAAuB,GAAG,MAAM,CAmC3E"}
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,9 +1,9 @@
1
- import { DEFAULT_LOCALE } from "./set-default-locale.js";
1
+ import { config } from "./config.js";
2
2
  /**
3
3
  * Returns a string representing how long ago the date was from now.
4
4
  *
5
5
  * @param date - The date to compare with now
6
- * @param locale - The locale to use for formatting (default: DEFAULT_LOCALE)
6
+ * @param locale - The locale to use for formatting (default: en-US)
7
7
  * @returns A human-readable relative time string
8
8
  *
9
9
  * @example
@@ -17,7 +17,7 @@ import { DEFAULT_LOCALE } from "./set-default-locale.js";
17
17
  * fromNow(inTwoHours, "es-ES"); // "dentro de 2 horas"
18
18
  * ```
19
19
  */
20
- export function fromNow(date, locale = DEFAULT_LOCALE) {
20
+ export function fromNow(date, locale = config.locale) {
21
21
  const now = Date.now();
22
22
  const diff = now - date.getTime();
23
23
  const absDiff = Math.abs(diff);
package/esm/lib/months.js CHANGED
@@ -1,4 +1,4 @@
1
- import { DEFAULT_LOCALE } from "./set-default-locale.js";
1
+ import { config } from "./config.js";
2
2
  const monthsCache = new Map();
3
3
  /**
4
4
  * Returns an array of month names for the specified locale and format.
@@ -12,7 +12,7 @@ const monthsCache = new Map();
12
12
  * console.log(months('pt-BR', 'long')); // ['janeiro', 'fevereiro', 'março', ...]
13
13
  */
14
14
  export function months(format = "long", opt = {}) {
15
- const locale = opt.locale ?? DEFAULT_LOCALE;
15
+ const locale = opt.locale ?? config.locale;
16
16
  const key = `${locale}|${format}`;
17
17
  const cached = monthsCache.get(key);
18
18
  if (cached)
@@ -1,14 +1,26 @@
1
1
  import type { Unit } from "../types/unit.js";
2
+ type StartOfOptions = {
3
+ tz?: string;
4
+ };
2
5
  /**
3
6
  * Returns the start of the specified time unit for the given date.
4
7
  *
5
8
  * @param date - The original date.
6
9
  * @param unit - The time unit to get the start of ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second').
10
+ * @param timezone - Optional IANA timezone string (e.g., 'America/Sao_Paulo', 'Europe/London'). Defaults to 'UTC'.
7
11
  * @returns A new Date object set to the start of the specified unit.
8
12
  * @example
13
+ * // UTC timezone (default)
9
14
  * const date = new Date('2024-06-15T14:35:22.500Z');
10
15
  * const startOfDay = startOf(date, 'day');
11
16
  * console.log(startOfDay.toISOString()); // '2024-06-15T00:00:00.000Z'
17
+ *
18
+ * @example
19
+ * // With timezone
20
+ * const date = new Date('2024-06-15T14:35:22.500Z');
21
+ * const startOfDay = startOf(date, 'day', 'America/Sao_Paulo'); // GMT-3
22
+ * console.log(startOfDay.toISOString()); // '2024-06-15T03:00:00.000Z' (00:00 in Sao Paulo)
12
23
  */
13
- export declare function startOf(date: Date, unit: Unit): Date;
24
+ export declare function startOf(date: Date, unit: Unit, opt?: StartOfOptions): Date;
25
+ export {};
14
26
  //# sourceMappingURL=start-of.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"start-of.d.ts","sourceRoot":"","sources":["../../src/lib/start-of.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;;;;;;;;;GAUG;AAEH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,CAwCpD"}
1
+ {"version":3,"file":"start-of.d.ts","sourceRoot":"","sources":["../../src/lib/start-of.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAG7C,KAAK,cAAc,GAAG;IACpB,EAAE,CAAC,EAAE,MAAM,CAAC;CACb,CAAA;AAED;;;;;;;;;;;;;;;;;;GAkBG;AAEH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,GAAE,cAAmB,GAAG,IAAI,CA4I9E"}