@formkit/tempo 1.3.0 → 1.4.0

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.
@@ -2,9 +2,7 @@
2
2
  import { date } from "./date.mjs";
3
3
  function dayOfYear(inputDate) {
4
4
  const d = date(inputDate);
5
- return Math.round(
6
- (new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0).getTime() - new Date(d.getFullYear(), 0, 0).getTime()) / 864e5
7
- );
5
+ return (Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) - Date.UTC(d.getFullYear(), 0, 0)) / 864e5;
8
6
  }
9
7
  export {
10
8
  dayOfYear
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/dayOfYear.ts"],"sourcesContent":["import { date } from \"./date\"\nimport type { MaybeDateInput } from \"./types\"\n\n/**\n * Gets the what day of the year a given date is. For example, August 1st is\n * the 213th day of the year on non- years and 214th on leap years.\n * @param [inputDate] - The input date or nothing for the current day.\n */\nexport function dayOfYear(inputDate?: MaybeDateInput): number {\n const d = date(inputDate)\n return Math.round(\n (new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0).getTime() -\n new Date(d.getFullYear(), 0, 0).getTime()) /\n 86400000\n )\n}\n"],"mappings":";AAAA,SAAS,YAAY;AAQd,SAAS,UAAU,WAAoC;AAC5D,QAAM,IAAI,KAAK,SAAS;AACxB,SAAO,KAAK;AAAA,KACT,IAAI,KAAK,EAAE,YAAY,GAAG,EAAE,SAAS,GAAG,EAAE,QAAQ,GAAG,GAAG,CAAC,EAAE,QAAQ,IAClE,IAAI,KAAK,EAAE,YAAY,GAAG,GAAG,CAAC,EAAE,QAAQ,KACxC;AAAA,EACJ;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/dayOfYear.ts"],"sourcesContent":["import { date } from \"./date\"\nimport type { MaybeDateInput } from \"./types\"\n\n/**\n * Gets the what day of the year a given date is. For example, August 1st is\n * the 213th day of the year on non- years and 214th on leap years.\n * @param [inputDate] - The input date or nothing for the current day.\n */\nexport function dayOfYear(inputDate?: MaybeDateInput): number {\n const d = date(inputDate)\n // Count calendar days independently of local offset changes or skipped dates.\n return (\n (Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) -\n Date.UTC(d.getFullYear(), 0, 0)) /\n 86400000\n )\n}\n"],"mappings":";AAAA,SAAS,YAAY;AAQd,SAAS,UAAU,WAAoC;AAC5D,QAAM,IAAI,KAAK,SAAS;AAExB,UACG,KAAK,IAAI,EAAE,YAAY,GAAG,EAAE,SAAS,GAAG,EAAE,QAAQ,CAAC,IAClD,KAAK,IAAI,EAAE,YAAY,GAAG,GAAG,CAAC,KAC9B;AAEN;","names":[]}
package/dist/diffDays.mjs CHANGED
@@ -1,13 +1,11 @@
1
1
  // src/diffDays.ts
2
- import { diffMilliseconds } from "./diffMilliseconds.mjs";
2
+ import { date } from "./date.mjs";
3
3
  import { diffRound } from "./diffRound.mjs";
4
4
  function diffDays(dateA, dateB, roundingMethod) {
5
- return diffRound(
6
- // @ts-ignore
7
- diffMilliseconds(dateA, dateB) / 864e5,
8
- // hour * 24
9
- roundingMethod
10
- );
5
+ const left = date(dateA);
6
+ const right = date(dateB);
7
+ const offset = (right.getTimezoneOffset() - left.getTimezoneOffset()) * 6e4;
8
+ return diffRound((+left - +right + offset) / 864e5, roundingMethod);
11
9
  }
