Skip to content

Commit 92e7b3e

Browse files
fturmelkossnocorp
authored andcommitted
edge case with milliseconds
1 parent c08f9df commit 92e7b3e

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

src/roundToNearestMinutes/index.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
<<<<<<< HEAD
1+
import { getRoundingMethod } from "../_lib/getRoundingMethod/index.js";
22
import { constructFrom } from "../constructFrom/index.js";
33
import { toDate } from "../toDate/index.js";
44
import type { NearestMinutesOptions, RoundingOptions } from "../types.js";
5-
import { getRoundingMethod } from "../_lib/roundingMethods/index.js";
6-
=======
7-
import constructFrom from '../constructFrom/index'
8-
import toDate from '../toDate/index'
9-
import type { RoundingOptions } from '../types'
10-
import getRoundingMethod from '../_lib/getRoundingMethod/index'
11-
import requiredArgs from '../_lib/requiredArgs/index'
12-
import toInteger from '../_lib/toInteger/index'
13-
>>>>>>> `roundToNearestMinutes` fixes and improvements
145

156
/**
167
* The {@link roundToNearestMinutes} function options.
@@ -63,17 +54,19 @@ export function roundToNearestMinutes<DateType extends Date>(
6354

6455
if (nearestTo < 1 || nearestTo > 30) return constructFrom(date, NaN);
6556

66-
const _date = toDate(date)
67-
const seconds = _date.getSeconds() // relevant if nearestTo is 1, which is the default case
68-
const minutes = _date.getMinutes() + seconds / 60
57+
const _date = toDate(date);
58+
const fractionalSeconds = _date.getSeconds() / 60;
59+
const fractionalMilliseconds = _date.getMilliseconds() / 1000 / 60;
60+
const minutes =
61+
_date.getMinutes() + fractionalSeconds + fractionalMilliseconds;
6962

7063
// Unlike the `differenceIn*` functions, the default rounding behavior is `round` and not 'trunc'
71-
const method = options?.roundingMethod ?? 'round'
72-
const roundingMethod = getRoundingMethod(method)
64+
const method = options?.roundingMethod ?? "round";
65+
const roundingMethod = getRoundingMethod(method);
7366

74-
const roundedMinutes = roundingMethod(minutes / nearestTo) * nearestTo
67+
const roundedMinutes = roundingMethod(minutes / nearestTo) * nearestTo;
7568

76-
const result = constructFrom(date, _date)
77-
result.setMinutes(roundedMinutes, 0, 0)
78-
return result
69+
const result = constructFrom(date, _date);
70+
result.setMinutes(roundedMinutes, 0, 0);
71+
return result;
7972
}

0 commit comments

Comments
 (0)