Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added scripts/maintain/switchToPR.ts
Empty file.
2 changes: 1 addition & 1 deletion scripts/release/buildChangelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Octokit } from "@octokit/core";
import { format } from "../../src/format/index.js";

const git = sg();
const gh = new Octokit({ auth: process.env.GITHUB_TOKEN });
const gh = new Octokit({ auth: process.env.OCTOKIT_TOKEN });

(async () => {
const changelog = await buildChangelog();
Expand Down
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