12
10
  export {
13
11
  diffDays
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/diffDays.ts"],"sourcesContent":["import { diffMilliseconds } from \"./diffMilliseconds\"\nimport type { DateInput, MaybeDateInput } from \"./types\"\nimport { diffRound, type DiffRoundingMethod } from \"./diffRound\"\n\n/**\n * Returns the difference between 2 dates in days.\n * @param dateA - A date to compare with the right date\n * @param [dateB] - A date to compare with the left date or nothing to compare with the current time\n * @param [roundingMethod] - the rounding method to use, default: trunc\n */\nexport function diffDays(\n dateA: DateInput,\n dateB?: MaybeDateInput,\n roundingMethod?: DiffRoundingMethod\n): number\n/**\n * Returns the difference between 2 dates in days.\n * @param [dateA] - A date to compare with the right date or null to compare with the current time\n * @param dateB - A date to compare with the left date\n * @param [roundingMethod] - the rounding method to use, default: trunc\n */\nexport function diffDays(\n dateA: MaybeDateInput,\n dateB: DateInput,\n roundingMethod?: DiffRoundingMethod\n): number\n\nexport function diffDays(\n dateA: MaybeDateInput,\n dateB?: MaybeDateInput,\n roundingMethod?: DiffRoundingMethod\n): number {\n return diffRound(\n // @ts-ignore\n diffMilliseconds(dateA, dateB) / 86_400_000, // hour * 24\n roundingMethod\n )\n}\n"],"mappings":";AAAA,SAAS,wBAAwB;AAEjC,SAAS,iBAA0C;AAyB5C,SAAS,SACd,OACA,OACA,gBACQ;AACR,SAAO;AAAA;AAAA,IAEL,iBAAiB,OAAO,KAAK,IAAI;AAAA;AAAA,IACjC;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/diffDays.ts"],"sourcesContent":["import { date } from \"./date\"\nimport type { DateInput, MaybeDateInput } from \"./types\"\nimport { diffRound, type DiffRoundingMethod } from \"./diffRound\"\n\n/**\n * Returns the difference between 2 dates in days.\n * @param dateA - A date to compare with the right date\n * @param [dateB] - A date to compare with the left date or nothing to compare with the current time\n * @param [roundingMethod] - the rounding method to use, default: trunc\n */\nexport function diffDays(\n dateA: DateInput,\n dateB?: MaybeDateInput,\n roundingMethod?: DiffRoundingMethod\n): number\n/**\n * Returns the difference between 2 dates in days.\n * @param [dateA] - A date to compare with the right date or null to compare with the current time\n * @param dateB - A date to compare with the left date\n * @param [roundingMethod] - the rounding method to use, default: trunc\n */\nexport function diffDays(\n dateA: MaybeDateInput,\n dateB: DateInput,\n roundingMethod?: DiffRoundingMethod\n): number\n\nexport function diffDays(\n dateA: MaybeDateInput,\n dateB?: MaybeDateInput,\n roundingMethod?: DiffRoundingMethod\n): number {\n const left = date(dateA)\n const right = date(dateB)\n // Count local days: a DST change makes one day 23 or 25 hours long.\n const offset = (right.getTimezoneOffset() - left.getTimezoneOffset()) * 60_000\n return diffRound((+left - +right + offset) / 86_400_000, roundingMethod) // hour * 24\n}\n"],"mappings":";AAAA,SAAS,YAAY;AAErB,SAAS,iBAA0C;AAyB5C,SAAS,SACd,OACA,OACA,gBACQ;AACR,QAAM,OAAO,KAAK,KAAK;AACvB,QAAM,QAAQ,KAAK,KAAK;AAExB,QAAM,UAAU,MAAM,kBAAkB,IAAI,KAAK,kBAAkB,KAAK;AACxE,SAAO,WAAW,CAAC,OAAO,CAAC,QAAQ,UAAU,OAAY,cAAc;AACzE;","names":[]}
@@ -1,6 +1,9 @@
1
1
  // src/diffMonths.ts
2
2
  import { date } from "./date.mjs";
3
3
  import { monthDays } from "./monthDays.mjs";
