Blame view

天文台pc/tianwentai-ui/node_modules/date-fns/roundToNearestHours.js 2.8 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  "use strict";
  exports.roundToNearestHours = roundToNearestHours;
  var _index = require("./_lib/getRoundingMethod.js");
  var _index2 = require("./constructFrom.js");
  var _index3 = require("./toDate.js");
  
  /**
   * The {@link roundToNearestHours} function options.
   */
  
  /**
   * @name roundToNearestHours
   * @category Hour Helpers
   * @summary Rounds the given date to the nearest hour
   *
   * @description
   * Rounds the given date to the nearest hour (or number of hours).
   * Rounds up when the given date is exactly between the nearest round hours.
   *
   * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
   *
   * @param date - The date to round
   * @param options - An object with options.
   *
   * @returns The new date rounded to the closest hour
   *
   * @example
   * // Round 10 July 2014 12:34:56 to nearest hour:
   * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56))
   * //=> Thu Jul 10 2014 13:00:00
   *
   * @example
   * // Round 10 July 2014 12:34:56 to nearest half hour:
   * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { nearestTo: 6 })
   * //=> Thu Jul 10 2014 12:00:00
  
   * @example
   * // Round 10 July 2014 12:34:56 to nearest half hour:
   * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { nearestTo: 8 })
   * //=> Thu Jul 10 2014 16:00:00
  
  * @example
   * // Floor (rounds down) 10 July 2014 12:34:56 to nearest hour:
   * const result = roundToNearestHours(new Date(2014, 6, 10, 1, 23, 45), { roundingMethod: 'ceil' })
   * //=> Thu Jul 10 2014 02:00:00
   *
   * @example
   * // Ceil (rounds up) 10 July 2014 12:34:56 to nearest quarter hour:
   * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { roundingMethod: 'floor', nearestTo: 8 })
   * //=> Thu Jul 10 2014 08:00:00
   */
  function roundToNearestHours(date, options) {
    const nearestTo = options?.nearestTo ?? 1;
  
    if (nearestTo < 1 || nearestTo > 12)
      return (0, _index2.constructFrom)(date, NaN);
  
    const _date = (0, _index3.toDate)(date);
    const fractionalMinutes = _date.getMinutes() / 60;
    const fractionalSeconds = _date.getSeconds() / 60 / 60;
    const fractionalMilliseconds = _date.getMilliseconds() / 1000 / 60 / 60;
    const hours =
      _date.getHours() +
      fractionalMinutes +
      fractionalSeconds +
      fractionalMilliseconds;
  
    // Unlike the `differenceIn*` functions, the default rounding behavior is `round` and not 'trunc'
    const method = options?.roundingMethod ?? "round";
    const roundingMethod = (0, _index.getRoundingMethod)(method);
  
    // nearestTo option does not care daylight savings time
    const roundedHours = roundingMethod(hours / nearestTo) * nearestTo;
  
    const result = (0, _index2.constructFrom)(date, _date);
    result.setHours(roundedHours, 0, 0, 0);
    return result;
  }