You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timeRange.js 3.2KB

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