4
+ function timeOfDay(d) {
5
+ return ((d.getHours() * 60 + d.getMinutes()) * 60 + d.getSeconds()) * 1e3 + d.getMilliseconds();
6
+ }
4
7
  function diffMonths(dateA, dateB) {
5
8
  const l = date(dateA);
6
9
  const r = date(dateB);
@@ -11,7 +14,7 @@ function diffMonths(dateA, dateB) {
11
14
  let months = (l.getFullYear() - r.getFullYear()) * 12 + (l.getMonth() - r.getMonth());
12
15
  const ld = l.getDate();
13
16
  const rd = r.getDate();
14
- if (ld < rd) {
17
+ if (ld < rd || ld == rd && timeOfDay(l) < timeOfDay(r)) {
15
18
  const lm = monthDays(l);
16
19
  if (!(lm == ld && lm < rd)) {
17
20
  months--;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/diffMonths.ts"],"sourcesContent":["import { date } from \"./date\"\nimport type { DateInput, MaybeDateInput } from \"./types\"\nimport { monthDays } from \"./monthDays\"\n\n/**\n * Returns the difference between 2 dates in months.\n * @param dateA - A date to compare with the dateB date\n * @param [dateB] - A date to compare with the dateA date or nothing to compare with the current time\n */\nexport function diffMonths(dateA: DateInput, dateB?: MaybeDateInput): number\n/**\n * Returns the difference between 2 dates in months.\n * @param [dateA] - A date to compare with the dateB date or null to compare with the current time\n * @param dateB - A date to compare with the dateA date\n */\nexport function diffMonths(dateA: MaybeDateInput, dateB: DateInput): number\n\nexport function diffMonths(dateA: MaybeDateInput, dateB?: MaybeDateInput): number {\n const l = date(dateA)\n const r = date(dateB)\n // if the dateB one is bigger, we switch them around as it's easier to do\n if (l < r) {\n const rs = diffMonths(r, l)\n return rs == 0 ? 0 : -rs\n }\n\n // we first get the amount of calendar months\n let months = (l.getFullYear() - r.getFullYear()) * 12 + (l.getMonth() - r.getMonth())\n\n const ld = l.getDate()\n const rd = r.getDate()\n\n // if no full month has passed we may subtract a month from the calendar months so we get the amount of full months\n if (ld < rd) {\n // in case dateA date is the last day of the month & the dateB date is higher, we don't subtract as a full month did actually pass\n const lm = monthDays(l)\n if (!(lm == ld && lm < rd)) {\n months--\n }\n }\n //ensures we don't give back -0\n return months == 0 ? 0 : months\n}\n"],"mappings":";AAAA,SAAS,YAAY;AAErB,SAAS,iBAAiB;AAenB,SAAS,WAAW,OAAuB,OAAgC;AAChF,QAAM,IAAI,KAAK,KAAK;AACpB,QAAM,IAAI,KAAK,KAAK;AAEpB,MAAI,IAAI,GAAG;AACT,UAAM,KAAK,WAAW,GAAG,CAAC;AAC1B,WAAO,MAAM,IAAI,IAAI,CAAC;AAAA,EACxB;AAGA,MAAI,UAAU,EAAE,YAAY,IAAI,EAAE,YAAY,KAAK,MAAM,EAAE,SAAS,IAAI,EAAE,SAAS;AAEnF,QAAM,KAAK,EAAE,QAAQ;AACrB,QAAM,KAAK,EAAE,QAAQ;AAGrB,MAAI,KAAK,IAAI;AAEX,UAAM,KAAK,UAAU,CAAC;AACtB,QAAI,EAAE,MAAM,MAAM,KAAK,KAAK;AAC1B;AAAA,IACF;AAAA,EACF;AAEA,SAAO,UAAU,IAAI,IAAI;AAC3B;","names":[]}
1
+ {"version":3,"sources":["../src/diffMonths.ts"],"sourcesContent":["import { date } from \"./date\"\nimport type { DateInput, MaybeDateInput } from \"./types\"\nimport { monthDays } from \"./monthDays\"\n\n/**\n * Milliseconds since local midnight.\n */\nfunction timeOfDay(d: Date): number {\n return (\n ((d.getHours() * 60 + d.getMinutes()) * 60 + d.getSeconds()) * 1000 +\n d.getMilliseconds()\n )\n}\n\n/**\n * Returns the difference between 2 dates in months.\n * @param dateA - A date to compare with the dateB date\n * @param [dateB] - A date to compare with the dateA date or nothing to compare with the current time\n */\nexport function diffMonths(dateA: DateInput, dateB?: MaybeDateInput): number\n/**\n * Returns the difference between 2 dates in months.\n * @param [dateA] - A date to compare with the dateB date or null to compare with the current time\n * @param dateB - A date to compare with the dateA date\n */\nexport function diffMonths(dateA: MaybeDateInput, dateB: DateInput): number\n\nexport function diffMonths(dateA: MaybeDateInput, dateB?: MaybeDateInput): number {\n const l = date(dateA)\n const r = date(dateB)\n // if the dateB one is bigger, we switch them around as it's easier to do\n if (l < r) {\n const rs = diffMonths(r, l)\n return rs == 0 ? 0 : -rs\n }\n\n // we first get the amount of calendar months\n let months = (l.getFullYear() - r.getFullYear()) * 12 + (l.getMonth() - r.getMonth())\n\n const ld = l.getDate()\n const rd = r.getDate()\n\n // if no full month has passed we may subtract a month from the calendar months so we get the amount of full months\n if (ld < rd || (ld == rd && timeOfDay(l) < timeOfDay(r))) {\n // in case dateA date is the last day of the month & the dateB date is higher, we don't subtract as a full month did actually pass\n const lm = monthDays(l)\n if (!(lm == ld && lm < rd)) {\n months--\n }\n }\n //ensures we don't give back -0\n return months == 0 ? 0 : months\n}\n"],"mappings":";AAAA,SAAS,YAAY;AAErB,SAAS,iBAAiB;AAK1B,SAAS,UAAU,GAAiB;AAClC,WACI,EAAE,SAAS,IAAI,KAAK,EAAE,WAAW,KAAK,KAAK,EAAE,WAAW,KAAK,MAC/D,EAAE,gBAAgB;AAEtB;AAeO,SAAS,WAAW,OAAuB,OAAgC;AAChF,QAAM,IAAI,KAAK,KAAK;AACpB,QAAM,IAAI,KAAK,KAAK;AAEpB,MAAI,IAAI,GAAG;AACT,UAAM,KAAK,WAAW,GAAG,CAAC;AAC1B,WAAO,MAAM,IAAI,IAAI,CAAC;AAAA,EACxB;AAGA,MAAI,UAAU,EAAE,YAAY,IAAI,EAAE,YAAY,KAAK,MAAM,EAAE,SAAS,IAAI,EAAE,SAAS;AAEnF,QAAM,KAAK,EAAE,QAAQ;AACrB,QAAM,KAAK,EAAE,QAAQ;AAGrB,MAAI,KAAK,MAAO,MAAM,MAAM,UAAU,CAAC,IAAI,UAAU,CAAC,GAAI;AAExD,UAAM,KAAK,UAAU,CAAC;AACtB,QAAI,EAAE,MAAM,MAAM,KAAK,KAAK;AAC1B;AAAA,IACF;AAAA,EACF;AAEA,SAAO,UAAU,IAAI,IAAI;AAC3B;","names":[]}
@@ -1,19 +1,19 @@
1
- import { DateInput } from './types.js';
1
+ import { DateInput, MaybeDateInput } from './types.js';
2
2
  import { DiffRoundingMethod } from './diffRound.js';
3
3
 
4
4
  /**
5
- * Returns the difference between 2 dates in days.
5
+ * Returns the difference between 2 dates in weeks.
6
6
  * @param dateA - A date to compare with the right date
7
7
  * @param [dateB] - A date to compare with the left date or nothing to compare with the current time
8
8
  * @param [roundingMethod] - the rounding method to use, default: trunc
9
9
  */
10
- declare function diffWeeks(dateA: DateInput, dateB: DateInput, roundingMethod?: DiffRoundingMethod): number;
10
+ declare function diffWeeks(dateA: DateInput, dateB?: MaybeDateInput, roundingMethod?: DiffRoundingMethod): number;
11
11
  /**
12
- * Returns the difference between 2 dates in days.
12
+ * Returns the difference between 2 dates in weeks.
13
13
  * @param [dateA] - A date to compare with the right date or null to compare with the current time
14
14
  * @param dateB - A date to compare with the left date
15
15
  * @param [roundingMethod] - the rounding method to use, default: trunc
16
16
  */
17
- declare function diffWeeks(dateA: DateInput, dateB: DateInput, roundingMethod?: DiffRoundingMethod): number;
17
+ declare function diffWeeks(dateA: MaybeDateInput, dateB: DateInput, roundingMethod?: DiffRoundingMethod): number;
18
18
 
19
19
  export { diffWeeks };
@@ -1,12 +1,11 @@
1
1
  // src/diffWeeks.ts
2
- import { diffMilliseconds } from "./diffMilliseconds.mjs";
2
+ import { date } from "./date.mjs";
3
3
  import { diffRound } from "./diffRound.mjs";
4
4
  function diffWeeks(dateA, dateB, roundingMethod) {
5
- return diffRound(
6
- diffMilliseconds(dateA, dateB) / 6048e5,
7
- // day * 7
8
- roundingMethod
9
- );
5
+ const left = date(dateA);
6
+ const right = date(dateB);
7
+ const offset = (right.getTimezoneOffset() - left.getTimezoneOffset()) * 6e4;
8
+ return diffRound((+left - +right + offset) / 6048e5, roundingMethod);
10
9
  }
11
10
  export {
12
11
  diffWeeks
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/diffWeeks.ts"],"sourcesContent":["import { diffMilliseconds } from \"./diffMilliseconds\"\nimport { DateInput, MaybeDateInput } from \"./types\"\nimport { diffRound, type DiffRoundingMethod } from \"./diffRound\"\n\n/**\n * Returns the difference between 2 dates in days.\n * @param dateA - A date to compare with the right date\n * @param [dateB] - A date to compare with the left date or nothing to compare with the current time\n * @param [roundingMethod] - the rounding method to use, default: trunc\n */\nexport function diffWeeks(\n dateA: DateInput,\n dateB: DateInput,\n roundingMethod?: DiffRoundingMethod\n): number\n\n/**\n * Returns the difference between 2 dates in days.\n * @param [dateA] - A date to compare with the right date or null to compare with the current time\n * @param dateB - A date to compare with the left date\n * @param [roundingMethod] - the rounding method to use, default: trunc\n */\nexport function diffWeeks(\n dateA: DateInput,\n dateB: DateInput,\n roundingMethod?: DiffRoundingMethod\n): number\n\nexport function diffWeeks(\n dateA: MaybeDateInput,\n dateB: DateInput,\n roundingMethod?: DiffRoundingMethod\n): number {\n return diffRound(\n diffMilliseconds(dateA, dateB) / 604800000, // day * 7\n roundingMethod\n )\n}\n"],"mappings":";AAAA,SAAS,wBAAwB;AAEjC,SAAS,iBAA0C;AA0B5C,SAAS,UACd,OACA,OACA,gBACQ;AACR,SAAO;AAAA,IACL,iBAAiB,OAAO,KAAK,IAAI;AAAA;AAAA,IACjC;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/diffWeeks.ts"],"sourcesContent":["import { date } from \"./date\"\nimport { DateInput, MaybeDateInput } from \"./types\"\nimport { diffRound, type DiffRoundingMethod } from \"./diffRound\"\n\n/**\n * Returns the difference between 2 dates in weeks.\n * @param dateA - A date to compare with the right date\n * @param [dateB] - A date to compare with the left date or nothing to compare with the current time\n * @param [roundingMethod] - the rounding method to use, default: trunc\n */\nexport function diffWeeks(\n dateA: DateInput,\n dateB?: MaybeDateInput,\n roundingMethod?: DiffRoundingMethod\n): number\n\n/**\n * Returns the difference between 2 dates in weeks.\n * @param [dateA] - A date to compare with the right date or null to compare with the current time\n * @param dateB - A date to compare with the left date\n * @param [roundingMethod] - the rounding method to use, default: trunc\n */\nexport function diffWeeks(\n dateA: MaybeDateInput,\n dateB: DateInput,\n roundingMethod?: DiffRoundingMethod\n): number\n\nexport function diffWeeks(\n dateA: MaybeDateInput,\n dateB?: MaybeDateInput,\n roundingMethod?: DiffRoundingMethod\n): number {\n const left = date(dateA)\n const right = date(dateB)\n // Count local days: a DST change makes one day 23 or 25 hours long.\n const offset = (right.getTimezoneOffset() - left.getTimezoneOffset()) * 60_000\n return diffRound((+left - +right + offset) / 604800000, roundingMethod) // day * 7\n}\n"],"mappings":";AAAA,SAAS,YAAY;AAErB,SAAS,iBAA0C;AA0B5C,SAAS,UACd,OACA,OACA,gBACQ;AACR,QAAM,OAAO,KAAK,KAAK;AACvB,QAAM,QAAQ,KAAK,KAAK;AAExB,QAAM,UAAU,MAAM,kBAAkB,IAAI,KAAK,kBAAkB,KAAK;AACxE,SAAO,WAAW,CAAC,OAAO,CAAC,QAAQ,UAAU,QAAW,cAAc;AACxE;","names":[]}
package/dist/hourEnd.mjs CHANGED
@@ -2,6 +2,11 @@
2
2
  import { date } from "./date.mjs";
3
3
  function hourEnd(inputDate) {
4
4
  const d = date(inputDate);
5
+ const shifted = new Date(
6
+ d.getTime() + ((59 - d.getMinutes()) * 60 + (59 - d.getSeconds())) * 1e3 + (999 - d.getMilliseconds())
7
+ );
8
+ if (shifted.getMinutes() === 59 && shifted.getHours() === d.getHours())
9
+ return shifted;
5
10
  d.setMinutes(59, 59, 999);
6
11
  return d;
7
12
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hourEnd.ts"],"sourcesContent":["import { date } from \"./date\"\nimport type { MaybeDateInput } from \"./types\"\n\n/**\n * Returns a Date object for end of the given hour.\n * @param [inputDate] - A string, Date object or nothing for the current time\n */\nexport function hourEnd(inputDate?: MaybeDateInput): Date {\n const d = date(inputDate)\n d.setMinutes(59, 59, 999)\n return d\n}\n"],"mappings":";AAAA,SAAS,YAAY;AAOd,SAAS,QAAQ,WAAkC;AACxD,QAAM,IAAI,KAAK,SAAS;AACxB,IAAE,WAAW,IAAI,IAAI,GAAG;AACxB,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../src/hourEnd.ts"],"sourcesContent":["import { date } from \"./date\"\nimport type { MaybeDateInput } from \"./types\"\n\n/**\n * Returns a Date object for end of the given hour.\n * @param [inputDate] - A string, Date object or nothing for the current time\n */\nexport function hourEnd(inputDate?: MaybeDateInput): Date {\n const d = date(inputDate)\n // Shift the timestamp instead of using local setters, which pick the first\n // occurrence of a repeated hour (DST fall-back).\n const shifted = new Date(\n d.getTime() +\n ((59 - d.getMinutes()) * 60 + (59 - d.getSeconds())) * 1000 +\n (999 - d.getMilliseconds())\n )\n // A sub-hour offset change (e.g. 30 min in Lord_Howe) breaks the shift.\n if (shifted.getMinutes() === 59 && shifted.getHours() === d.getHours())\n return shifted\n d.setMinutes(59, 59, 999)\n return d\n}\n"],"mappings":";AAAA,SAAS,YAAY;AAOd,SAAS,QAAQ,WAAkC;AACxD,QAAM,IAAI,KAAK,SAAS;AAGxB,QAAM,UAAU,IAAI;AAAA,IAClB,EAAE,QAAQ,MACN,KAAK,EAAE,WAAW,KAAK,MAAM,KAAK,EAAE,WAAW,MAAM,OACtD,MAAM,EAAE,gBAAgB;AAAA,EAC7B;AAEA,MAAI,QAAQ,WAAW,MAAM,MAAM,QAAQ,SAAS,MAAM,EAAE,SAAS;AACnE,WAAO;AACT,IAAE,WAAW,IAAI,IAAI,GAAG;AACxB,SAAO;AACT;","names":[]}
@@ -2,6 +2,10 @@
2
2
  import { date } from "./date.mjs";
