date.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. 'use strict';
  2. var luxon = require('luxon');
  3. CronDate.prototype.addYear = function() {
  4. this._date = this._date.plus({ years: 1 });
  5. };
  6. CronDate.prototype.addMonth = function() {
  7. this._date = this._date.plus({ months: 1 }).startOf('month');
  8. };
  9. CronDate.prototype.addDay = function() {
  10. this._date = this._date.plus({ days: 1 }).startOf('day');
  11. };
  12. CronDate.prototype.addHour = function() {
  13. var prev = this._date;
  14. this._date = this._date.plus({ hours: 1 }).startOf('hour');
  15. if (this._date <= prev) {
  16. this._date = this._date.plus({ hours: 1 });
  17. }
  18. };
  19. CronDate.prototype.addMinute = function() {
  20. var prev = this._date;
  21. this._date = this._date.plus({ minutes: 1 }).startOf('minute');
  22. if (this._date < prev) {
  23. this._date = this._date.plus({ hours: 1 });
  24. }
  25. };
  26. CronDate.prototype.addSecond = function() {
  27. var prev = this._date;
  28. this._date = this._date.plus({ seconds: 1 }).startOf('second');
  29. if (this._date < prev) {
  30. this._date = this._date.plus({ hours: 1 });
  31. }
  32. };
  33. CronDate.prototype.subtractYear = function() {
  34. this._date = this._date.minus({ years: 1 });
  35. };
  36. CronDate.prototype.subtractMonth = function() {
  37. this._date = this._date
  38. .minus({ months: 1 })
  39. .endOf('month')
  40. .startOf('second');
  41. };
  42. CronDate.prototype.subtractDay = function() {
  43. this._date = this._date
  44. .minus({ days: 1 })
  45. .endOf('day')
  46. .startOf('second');
  47. };
  48. CronDate.prototype.subtractHour = function() {
  49. var prev = this._date;
  50. this._date = this._date
  51. .minus({ hours: 1 })
  52. .endOf('hour')
  53. .startOf('second');
  54. if (this._date >= prev) {
  55. this._date = this._date.minus({ hours: 1 });
  56. }
  57. };
  58. CronDate.prototype.subtractMinute = function() {
  59. var prev = this._date;
  60. this._date = this._date.minus({ minutes: 1 })
  61. .endOf('minute')
  62. .startOf('second');
  63. if (this._date > prev) {
  64. this._date = this._date.minus({ hours: 1 });
  65. }
  66. };
  67. CronDate.prototype.subtractSecond = function() {
  68. var prev = this._date;
  69. this._date = this._date
  70. .minus({ seconds: 1 })
  71. .startOf('second');
  72. if (this._date > prev) {
  73. this._date = this._date.minus({ hours: 1 });
  74. }
  75. };
  76. CronDate.prototype.getDate = function() {
  77. return this._date.day;
  78. };
  79. CronDate.prototype.getFullYear = function() {
  80. return this._date.year;
  81. };
  82. CronDate.prototype.getDay = function() {
  83. var weekday = this._date.weekday;
  84. return weekday == 7 ? 0 : weekday;
  85. };
  86. CronDate.prototype.getMonth = function() {
  87. return this._date.month - 1;
  88. };
  89. CronDate.prototype.getHours = function() {
  90. return this._date.hour;
  91. };
  92. CronDate.prototype.getMinutes = function() {
  93. return this._date.minute;
  94. };
  95. CronDate.prototype.getSeconds = function() {
  96. return this._date.second;
  97. };
  98. CronDate.prototype.getMilliseconds = function() {
  99. return this._date.millisecond;
  100. };
  101. CronDate.prototype.getTime = function() {
  102. return this._date.valueOf();
  103. };
  104. CronDate.prototype.getUTCDate = function() {
  105. return this._getUTC().day;
  106. };
  107. CronDate.prototype.getUTCFullYear = function() {
  108. return this._getUTC().year;
  109. };
  110. CronDate.prototype.getUTCDay = function() {
  111. var weekday = this._getUTC().weekday;
  112. return weekday == 7 ? 0 : weekday;
  113. };
  114. CronDate.prototype.getUTCMonth = function() {
  115. return this._getUTC().month - 1;
  116. };
  117. CronDate.prototype.getUTCHours = function() {
  118. return this._getUTC().hour;
  119. };
  120. CronDate.prototype.getUTCMinutes = function() {
  121. return this._getUTC().minute;
  122. };
  123. CronDate.prototype.getUTCSeconds = function() {
  124. return this._getUTC().second;
  125. };
  126. CronDate.prototype.toISOString = function() {
  127. return this._date.toUTC().toISO();
  128. };
  129. CronDate.prototype.toJSON = function() {
  130. return this._date.toJSON();
  131. };
  132. CronDate.prototype.setDate = function(d) {
  133. this._date = this._date.set({ day: d });
  134. };
  135. CronDate.prototype.setFullYear = function(y) {
  136. this._date = this._date.set({ year: y });
  137. };
  138. CronDate.prototype.setDay = function(d) {
  139. this._date = this._date.set({ weekday: d });
  140. };
  141. CronDate.prototype.setMonth = function(m) {
  142. this._date = this._date.set({ month: m + 1 });
  143. };
  144. CronDate.prototype.setHours = function(h) {
  145. this._date = this._date.set({ hour: h });
  146. };
  147. CronDate.prototype.setMinutes = function(m) {
  148. this._date = this._date.set({ minute: m });
  149. };
  150. CronDate.prototype.setSeconds = function(s) {
  151. this._date = this._date.set({ second: s });
  152. };
  153. CronDate.prototype.setMilliseconds = function(s) {
  154. this._date = this._date.set({ millisecond: s });
  155. };
  156. CronDate.prototype._getUTC = function() {
  157. return this._date.toUTC();
  158. };
  159. CronDate.prototype.toString = function() {
  160. return this.toDate().toString();
  161. };
  162. CronDate.prototype.toDate = function() {
  163. return this._date.toJSDate();
  164. };
  165. CronDate.prototype.isLastDayOfMonth = function() {
  166. //next day
  167. var newDate = this._date.plus({ days: 1 }).startOf('day');
  168. return this._date.month !== newDate.month;
  169. };
  170. /**
  171. * Returns true when the current weekday is the last occurrence of this weekday
  172. * for the present month.
  173. */
  174. CronDate.prototype.isLastWeekdayOfMonth = function() {
  175. // Check this by adding 7 days to the current date and seeing if it's
  176. // a different month
  177. var newDate = this._date.plus({ days: 7 }).startOf('day');
  178. return this._date.month !== newDate.month;
  179. };
  180. function CronDate (timestamp, tz) {
  181. var dateOpts = { zone: tz };
  182. if (!timestamp) {
  183. this._date = luxon.DateTime.local();
  184. } else if (timestamp instanceof CronDate) {
  185. this._date = timestamp._date;
  186. } else if (timestamp instanceof Date) {
  187. this._date = luxon.DateTime.fromJSDate(timestamp, dateOpts);
  188. } else if (typeof timestamp === 'number') {
  189. this._date = luxon.DateTime.fromMillis(timestamp, dateOpts);
  190. } else if (typeof timestamp === 'string') {
  191. this._date = luxon.DateTime.fromISO(timestamp, dateOpts);
  192. this._date.isValid || (this._date = luxon.DateTime.fromRFC2822(timestamp, dateOpts));
  193. this._date.isValid || (this._date = luxon.DateTime.fromSQL(timestamp, dateOpts));
  194. // RFC2822-like format without the required timezone offset (used in tests)
  195. this._date.isValid || (this._date = luxon.DateTime.fromFormat(timestamp, 'EEE, d MMM yyyy HH:mm:ss', dateOpts));
  196. }
  197. if (!this._date || !this._date.isValid) {
  198. throw new Error('CronDate: unhandled timestamp: ' + JSON.stringify(timestamp));
  199. }
  200. if (tz && tz !== this._date.zoneName) {
  201. this._date = this._date.setZone(tz);
  202. }
  203. }
  204. module.exports = CronDate;