Skip to content

Commit

Permalink
Fix roundToNearestMinutes (date-fns#3132)
Browse files Browse the repository at this point in the history
Fixed inconsistent behavior of `roundToNearestMinutes`.

Co-authored-by: Sasha Koss <[email protected]>
  • Loading branch information
fturmel and kossnocorp authored Jan 9, 2024
1 parent d461c19 commit b15fdac
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 94 deletions.
Empty file added scripts/maintain/switchToPR.ts
Empty file.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/differenceInHours/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getRoundingMethod } from "../_lib/getRoundingMethod/index.js";
import { millisecondsInHour } from "../constants/index.js";
import { differenceInMilliseconds } from "../differenceInMilliseconds/index.js";
import type { RoundingOptions } from "../types.js";
import { getRoundingMethod } from "../_lib/roundingMethods/index.js";

/**
* The {@link differenceInHours} function options.
Expand Down
2 changes: 1 addition & 1 deletion src/differenceInMinutes/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getRoundingMethod } from "../_lib/getRoundingMethod/index.js";
import { millisecondsInMinute } from "../constants/index.js";
import { differenceInMilliseconds } from "../differenceInMilliseconds/index.js";
import type { RoundingOptions } from "../types.js";
import { getRoundingMethod } from "../_lib/roundingMethods/index.js";

/**
* The {@link differenceInMinutes} function options.
Expand Down
2 changes: 1 addition & 1 deletion src/differenceInQuarters/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getRoundingMethod } from "../_lib/getRoundingMethod/index.js";
import { differenceInMonths } from "../differenceInMonths/index.js";
import type { RoundingOptions } from "../types.js";
import { getRoundingMethod } from "../_lib/roundingMethods/index.js";

/**
* The {@link differenceInQuarters} function options.
Expand Down
2 changes: 1 addition & 1 deletion src/differenceInSeconds/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getRoundingMethod } from "../_lib/getRoundingMethod/index.js";
import { differenceInMilliseconds } from "../differenceInMilliseconds/index.js";
import type { RoundingOptions } from "../types.js";
import { getRoundingMethod } from "../_lib/roundingMethods/index.js";

/**
* The {@link differenceInSeconds} function options.
Expand Down
2 changes: 1 addition & 1 deletion src/differenceInWeeks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getRoundingMethod } from "../_lib/getRoundingMethod/index.js";
import { differenceInDays } from "../differenceInDays/index.js";
import type { RoundingOptions } from "../types.js";
import { getRoundingMethod } from "../_lib/roundingMethods/index.js";

/**
* The {@link differenceInWeeks} function options.
Expand Down
8 changes: 4 additions & 4 deletions src/formatDistanceStrict/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { defaultLocale } from "../_lib/defaultLocale/index.js";
import { getDefaultOptions } from "../_lib/defaultOptions/index.js";
import { getRoundingMethod } from "../_lib/getRoundingMethod/index.js";
import { getTimezoneOffsetInMilliseconds } from "../_lib/getTimezoneOffsetInMilliseconds/index.js";
import { compareAsc } from "../compareAsc/index.js";
import {
millisecondsInMinute,
Expand All @@ -7,10 +11,6 @@ import {
} from "../constants/index.js";
import { toDate } from "../toDate/index.js";
import type { LocalizedOptions, RoundingOptions } from "../types.js";
import { defaultLocale } from "../_lib/defaultLocale/index.js";
import { getDefaultOptions } from "../_lib/defaultOptions/index.js";
import { getTimezoneOffsetInMilliseconds } from "../_lib/getTimezoneOffsetInMilliseconds/index.js";
import { getRoundingMethod } from "../_lib/roundingMethods/index.js";

/**
* The {@link formatDistanceStrict} function options.
Expand Down
33 changes: 23 additions & 10 deletions src/roundToNearestMinutes/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getRoundingMethod } from "../_lib/getRoundingMethod/index.js";
import { constructFrom } from "../constructFrom/index.js";
import { toDate } from "../toDate/index.js";
import type { NearestMinutesOptions, RoundingOptions } from "../types.js";
import { getRoundingMethod } from "../_lib/roundingMethods/index.js";

/**
* The {@link roundToNearestMinutes} function options.
Expand Down Expand Up @@ -32,10 +32,19 @@ export interface RoundToNearestMinutesOptions
* //=> Thu Jul 10 2014 12:13:00
*
* @example
* // Round 10 July 2014 12:07:30 to nearest quarter hour:
* // Round 10 July 2014 12:12:34 to nearest quarter hour:
* const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { nearestTo: 15 })
* // rounds up because given date is exactly between 12:00:00 and 12:15:00
* //=> Thu Jul 10 2014 12:15:00
*
* @example
* // Floor (rounds down) 10 July 2014 12:12:34 to nearest minute:
* const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'floor' })
* //=> Thu Jul 10 2014 12:12:00
*
* @example
* // Ceil (rounds up) 10 July 2014 12:12:34 to nearest half hour:
* const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'ceil', nearestTo: 30 })
* //=> Thu Jul 10 2014 12:30:00
*/
export function roundToNearestMinutes<DateType extends Date>(
date: DateType | number | string,
Expand All @@ -46,14 +55,18 @@ export function roundToNearestMinutes<DateType extends Date>(
if (nearestTo < 1 || nearestTo > 30) return constructFrom(date, NaN);

const _date = toDate(date);
const seconds = _date.getSeconds(); // relevant if nearestTo is 1, which is the default case
const minutes = _date.getMinutes() + seconds / 60;
const roundingMethod = getRoundingMethod(options?.roundingMethod);
const fractionalSeconds = _date.getSeconds() / 60;
const fractionalMilliseconds = _date.getMilliseconds() / 1000 / 60;
const minutes =
_date.getMinutes() + fractionalSeconds + fractionalMilliseconds;

// Unlike the `differenceIn*` functions, the default rounding behavior is `round` and not 'trunc'
const method = options?.roundingMethod ?? "round";
const roundingMethod = getRoundingMethod(method);

const roundedMinutes = roundingMethod(minutes / nearestTo) * nearestTo;
const remainderMinutes = minutes % nearestTo;
const addedMinutes = Math.round(remainderMinutes / nearestTo) * nearestTo;

const result = constructFrom(_date, _date);
result.setMinutes(roundedMinutes + addedMinutes, 0, 0);
const result = constructFrom(date, _date);
result.setMinutes(roundedMinutes, 0, 0);
return result;
}
Loading

0 comments on commit b15fdac

Please sign in to comment.