3
3
  function hourStart(inputDate) {
4
4
  const d = date(inputDate);
5
+ const shifted = new Date(
6
+ d.getTime() - (d.getMinutes() * 60 + d.getSeconds()) * 1e3 - d.getMilliseconds()
7
+ );
8
+ if (shifted.getMinutes() === 0 && shifted.getHours() === d.getHours()) return shifted;
5
9
  d.setMinutes(0, 0, 0);
6
10
  return d;
7
11
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hourStart.ts"],"sourcesContent":["import { date } from \"./date\"\nimport type { MaybeDateInput } from \"./types\"\n\n/**\n * Returns a Date object for start of the given hour.\n * @param [inputDate] - A string, Date object or nothing for the current time\n */\nexport function hourStart(inputDate?: MaybeDateInput): Date {\n const d = date(inputDate)\n d.setMinutes(0, 0, 0)\n return d\n}\n"],"mappings":";AAAA,SAAS,YAAY;AAOd,SAAS,UAAU,WAAkC;AAC1D,QAAM,IAAI,KAAK,SAAS;AACxB,IAAE,WAAW,GAAG,GAAG,CAAC;AACpB,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../src/hourStart.ts"],"sourcesContent":["import { date } from \"./date\"\nimport type { MaybeDateInput } from \"./types\"\n\n/**\n * Returns a Date object for start of the given hour.\n * @param [inputDate] - A string, Date object or nothing for the current time\n */\nexport function hourStart(inputDate?: MaybeDateInput): Date {\n const d = date(inputDate)\n // Shift the timestamp instead of using local setters, which pick the first\n // occurrence of a repeated hour (DST fall-back).\n const shifted = new Date(\n d.getTime() -\n (d.getMinutes() * 60 + d.getSeconds()) * 1000 -\n d.getMilliseconds()\n )\n // A sub-hour offset change (e.g. 30 min in Lord_Howe) breaks the shift.\n if (shifted.getMinutes() === 0 && shifted.getHours() === d.getHours()) return shifted\n d.setMinutes(0, 0, 0)\n return d\n}\n"],"mappings":";AAAA,SAAS,YAAY;AAOd,SAAS,UAAU,WAAkC;AAC1D,QAAM,IAAI,KAAK,SAAS;AAGxB,QAAM,UAAU,IAAI;AAAA,IAClB,EAAE,QAAQ,KACP,EAAE,WAAW,IAAI,KAAK,EAAE,WAAW,KAAK,MACzC,EAAE,gBAAgB;AAAA,EACtB;AAEA,MAAI,QAAQ,WAAW,MAAM,KAAK,QAAQ,SAAS,MAAM,EAAE,SAAS,EAAG,QAAO;AAC9E,IAAE,WAAW,GAAG,GAAG,CAAC;AACpB,SAAO;AACT;","names":[]}
package/dist/index.cjs CHANGED
@@ -573,9 +573,7 @@ function tzDate(inputDate, tz) {
573
573
  // src/dayOfYear.ts
574
574
  function dayOfYear(inputDate) {
575
575
  const d = date(inputDate);
576
- return Math.round(
577
- (new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0).getTime() - new Date(d.getFullYear(), 0, 0).getTime()) / 864e5
578
- );
576
+ return (Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) - Date.UTC(d.getFullYear(), 0, 0)) / 864e5;
579
577
  }
