timeRange.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. var args = Array.prototype.slice.call(arguments), lastArg = args.pop(), useGMTzone = lastArg == 'GMT', currentDate = new Date();
  46. if (!useGMTzone) {
  47. args.push(lastArg);
  48. }
  49. var noOfArgs = args.length, result = false, numericArgs = args.map(function (n) {
  50. return parseInt(n);
  51. });
  52. // timeRange(hour)
  53. if (noOfArgs == 1) {
  54. result = getCurrentHour(useGMTzone, currentDate) == numericArgs[0];
  55. // timeRange(hour1, hour2)
  56. }
  57. else if (noOfArgs == 2) {
  58. var currentHour = getCurrentHour(useGMTzone, currentDate);
  59. result = numericArgs[0] <= currentHour && currentHour < numericArgs[1];
  60. // timeRange(hour1, min1, hour2, min2)
  61. }
  62. else if (noOfArgs == 4) {
  63. result = valueInRange(secondsElapsedToday(numericArgs[0], numericArgs[1], 0), secondsElapsedToday(getCurrentHour(useGMTzone, currentDate), getCurrentMinute(useGMTzone, currentDate), 0), secondsElapsedToday(numericArgs[2], numericArgs[3], 59));
  64. // timeRange(hour1, min1, sec1, hour2, min2, sec2)
  65. }
  66. else if (noOfArgs == 6) {
  67. 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]));
  68. }
  69. return result;
  70. }
  71. exports.default = timeRange;
  72. function secondsElapsedToday(hh, mm, ss) {
  73. return hh * 3600 + mm * 60 + ss;
  74. }
  75. function getCurrentHour(gmt, currentDate) {
  76. return gmt ? currentDate.getUTCHours() : currentDate.getHours();
  77. }
  78. function getCurrentMinute(gmt, currentDate) {
  79. return gmt ? currentDate.getUTCMinutes() : currentDate.getMinutes();
  80. }
  81. function getCurrentSecond(gmt, currentDate) {
  82. return gmt ? currentDate.getUTCSeconds() : currentDate.getSeconds();
  83. }
  84. // start <= value <= finish
  85. function valueInRange(start, value, finish) {
  86. return start <= value && value <= finish;
  87. }
  88. //# sourceMappingURL=timeRange.js.map