timeRange.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. /**
  3. * True during (or between) the specified time(s).
  4. *
  5. * Even though the examples don't show it, this parameter may be present in
  6. * each of the different parameter profiles, always as the last parameter.
  7. *
  8. *
  9. * Examples:
  10. *
  11. * ``` js
  12. * timerange(12)
  13. * true from noon to 1pm.
  14. *
  15. * timerange(12, 13)
  16. * same as above.
  17. *
  18. * timerange(12, "GMT")
  19. * true from noon to 1pm, in GMT timezone.
  20. *
  21. * timerange(9, 17)
  22. * true from 9am to 5pm.
  23. *
  24. * timerange(8, 30, 17, 00)
  25. * true from 8:30am to 5:00pm.
  26. *
  27. * timerange(0, 0, 0, 0, 0, 30)
  28. * true between midnight and 30 seconds past midnight.
  29. * ```
  30. *
  31. * timeRange(hour)
  32. * timeRange(hour1, hour2)
  33. * timeRange(hour1, min1, hour2, min2)
  34. * timeRange(hour1, min1, sec1, hour2, min2, sec2)
  35. * timeRange(hour1, min1, sec1, hour2, min2, sec2, gmt)
  36. *
  37. * @param {String} hour is the hour from 0 to 23. (0 is midnight, 23 is 11 pm.)
  38. * @param {String} min minutes from 0 to 59.
  39. * @param {String} sec seconds from 0 to 59.
  40. * @param {String} gmt either the string "GMT" for GMT timezone, or not specified, for local timezone.
  41. * @return {Boolean}
  42. */
  43. Object.defineProperty(exports, "__esModule", { value: true });
  44. function timeRange() {
  45. // eslint-disable-next-line prefer-rest-params
  46. const args = Array.prototype.slice.call(arguments);
  47. const lastArg = args.pop();
  48. const useGMTzone = lastArg === 'GMT';
  49. const currentDate = new Date();
  50. if (!useGMTzone) {
  51. args.push(lastArg);
  52. }
  53. let result = false;
  54. const noOfArgs = args.length;
  55. const numericArgs = args.map((n) => parseInt(n, 10));
  56. // timeRange(hour)
  57. if (noOfArgs === 1) {
  58. result = getCurrentHour(useGMTzone, currentDate) === numericArgs[0];
  59. // timeRange(hour1, hour2)
  60. }
  61. else if (noOfArgs === 2) {
  62. const currentHour = getCurrentHour(useGMTzone, currentDate);
  63. result = numericArgs[0] <= currentHour && currentHour < numericArgs[1];
  64. // timeRange(hour1, min1, hour2, min2)
  65. }
  66. else if (noOfArgs === 4) {
  67. result = valueInRange(secondsElapsedToday(numericArgs[0], numericArgs[1], 0), secondsElapsedToday(getCurrentHour(useGMTzone, currentDate), getCurrentMinute(useGMTzone, currentDate), 0), secondsElapsedToday(numericArgs[2], numericArgs[3], 59));
  68. // timeRange(hour1, min1, sec1, hour2, min2, sec2)
  69. }
  70. else if (noOfArgs === 6) {
  71. result = valueInRange(secondsElapsedToday(numericArgs[0], numericArgs[1], numericArgs[2]), secondsElapsedToday(getCurrentHour(useGMTzone, currentDate), getCurrentMinute(useGMTzone, currentDate), getCurrentSecond(useGMTzone, currentDate)), secondsElapsedToday(numericArgs[3], numericArgs[4], numericArgs[5]));
  72. }
  73. return result;
  74. }
  75. exports.default = timeRange;
  76. function secondsElapsedToday(hh, mm, ss) {
  77. return hh * 3600 + mm * 60 + ss;
  78. }
  79. function getCurrentHour(gmt, currentDate) {
  80. return gmt ? currentDate.getUTCHours() : currentDate.getHours();
  81. }
  82. function getCurrentMinute(gmt, currentDate) {
  83. return gmt ? currentDate.getUTCMinutes() : currentDate.getMinutes();
  84. }
  85. function getCurrentSecond(gmt, currentDate) {
  86. return gmt ? currentDate.getUTCSeconds() : currentDate.getSeconds();
  87. }
  88. // start <= value <= finish
  89. function valueInRange(start, value, finish) {
  90. return start <= value && value <= finish;
  91. }
  92. //# sourceMappingURL=timeRange.js.map