|
1 | | -<<<<<<< HEAD |
| 1 | +import { getRoundingMethod } from "../_lib/getRoundingMethod/index.js"; |
2 | 2 | import { constructFrom } from "../constructFrom/index.js"; |
3 | 3 | import { toDate } from "../toDate/index.js"; |
4 | 4 | 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 |
14 | 5 |
|
15 | 6 | /** |
16 | 7 | * The {@link roundToNearestMinutes} function options. |
@@ -63,17 +54,19 @@ export function roundToNearestMinutes<DateType extends Date>( |
63 | 54 |
|
64 | 55 | if (nearestTo < 1 || nearestTo > 30) return constructFrom(date, NaN); |
65 | 56 |
|
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; |
69 | 62 |
|
70 | 63 | // 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); |
73 | 66 |
|
74 | | - const roundedMinutes = roundingMethod(minutes / nearestTo) * nearestTo |
| 67 | + const roundedMinutes = roundingMethod(minutes / nearestTo) * nearestTo; |
75 | 68 |
|
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; |
79 | 72 | } |
0 commit comments