580
578
 
581
579
  // src/dayEnd.ts
@@ -853,6 +851,11 @@ function fourDigitYear(value) {
853
851
  // src/hourEnd.ts
854
852
  function hourEnd(inputDate) {
855
853
  const d = date(inputDate);
854
+ const shifted = new Date(
855
+ d.getTime() + ((59 - d.getMinutes()) * 60 + (59 - d.getSeconds())) * 1e3 + (999 - d.getMilliseconds())
856
+ );
857
+ if (shifted.getMinutes() === 59 && shifted.getHours() === d.getHours())
858
+ return shifted;
856
859
  d.setMinutes(59, 59, 999);
857
860
  return d;
858
861
  }
@@ -860,6 +863,10 @@ function hourEnd(inputDate) {
860
863
  // src/hourStart.ts
861
864
  function hourStart(inputDate) {
862
865
  const d = date(inputDate);
866
+ const shifted = new Date(
867
+ d.getTime() - (d.getMinutes() * 60 + d.getSeconds()) * 1e3 - d.getMilliseconds()
868
+ );
869
+ if (shifted.getMinutes() === 0 && shifted.getHours() === d.getHours()) return shifted;
863
870
  d.setMinutes(0, 0, 0);
864
871
  return d;
865
872
  }
@@ -867,14 +874,16 @@ function hourStart(inputDate) {
867
874
  // src/minuteEnd.ts
868
875
  function minuteEnd(inputDate) {
869
876
  const d = date(inputDate);
870
- d.setSeconds(59, 999);
877
+ d.setTime(
878
+ d.getTime() + (59 - d.getSeconds()) * 1e3 + (999 - d.getMilliseconds())
879
+ );
871
880
  return d;
872
881
  }
873
882
 
874
883
  // src/minuteStart.ts
875
884
  function minuteStart(inputDate) {
876
885
  const d = date(inputDate);
877
- d.setSeconds(0, 0);
886
+ d.setTime(d.getTime() - d.getSeconds() * 1e3 - d.getMilliseconds());
878
887
  return d;
879
888
  }
880
889
 
@@ -899,23 +908,23 @@ function nearestDay(inputDate, search, constraint = 7) {
899
908
  const d = date(inputDate);
900
909
  switch (constraint) {
901
910
  case "month":
902
- decrements = d.getDate();
911
+ decrements = d.getDate() - 1;
903
912
  increments = monthDays(d) - d.getDate();
904
913
  break;
905
914
  case "week":
906
- decrements = d.getDay() + 1;
915
+ decrements = d.getDay();
907
916
  increments = 6 - d.getDay();
908
917
  break;
909
918
  case "year":
910
919
  const total = yearDays(d);
911
920
  const day = dayOfYear(d);
912
- decrements = day;
921
+ decrements = day - 1;
913
922
  increments = total - day;
914
923
  break;
915
924
  default:
916
925
  increments = decrements = constraint;
917
926
  }
918
- for (let i = 0; i <= increments || i < decrements; i++) {
927
+ for (let i = 0; i <= increments || i <= decrements; i++) {
919
928
  if (i <= increments) {
920
929
  const next = addDay(d, i);
921
930
  if (search(next)) return next;
@@ -1356,24 +1365,24 @@ function diffHours(dateA, dateB, roundingMethod) {
1356
1365
 
1357
1366
  // src/diffDays.ts
1358
1367
  function diffDays(dateA, dateB, roundingMethod) {
1359
- return diffRound(
1360
- // @ts-ignore
1361
- diffMilliseconds(dateA, dateB) / 864e5,
1362
- // hour * 24
1363
- roundingMethod
1364
- );
1368
+ const left = date(dateA);
1369
+ const right = date(dateB);
1370
+ const offset2 = (right.getTimezoneOffset() - left.getTimezoneOffset()) * 6e4;
1371
+ return diffRound((+left - +right + offset2) / 864e5, roundingMethod);
1365
1372
  }
1366
1373
 
1367
1374
  // src/diffWeeks.ts
1368
1375
  function diffWeeks(dateA, dateB, roundingMethod) {
1369
- return diffRound(
1370
- diffMilliseconds(dateA, dateB) / 6048e5,
1371
- // day * 7
1372
- roundingMethod
1373
- );
1376
+ const left = date(dateA);
1377
+ const right = date(dateB);
1378
+ const offset2 = (right.getTimezoneOffset() - left.getTimezoneOffset()) * 6e4;
1379
+ return diffRound((+left - +right + offset2) / 6048e5, roundingMethod);
1374
1380
  }
1375
1381
 
1376
1382
  // src/diffMonths.ts
1383
+ function timeOfDay(d) {
1384
+ return ((d.getHours() * 60 + d.getMinutes()) * 60 + d.getSeconds()) * 1e3 + d.getMilliseconds();
1385
+ }
1377
1386
  function diffMonths(dateA, dateB) {
1378
1387
  const l = date(dateA);
1379
1388
  const r = date(dateB);
@@ -1384,7 +1393,7 @@ function diffMonths(dateA, dateB) {
1384
1393
  let months = (l.getFullYear() - r.getFullYear()) * 12 + (l.getMonth() - r.getMonth());
1385
1394
  const ld = l.getDate();
1386
1395
  const rd = r.getDate();
1387
- if (ld < rd) {
1396
+ if (ld < rd || ld == rd && timeOfDay(l) < timeOfDay(r)) {
1388
1397
  const lm = monthDays(l);
1389
1398
  if (!(lm == ld && lm < rd)) {
1390
1399
  months--;