date.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. 'use strict';
  2. var moment = require('moment-timezone');
  3. CronDate.prototype.addYear = function() {
  4. this._date.add(1, 'year');
  5. };
  6. CronDate.prototype.addMonth = function() {
  7. this._date.add(1, 'month').startOf('month');
  8. };
  9. CronDate.prototype.addDay = function() {
  10. this._date.add(1, 'day').startOf('day');
  11. };
  12. CronDate.prototype.addHour = function() {
  13. var prev = this.getTime();
  14. this._date.add(1, 'hour').startOf('hour');
  15. if (this.getTime() <= prev) {
  16. this._date.add(1, 'hour');
  17. }
  18. };
  19. CronDate.prototype.addMinute = function() {
  20. var prev = this.getTime();
  21. this._date.add(1, 'minute').startOf('minute');
  22. if (this.getTime() < prev) {
  23. this._date.add(1, 'hour');
  24. }
  25. };
  26. CronDate.prototype.addSecond = function() {
  27. var prev = this.getTime();
  28. this._date.add(1, 'second').startOf('second');
  29. if (this.getTime() < prev) {
  30. this._date.add(1, 'hour');
  31. }
  32. };
  33. CronDate.prototype.subtractYear = function() {
  34. this._date.subtract(1, 'year');
  35. };
  36. CronDate.prototype.subtractMonth = function() {
  37. this._date.subtract(1, 'month').endOf('month');
  38. };
  39. CronDate.prototype.subtractDay = function() {
  40. this._date.subtract(1, 'day').endOf('day');
  41. };
  42. CronDate.prototype.subtractHour = function() {
  43. var prev = this.getTime();
  44. this._date.subtract(1, 'hour').endOf('hour');
  45. if (this.getTime() >= prev) {
  46. this._date.subtract(1, 'hour');
  47. }
  48. };
  49. CronDate.prototype.subtractMinute = function() {
  50. var prev = this.getTime();
  51. this._date.subtract(1, 'minute').endOf('minute');
  52. if (this.getTime() > prev) {
  53. this._date.subtract(1, 'hour');
  54. }
  55. };
  56. CronDate.prototype.subtractSecond = function() {
  57. var prev = this.getTime();
  58. this._date.subtract(1, 'second').startOf('second');
  59. if (this.getTime() > prev) {
  60. this._date.subtract(1, 'hour');
  61. }
  62. };
  63. CronDate.prototype.getDate = function() {
  64. return this._date.date();
  65. };
  66. CronDate.prototype.getFullYear = function() {
  67. return this._date.year();
  68. };
  69. CronDate.prototype.getDay = function() {
  70. return this._date.day();
  71. };
  72. CronDate.prototype.getMonth = function() {
  73. return this._date.month();
  74. };
  75. CronDate.prototype.getHours = function() {
  76. return this._date.hours();
  77. };
  78. CronDate.prototype.getMinutes = function() {
  79. return this._date.minute();
  80. };
  81. CronDate.prototype.getSeconds = function() {
  82. return this._date.second();
  83. };
  84. CronDate.prototype.getMilliseconds = function() {
  85. return this._date.millisecond();
  86. };
  87. CronDate.prototype.getTime = function() {
  88. return this._date.valueOf();
  89. };
  90. CronDate.prototype.getUTCDate = function() {
  91. return this._getUTC().date();
  92. };
  93. CronDate.prototype.getUTCFullYear = function() {
  94. return this._getUTC().year();
  95. };
  96. CronDate.prototype.getUTCDay = function() {
  97. return this._getUTC().day();
  98. };
  99. CronDate.prototype.getUTCMonth = function() {
  100. return this._getUTC().month();
  101. };
  102. CronDate.prototype.getUTCHours = function() {
  103. return this._getUTC().hours();
  104. };
  105. CronDate.prototype.getUTCMinutes = function() {
  106. return this._getUTC().minute();
  107. };
  108. CronDate.prototype.getUTCSeconds = function() {
  109. return this._getUTC().second();
  110. };
  111. CronDate.prototype.toISOString = function() {
  112. return this._date.toISOString();
  113. };
  114. CronDate.prototype.toJSON = function() {
  115. return this._date.toJSON();
  116. };
  117. CronDate.prototype.setDate = function(d) {
  118. return this._date.date(d);
  119. };
  120. CronDate.prototype.setFullYear = function(y) {
  121. return this._date.year(y);
  122. };
  123. CronDate.prototype.setDay = function(d) {
  124. return this._date.day(d);
  125. };
  126. CronDate.prototype.setMonth = function(m) {
  127. return this._date.month(m);
  128. };
  129. CronDate.prototype.setHours = function(h) {
  130. return this._date.hour(h);
  131. };
  132. CronDate.prototype.setMinutes = function(m) {
  133. return this._date.minute(m);
  134. };
  135. CronDate.prototype.setSeconds = function(s) {
  136. return this._date.second(s);
  137. };
  138. CronDate.prototype.setMilliseconds = function(s) {
  139. return this._date.millisecond(s);
  140. };
  141. CronDate.prototype.getTime = function() {
  142. return this._date.valueOf();
  143. };
  144. CronDate.prototype._getUTC = function() {
  145. return moment.utc(this._date);
  146. };
  147. CronDate.prototype.toString = function() {
  148. return this._date.toString();
  149. };
  150. CronDate.prototype.toDate = function() {
  151. return this._date.toDate();
  152. };
  153. function CronDate (timestamp, tz) {
  154. if (timestamp instanceof CronDate) {
  155. timestamp = timestamp._date;
  156. }
  157. if (!tz) {
  158. this._date = moment(timestamp);
  159. } else {
  160. this._date = moment.tz(timestamp, tz);
  161. }
  162. }
  163. module.exports = CronDate;