date-validate-test.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. var vows = require('vows');
  2. var assert = require('assert');
  3. require('../lib/date-utils.js');
  4. vows.describe('Date Validate').addBatch({
  5. 'can deal with hours': {
  6. topic: function () { return Date; },
  7. 'false for less than 0': function (topic) {
  8. assert.equal(topic.validateHour(-1), false);
  9. },
  10. 'false for greater than 23': function (topic) {
  11. assert.equal(topic.validateHour(24), false);
  12. },
  13. 'true for in range': function (topic) {
  14. assert.equal(topic.validateHour(12), true);
  15. }
  16. },
  17. 'can deal with minutes': {
  18. topic: function () { return Date; },
  19. 'false for less than 0': function (topic) {
  20. assert.equal(topic.validateMinute(-1), false);
  21. },
  22. 'false for greater than 59': function (topic) {
  23. assert.equal(topic.validateMinute(60), false);
  24. },
  25. 'true for in range': function (topic) {
  26. assert.equal(topic.validateMinute(30), true);
  27. }
  28. },
  29. 'can deal with seconds': {
  30. topic: function () { return Date; },
  31. 'false for less than 0': function (topic) {
  32. assert.equal(topic.validateSecond(-1), false);
  33. },
  34. 'false for greater than 59': function (topic) {
  35. assert.equal(topic.validateSecond(60), false);
  36. },
  37. 'true for in range': function (topic) {
  38. assert.equal(topic.validateSecond(30), true);
  39. }
  40. },
  41. 'can deal with milliseconds': {
  42. topic: function () { return Date; },
  43. 'false for less than 0': function (topic) {
  44. assert.equal(topic.validateMillisecond(-1), false);
  45. },
  46. 'false for greater than 999': function (topic) {
  47. assert.equal(topic.validateMillisecond(1000), false);
  48. },
  49. 'true for in range': function (topic) {
  50. assert.equal(topic.validateMillisecond(500), true);
  51. }
  52. },
  53. 'can deal with years': {
  54. topic: function () { return Date; },
  55. 'false for less than 0': function (topic) {
  56. assert.equal(topic.validateYear(-1), false);
  57. },
  58. 'false for greater than 9999': function (topic) {
  59. assert.equal(topic.validateYear(10000), false);
  60. },
  61. 'true for in range': function (topic) {
  62. assert.equal(topic.validateYear(5000), true);
  63. }
  64. },
  65. 'can deal with days': {
  66. topic: function () { return Date; },
  67. 'false for less than 0': function (topic) {
  68. assert.equal(topic.validateDay(-1, 2011, 12), false);
  69. },
  70. 'false for greater than 31': function (topic) {
  71. assert.equal(topic.validateDay(32, 2011, 12), false);
  72. },
  73. 'true for in range': function (topic) {
  74. assert.equal(topic.validateDay(10, 2011, 11), true);
  75. }
  76. },
  77. 'static compare works': {
  78. topic: function () { return Date.today(); },
  79. '-1 for yesterday': function (topic) {
  80. assert.equal(Date.compare(Date.yesterday(), topic), -1);
  81. },
  82. '1 for tomorrow': function (topic) {
  83. assert.equal(Date.compare(Date.tomorrow(), topic), 1);
  84. },
  85. '0 for today': function (topic) {
  86. assert.equal(Date.compare(Date.today(), topic), 0);
  87. }
  88. },
  89. 'static equals works': {
  90. topic: function () { return Date.today(); },
  91. 'equal for today': function (topic) {
  92. assert.equal(Date.equals(topic, Date.today()), true);
  93. },
  94. 'false for tomorrow': function (topic) {
  95. assert.equal(Date.equals(topic, Date.tomorrow()), false);
  96. },
  97. 'false for yesterday': function (topic) {
  98. assert.equal(Date.equals(topic, Date.yesterday()), false);
  99. }
  100. },
  101. 'static equalsDay works': {
  102. topic: function () { return Date.today(); },
  103. 'true for today': function (topic) {
  104. assert.equal(Date.equalsDay(topic, Date.today()), true);
  105. },
  106. 'false for yesterday': function (topic) {
  107. assert.equal(Date.equalsDay(topic, Date.yesterday()), false);
  108. },
  109. },
  110. 'getDayNumberFromName works': {
  111. topic: function () { return Date; },
  112. 'sunday works': function (topic) {
  113. assert.equal(topic.getDayNumberFromName('sunday'), 0);
  114. },
  115. 'sun works': function (topic) {
  116. assert.equal(topic.getDayNumberFromName('sun'), 0);
  117. },
  118. 'su works': function (topic) {
  119. assert.equal(topic.getDayNumberFromName('su'), 0);
  120. },
  121. 'monday works': function (topic) {
  122. assert.equal(topic.getDayNumberFromName('monday'), 1);
  123. },
  124. 'mon works': function (topic) {
  125. assert.equal(topic.getDayNumberFromName('mon'), 1);
  126. },
  127. 'mo works': function (topic) {
  128. assert.equal(topic.getDayNumberFromName('mo'), 1);
  129. },
  130. 'tuesday works': function (topic) {
  131. assert.equal(topic.getDayNumberFromName('tuesday'), 2);
  132. },
  133. 'tue works': function (topic) {
  134. assert.equal(topic.getDayNumberFromName('tue'), 2);
  135. },
  136. 'tu works': function (topic) {
  137. assert.equal(topic.getDayNumberFromName('tu'), 2);
  138. },
  139. 'wednesday works': function (topic) {
  140. assert.equal(topic.getDayNumberFromName('wednesday'), 3);
  141. },
  142. 'wed works': function (topic) {
  143. assert.equal(topic.getDayNumberFromName('wed'), 3);
  144. },
  145. 'we works': function (topic) {
  146. assert.equal(topic.getDayNumberFromName('we'), 3);
  147. },
  148. 'thursday works': function (topic) {
  149. assert.equal(topic.getDayNumberFromName('thursday'), 4);
  150. },
  151. 'thu works': function (topic) {
  152. assert.equal(topic.getDayNumberFromName('thu'), 4);
  153. },
  154. 'th works': function (topic) {
  155. assert.equal(topic.getDayNumberFromName('th'), 4);
  156. },
  157. 'friday works': function (topic) {
  158. assert.equal(topic.getDayNumberFromName('friday'), 5);
  159. },
  160. 'fri works': function (topic) {
  161. assert.equal(topic.getDayNumberFromName('fri'), 5);
  162. },
  163. 'fr works': function (topic) {
  164. assert.equal(topic.getDayNumberFromName('fr'), 5);
  165. },
  166. 'saturday works': function (topic) {
  167. assert.equal(topic.getDayNumberFromName('saturday'), 6);
  168. },
  169. 'sat works': function (topic) {
  170. assert.equal(topic.getDayNumberFromName('sat'), 6);
  171. },
  172. 'sa works': function (topic) {
  173. assert.equal(topic.getDayNumberFromName('sa'), 6);
  174. },
  175. 'everything else does not': function (topic) {
  176. assert.equal(topic.getDayNumberFromName('junk'), undefined);
  177. }
  178. },
  179. 'getMonthNumberFromName works': {
  180. topic: function () { return Date; },
  181. 'january works': function (topic) {
  182. assert.equal(topic.getMonthNumberFromName('january'), 0);
  183. },
  184. 'jan works': function (topic) {
  185. assert.equal(topic.getMonthNumberFromName('jan'), 0);
  186. },
  187. 'february works': function (topic) {
  188. assert.equal(topic.getMonthNumberFromName('february'), 1);
  189. },
  190. 'feb works': function (topic) {
  191. assert.equal(topic.getMonthNumberFromName('feb'), 1);
  192. },
  193. 'march works': function (topic) {
  194. assert.equal(topic.getMonthNumberFromName('march'), 2);
  195. },
  196. 'mar works': function (topic) {
  197. assert.equal(topic.getMonthNumberFromName('mar'), 2);
  198. },
  199. 'april works': function (topic) {
  200. assert.equal(topic.getMonthNumberFromName('april'), 3);
  201. },
  202. 'apr works': function (topic) {
  203. assert.equal(topic.getMonthNumberFromName('apr'), 3);
  204. },
  205. 'may works': function (topic) {
  206. assert.equal(topic.getMonthNumberFromName('may'), 4);
  207. },
  208. 'june works': function (topic) {
  209. assert.equal(topic.getMonthNumberFromName('june'), 5);
  210. },
  211. 'jun works': function (topic) {
  212. assert.equal(topic.getMonthNumberFromName('jun'), 5);
  213. },
  214. 'july works': function (topic) {
  215. assert.equal(topic.getMonthNumberFromName('july'), 6);
  216. },
  217. 'jul works': function (topic) {
  218. assert.equal(topic.getMonthNumberFromName('jul'), 6);
  219. },
  220. 'august works': function (topic) {
  221. assert.equal(topic.getMonthNumberFromName('august'), 7);
  222. },
  223. 'aug works': function (topic) {
  224. assert.equal(topic.getMonthNumberFromName('aug'), 7);
  225. },
  226. 'september works': function (topic) {
  227. assert.equal(topic.getMonthNumberFromName('september'), 8);
  228. },
  229. 'sep works': function (topic) {
  230. assert.equal(topic.getMonthNumberFromName('sep'), 8);
  231. },
  232. 'october works': function (topic) {
  233. assert.equal(topic.getMonthNumberFromName('october'), 9);
  234. },
  235. 'oct works': function (topic) {
  236. assert.equal(topic.getMonthNumberFromName('oct'), 9);
  237. },
  238. 'november works': function (topic) {
  239. assert.equal(topic.getMonthNumberFromName('november'), 10);
  240. },
  241. 'nov works': function (topic) {
  242. assert.equal(topic.getMonthNumberFromName('nov'), 10);
  243. },
  244. 'december works': function (topic) {
  245. assert.equal(topic.getMonthNumberFromName('december'), 11);
  246. },
  247. 'dec works': function (topic) {
  248. assert.equal(topic.getMonthNumberFromName('dec'), 11);
  249. }
  250. },
  251. 'getWeekNumber works': {
  252. 'the first week': {
  253. topic: function() { return new Date(2013, 0, 1); },
  254. 'must be 1': function(topic) {
  255. assert.strictEqual(topic.getWeekNumber(), 1);
  256. }
  257. },
  258. 'week 16': {
  259. topic: function() { return new Date(2013, 3, 15); },
  260. 'must be 16': function(topic) {
  261. assert.strictEqual(topic.getWeekNumber(), 16);
  262. }
  263. }
  264. },
  265. 'getFullWeekNumber works': {
  266. 'the first week': {
  267. topic: function() { return new Date(2013, 0, 1); },
  268. 'must be 1': function(topic) {
  269. assert.strictEqual(topic.getFullWeekNumber(), "01");
  270. }
  271. },
  272. 'week 16': {
  273. topic: function() { return new Date(2013, 3, 15); },
  274. 'must be 16': function(topic) {
  275. assert.strictEqual(topic.getFullWeekNumber(), "16");
  276. }
  277. }
  278. },
  279. 'getMonthNameFromNumber works': {
  280. topic: function () { return Date; },
  281. '0 works': function (topic) {
  282. assert.equal(topic.getMonthNameFromNumber(0), 'January');
  283. },
  284. '1 works': function (topic) {
  285. assert.equal(topic.getMonthNameFromNumber(1), 'February');
  286. },
  287. '2 works': function (topic) {
  288. assert.equal(topic.getMonthNameFromNumber(2), 'March');
  289. },
  290. '3 works': function (topic) {
  291. assert.equal(topic.getMonthNameFromNumber(3), 'April');
  292. },
  293. '4 works': function (topic) {
  294. assert.equal(topic.getMonthNameFromNumber(4), 'May');
  295. },
  296. '5 works': function (topic) {
  297. assert.equal(topic.getMonthNameFromNumber(5), 'June');
  298. },
  299. '6 works': function (topic) {
  300. assert.equal(topic.getMonthNameFromNumber(6), 'July');
  301. },
  302. '7 works': function (topic) {
  303. assert.equal(topic.getMonthNameFromNumber(7), 'August');
  304. },
  305. '8 works': function (topic) {
  306. assert.equal(topic.getMonthNameFromNumber(8), 'September');
  307. },
  308. '9 works': function (topic) {
  309. assert.equal(topic.getMonthNameFromNumber(9), 'October');
  310. },
  311. '10 works': function (topic) {
  312. assert.equal(topic.getMonthNameFromNumber(10), 'November');
  313. },
  314. '11 works': function (topic) {
  315. assert.equal(topic.getMonthNameFromNumber(11), 'December');
  316. }
  317. },
  318. 'getMonthAbbrFromNumber works': {
  319. topic: function () { return Date; },
  320. '0 works': function (topic) {
  321. assert.equal(topic.getMonthAbbrFromNumber(0), 'Jan');
  322. },
  323. '1 works': function (topic) {
  324. assert.equal(topic.getMonthAbbrFromNumber(1), 'Feb');
  325. },
  326. '2 works': function (topic) {
  327. assert.equal(topic.getMonthAbbrFromNumber(2), 'Mar');
  328. },
  329. '3 works': function (topic) {
  330. assert.equal(topic.getMonthAbbrFromNumber(3), 'Apr');
  331. },
  332. '4 works': function (topic) {
  333. assert.equal(topic.getMonthAbbrFromNumber(4), 'May');
  334. },
  335. '5 works': function (topic) {
  336. assert.equal(topic.getMonthAbbrFromNumber(5), 'Jun');
  337. },
  338. '6 works': function (topic) {
  339. assert.equal(topic.getMonthAbbrFromNumber(6), 'Jul');
  340. },
  341. '7 works': function (topic) {
  342. assert.equal(topic.getMonthAbbrFromNumber(7), 'Aug');
  343. },
  344. '8 works': function (topic) {
  345. assert.equal(topic.getMonthAbbrFromNumber(8), 'Sep');
  346. },
  347. '9 works': function (topic) {
  348. assert.equal(topic.getMonthAbbrFromNumber(9), 'Oct');
  349. },
  350. '10 works': function (topic) {
  351. assert.equal(topic.getMonthAbbrFromNumber(10), 'Nov');
  352. },
  353. '11 works': function (topic) {
  354. assert.equal(topic.getMonthAbbrFromNumber(11), 'Dec');
  355. }
  356. },
  357. 'getLastMonthName': {
  358. 'when it is January': {
  359. topic: function () {
  360. return new Date("January 15, 2010 12:00:00");
  361. },
  362. 'returns December': function(instance) {
  363. assert.equal(instance.getLastMonthName(), 'December');
  364. }
  365. },
  366. 'when it is Feburary': {
  367. topic: function () {
  368. return new Date("Feburary 15, 2010 12:00:00");
  369. },
  370. 'returns January': function(instance) {
  371. assert.equal(instance.getLastMonthName(), 'January');
  372. }
  373. },
  374. 'when it is March': {
  375. topic: function () {
  376. return new Date("March 15, 2010 12:00:00");
  377. },
  378. 'returns February': function(instance) {
  379. assert.equal(instance.getLastMonthName(), 'February');
  380. }
  381. },
  382. 'when it is April': {
  383. topic: function () {
  384. return new Date("April 15, 2010 12:00:00");
  385. },
  386. 'returns March': function(instance) {
  387. assert.equal(instance.getLastMonthName(), 'March');
  388. }
  389. },
  390. 'when it is May': {
  391. topic: function () {
  392. return new Date("May 15, 2010 12:00:00");
  393. },
  394. 'returns April': function(instance) {
  395. assert.equal(instance.getLastMonthName(), 'April');
  396. }
  397. },
  398. 'when it is June': {
  399. topic: function () {
  400. return new Date("June 15, 2010 12:00:00");
  401. },
  402. 'returns May': function(instance) {
  403. assert.equal(instance.getLastMonthName(), 'May');
  404. }
  405. },
  406. 'when it is July': {
  407. topic: function () {
  408. return new Date("July 15, 2010 12:00:00");
  409. },
  410. 'returns June': function(instance) {
  411. assert.equal(instance.getLastMonthName(), 'June');
  412. }
  413. },
  414. 'when it is August': {
  415. topic: function () {
  416. return new Date("August 15, 2010 12:00:00");
  417. },
  418. 'returns July': function(instance) {
  419. assert.equal(instance.getLastMonthName(), 'July');
  420. }
  421. },
  422. 'when it is September': {
  423. topic: function () {
  424. return new Date("September 15, 2010 12:00:00");
  425. },
  426. 'returns August': function(instance) {
  427. assert.equal(instance.getLastMonthName(), 'August');
  428. }
  429. },
  430. 'when it is October': {
  431. topic: function () {
  432. return new Date("October 15, 2010 12:00:00");
  433. },
  434. 'returns September': function(instance) {
  435. assert.equal(instance.getLastMonthName(), 'September');
  436. }
  437. },
  438. 'when it is November': {
  439. topic: function () {
  440. return new Date("November 15, 2010 12:00:00");
  441. },
  442. 'returns October': function(instance) {
  443. assert.equal(instance.getLastMonthName(), 'October');
  444. }
  445. },
  446. 'when it is December': {
  447. topic: function () {
  448. return new Date("December 15, 2010 12:00:00");
  449. },
  450. 'returns November': function(instance) {
  451. assert.equal(instance.getLastMonthName(), 'November');
  452. }
  453. }
  454. },
  455. 'can add milliseconds': {
  456. 'adding positive milliseconds works': function () {
  457. var topic = Date.today();
  458. topic.addMilliseconds(500);
  459. assert.equal(topic.getMilliseconds(), 500);
  460. },
  461. 'adding negative milliseconds works': function () {
  462. var topic = Date.today();
  463. topic.addMilliseconds(500);
  464. assert.equal(topic.getMilliseconds(), 500);
  465. topic.addMilliseconds(-250);
  466. assert.equal(topic.getMilliseconds(), 250);
  467. }
  468. },
  469. 'can add seconds': {
  470. 'adding positive seconds works': function () {
  471. var topic = Date.today();
  472. topic.addSeconds(50);
  473. assert.equal(topic.getSeconds(), 50);
  474. },
  475. 'adding negative seconds works': function () {
  476. var topic = Date.today();
  477. topic.addSeconds(50);
  478. assert.equal(topic.getSeconds(), 50);
  479. topic.addSeconds(-25);
  480. assert.equal(topic.getSeconds(), 25);
  481. }
  482. },
  483. 'can add minutes': {
  484. 'adding positive minutes works': function () {
  485. var topic = Date.today();
  486. topic.addMinutes(50);
  487. assert.equal(topic.getMinutes(), 50);
  488. },
  489. 'adding negative minutes works': function () {
  490. var topic = Date.today();
  491. topic.addMinutes(50);
  492. assert.equal(topic.getMinutes(), 50);
  493. topic.addMinutes(-25);
  494. assert.equal(topic.getMinutes(), 25);
  495. }
  496. },
  497. 'can add hours': {
  498. 'adding positive hours works': function () {
  499. var topic = Date.today();
  500. topic.addHours(5);
  501. assert.equal(topic.getHours(), 5);
  502. },
  503. 'adding negative hours works': function () {
  504. var topic = Date.today();
  505. topic.addHours(5);
  506. assert.equal(topic.getHours(), 5);
  507. topic.addHours(-2);
  508. assert.equal(topic.getHours(), 3);
  509. }
  510. },
  511. 'can add days': {
  512. 'adding positive days works': function () {
  513. var topic = new Date(2011, 0, 10);
  514. topic.addDays(1);
  515. assert.equal(topic.getDate(), 11);
  516. },
  517. 'adding positive days works across boundaries': function () {
  518. var topic = new Date(2011, 0, 31);
  519. topic.addDays(1);
  520. assert.equal(topic.getDate(), 1);
  521. assert.equal(topic.getMonth(), 1);
  522. },
  523. 'adding negative days works': function () {
  524. var topic = new Date(2011, 0, 10);
  525. topic.addDays(1);
  526. assert.equal(topic.getDate(), 11);
  527. topic.addDays(-2);
  528. assert.equal(topic.getDate(), 9);
  529. }
  530. },
  531. 'can add weeks': {
  532. 'adding positive weeks works': function () {
  533. var topic = new Date(2011, 0, 10);
  534. topic.addWeeks(1);
  535. assert.equal(topic.getDate(), 17);
  536. },
  537. 'adding negative weeks works': function () {
  538. var topic = new Date(2011, 0, 10);
  539. topic.addWeeks(1);
  540. assert.equal(topic.getDate(), 17);
  541. topic.addWeeks(-2);
  542. assert.equal(topic.getDate(), 3);
  543. }
  544. },
  545. 'can add months': {
  546. 'adding positive months works': function () {
  547. var topic = new Date(2011, 1, 10);
  548. topic.addMonths(1);
  549. assert.equal(topic.getMonth(), 2);
  550. },
  551. 'adding negative months works': function () {
  552. var topic = new Date(2011, 1, 10);
  553. topic.addMonths(1);
  554. assert.equal(topic.getMonth(), 2);
  555. topic.addMonths(-2);
  556. assert.equal(topic.getMonth(), 0);
  557. }
  558. },
  559. 'can add years': {
  560. 'adding positive years works': function () {
  561. var topic = new Date(2011, 1, 10);
  562. topic.addYears(1);
  563. assert.equal(topic.getFullYear(), 2012);
  564. },
  565. 'adding negative years works': function () {
  566. var topic = new Date(2011, 1, 10);
  567. topic.addYears(1);
  568. assert.equal(topic.getFullYear(), 2012);
  569. topic.addYears(-2);
  570. assert.equal(topic.getFullYear(), 2010);
  571. }
  572. },
  573. 'cannot remove milliseconds': {
  574. 'removing is not implemented': function () {
  575. var topic = Date.today();
  576. assert.throws(function() { topic.removeMilliseconds(500) }, Error);
  577. }
  578. },
  579. 'can remove seconds': {
  580. 'removing positive seconds works': function () {
  581. var topic = Date.today();
  582. topic.removeSeconds(50);
  583. assert.equal(topic.getSeconds(), 10);
  584. },
  585. 'removing negative seconds works': function () {
  586. var topic = Date.today();
  587. topic.removeSeconds(50);
  588. assert.equal(topic.getSeconds(), 10);
  589. topic.removeSeconds(-25);
  590. assert.equal(topic.getSeconds(), 35);
  591. }
  592. },
  593. 'can remove minutes': {
  594. 'removing positive minutes works': function () {
  595. var topic = Date.today();
  596. topic.removeMinutes(50);
  597. assert.equal(topic.getMinutes(), 10);
  598. },
  599. 'removing negative minutes works': function () {
  600. var topic = Date.today();
  601. topic.removeMinutes(50);
  602. assert.equal(topic.getMinutes(), 10);
  603. topic.removeMinutes(-25);
  604. assert.equal(topic.getMinutes(), 35);
  605. }
  606. },
  607. 'can remove hours': {
  608. 'removing positive hours works': function () {
  609. var topic = Date.today();
  610. topic.removeHours(5);
  611. assert.equal(topic.getHours(), 19);
  612. },
  613. 'removing negative hours works': function () {
  614. var topic = Date.today();
  615. topic.removeHours(5);
  616. assert.equal(topic.getHours(), 19);
  617. topic.removeHours(-2);
  618. assert.equal(topic.getHours(), 21);
  619. }
  620. },
  621. 'can remove days': {
  622. 'removing positive days works': function () {
  623. var topic = new Date(2011, 0, 10);
  624. topic.removeDays(1);
  625. assert.equal(topic.getDate(), 09);
  626. },
  627. 'removing positive days works across boundaries': function () {
  628. var topic = new Date(2011, 0, 01);
  629. topic.removeDays(1);
  630. assert.equal(topic.getDate(), 31);
  631. assert.equal(topic.getMonth(), 11);
  632. },
  633. 'removing negative days works': function () {
  634. var topic = new Date(2011, 0, 10);
  635. topic.removeDays(1);
  636. assert.equal(topic.getDate(), 09);
  637. topic.removeDays(-2);
  638. assert.equal(topic.getDate(), 11);
  639. }
  640. },
  641. 'can remove weeks': {
  642. 'removing positive weeks works': function () {
  643. var topic = new Date(2011, 0, 10);
  644. topic.removeWeeks(1);
  645. assert.equal(topic.getDate(), 03);
  646. },
  647. 'removing negative weeks works': function () {
  648. var topic = new Date(2011, 0, 10);
  649. topic.removeWeeks(1);
  650. assert.equal(topic.getDate(), 03);
  651. topic.removeWeeks(-2);
  652. assert.equal(topic.getDate(), 17);
  653. }
  654. },
  655. 'can remove months': {
  656. 'removing positive months works': function () {
  657. var topic = new Date(2011, 1, 10);
  658. topic.removeMonths(1);
  659. assert.equal(topic.getMonth(), 0);
  660. },
  661. 'removing negative months works': function () {
  662. var topic = new Date(2011, 1, 10);
  663. topic.removeMonths(1);
  664. assert.equal(topic.getMonth(), 0);
  665. topic.removeMonths(-2);
  666. assert.equal(topic.getMonth(), 2);
  667. }
  668. },
  669. 'can remove years': {
  670. 'removing positive years works': function () {
  671. var topic = new Date(2011, 1, 10);
  672. topic.removeYears(1);
  673. assert.equal(topic.getFullYear(), 2010);
  674. },
  675. 'removing negative years works': function () {
  676. var topic = new Date(2011, 1, 10);
  677. topic.removeYears(1);
  678. assert.equal(topic.getFullYear(), 2010);
  679. topic.removeYears(-2);
  680. assert.equal(topic.getFullYear(), 2012);
  681. }
  682. },
  683. 'can add weekdays within a week': {
  684. 'adding positive weekdays': function () {
  685. var topic = new Date(2013, 1, 13); //Wed
  686. topic.addWeekdays(2);
  687. assert.equal(topic.getDay(), 5);
  688. },
  689. 'adding negative weekdays': function () {
  690. var topic = new Date(2013, 1, 13); //Wed
  691. topic.addWeekdays(-2);
  692. assert.equal(topic.getDay(), 1);
  693. }
  694. },
  695. 'can add weekdays across one week': {
  696. 'adding positive weekdays': function () {
  697. var wed = new Date(2013, 1, 13);
  698. assert.equal(wed.addWeekdays(3).getDate(), 18);
  699. var fri = new Date(2013, 1, 15);
  700. assert.equal(fri.addWeekdays(1).getDate(), 18);
  701. },
  702. 'adding negative weekdays': function () {
  703. var wed = new Date(2013, 1, 13);
  704. assert.equal(wed.addWeekdays(-3).getDate(), 8);
  705. var mon = new Date(2013, 1, 11);
  706. assert.equal(mon.addWeekdays(-1).getDate(), 8);
  707. }
  708. },
  709. 'can add weekdays across multiple weeks': {
  710. 'adding positive weekdays': function () {
  711. var tue = new Date(2013, 3, 16);
  712. assert.equal(tue.clone().addWeekdays(14).getDate(), 6);
  713. assert.equal(tue.clone().addWeekdays(14).getMonth(), 4);
  714. var fri = new Date(2013, 3, 19);
  715. assert.equal(fri.clone().addWeekdays(14).getDate(), 9);
  716. assert.equal(fri.clone().addWeekdays(14).getMonth(), 4);
  717. },
  718. 'adding negative weekdays': function () {
  719. var tue = new Date(2013, 3, 16); //Wed
  720. assert.equal(tue.clone().addWeekdays(-17).getDate(), 22);
  721. assert.equal(tue.clone().addWeekdays(-17).getMonth(), 2);
  722. var mon = new Date(2013, 3, 15); //Wed
  723. assert.equal(mon.clone().addWeekdays(-17).getDate(), 21);
  724. assert.equal(mon.clone().addWeekdays(-17).getMonth(), 2);
  725. }
  726. },
  727. 'can add weekdays to a Saturday': {
  728. 'adding positive weekdays': function () {
  729. var sat = new Date(2013, 1, 16);
  730. assert.equal(sat.clone().addWeekdays(1).getDate(), 18);
  731. assert.equal(sat.clone().addWeekdays(11).getDate(), 4);
  732. assert.equal(sat.clone().addWeekdays(31).getDate(), 1);
  733. },
  734. 'adding negative weekdays': function () {
  735. var sat = new Date(2013, 1, 16);
  736. assert.equal(sat.clone().addWeekdays(-1).getDate(), 15);
  737. assert.equal(sat.clone().addWeekdays(-15).getDate(), 28);
  738. assert.equal(sat.clone().addWeekdays(-33).getDate(), 2);
  739. }
  740. },
  741. 'can add weekdays to a Sunday': {
  742. 'adding positive weekdays': function () {
  743. var sun = new Date(2013, 1, 17);
  744. assert.equal(sun.clone().addWeekdays(1).getDate(), 18);
  745. assert.equal(sun.clone().addWeekdays(11).getDate(), 4);
  746. assert.equal(sun.clone().addWeekdays(31).getDate(), 1);
  747. },
  748. 'adding negative weekdays': function () {
  749. var sun = new Date(2013, 1, 17);
  750. assert.equal(sun.clone().addWeekdays(-1).getDate(), 15);
  751. assert.equal(sun.clone().addWeekdays(-15).getDate(), 28);
  752. assert.equal(sun.clone().addWeekdays(-33).getDate(), 2);
  753. }
  754. },
  755. 'can set time to now': {
  756. 'setting time to now works': function () {
  757. var topic = Date.today();
  758. topic.setTimeToNow();
  759. var now = new Date();
  760. // hokey, but should be sufficient
  761. assert.equal((now.valueOf() - topic.valueOf() < 100), true);
  762. }
  763. },
  764. 'can clone time': {
  765. topic: function () { return new Date(); },
  766. 'clone works': function (topic) {
  767. var clone = topic.clone();
  768. assert.equal(clone.valueOf(), topic.valueOf());
  769. }
  770. },
  771. 'between works': {
  772. 'between returns true for valid start and end': function () {
  773. var today = Date.today();
  774. var yesterday = Date.yesterday();
  775. var tomorrow = Date.tomorrow();
  776. assert.equal(today.between(yesterday, tomorrow), true);
  777. },
  778. 'between returns false for invalid start and end': function () {
  779. var today = Date.today();
  780. var yesterday = Date.yesterday();
  781. var tomorrow = Date.tomorrow();
  782. assert.equal(today.between(tomorrow, yesterday), false);
  783. }
  784. },
  785. 'compareTo works': {
  786. topic: function () { return Date.today(); },
  787. '-1 for tomorrow': function (topic) {
  788. assert.equal(topic.compareTo(Date.tomorrow()), -1);
  789. },
  790. '1 for yesterday': function (topic) {
  791. assert.equal(topic.compareTo(Date.yesterday()), 1);
  792. },
  793. '0 for today': function (topic) {
  794. assert.equal(topic.compareTo(Date.today()), 0);
  795. }
  796. },
  797. 'isToday works': {
  798. topic: function () { return Date.today(); },
  799. 'true for today': function (topic) {
  800. assert.equal(topic.isToday(), true);
  801. },
  802. 'false if not today': function (topic) {
  803. assert.equal(Date.yesterday().isToday(), false);
  804. }
  805. },
  806. 'equals instance works': {
  807. topic: function () { return Date.today(); },
  808. 'true for equal': function (topic) {
  809. assert.equal(topic.equals(Date.today()), true);
  810. },
  811. 'false for not equal': function (topic) {
  812. assert.equal(topic.equals(Date.tomorrow()), false);
  813. }
  814. },
  815. 'equalsDay instance works': {
  816. topic: function () { return Date.today(); },
  817. 'true for today': function (topic) {
  818. assert.equal(topic.equalsDay(Date.today()), true);
  819. },
  820. 'false for yesterday': function (topic) {
  821. assert.equal(topic.equalsDay(Date.yesterday()), false);
  822. }
  823. },
  824. 'isBefore works': {
  825. topic: function () { return Date.today(); },
  826. 'true for before': function (topic) {
  827. assert.equal(topic.isBefore(Date.tomorrow()), true);
  828. },
  829. 'false for after': function (topic) {
  830. assert.equal(topic.isBefore(Date.yesterday()), false);
  831. }
  832. },
  833. 'isAfter works': {
  834. topic: function () { return Date.today(); },
  835. 'false for before': function (topic) {
  836. assert.equal(topic.isAfter(Date.tomorrow()), false);
  837. },
  838. 'true for after': function (topic) {
  839. assert.equal(topic.isAfter(Date.yesterday()), true);
  840. }
  841. },
  842. 'isWeekend works': {
  843. 'false for weekdays': function (topic) {
  844. assert.equal(new Date(2013,2,11).isWeekend(), false);
  845. assert.equal(new Date(2013,2,12).isWeekend(), false);
  846. assert.equal(new Date(2013,2,13).isWeekend(), false);
  847. assert.equal(new Date(2013,2,14).isWeekend(), false);
  848. assert.equal(new Date(2013,2,15).isWeekend(), false);
  849. },
  850. 'true for weekend': function (topic) {
  851. assert.equal(new Date(2013,2,16).isWeekend(), true);
  852. assert.equal(new Date(2013,2,17).isWeekend(), true);
  853. }
  854. },
  855. 'getDaysBetween works': {
  856. topic: function () { return Date.today(); },
  857. '1 for tomorrow': function (topic) {
  858. assert.equal(topic.getDaysBetween(Date.tomorrow()), 1);
  859. },
  860. '-1 for yesterday': function (topic) {
  861. assert.equal(topic.getDaysBetween(Date.yesterday()), -1);
  862. },
  863. '0 for today': function (topic) {
  864. assert.equal(topic.getDaysBetween(Date.today()), 0);
  865. }
  866. },
  867. 'getDaysBetween works for beginning of year': {
  868. topic: function () { return new Date('Jan 1, 2011 01:01:01 GMT'); },
  869. 'should return 0 for the same day': function (topic) {
  870. var date = new Date('Jan 1, 2011 01:01:01 GMT');
  871. assert.equal(topic.getDaysBetween(date), 0);
  872. },
  873. 'should return 1 for tomorrow': function (topic) {
  874. var date = new Date('Jan 2, 2011 01:01:01 GMT');
  875. assert.equal(topic.getDaysBetween(date), 1);
  876. }
  877. },
  878. 'getMinutesBetween works': {
  879. topic: function () { return new Date('Jan 1, 2011 23:31:01 GMT'); },
  880. '10 for 10 minutes': function (topic) {
  881. assert.equal(topic.getMinutesBetween(new Date('Jan 1, 2011 23:41:01 GMT')), 10);
  882. },
  883. '-10 for 10 minutes ago': function (topic) {
  884. assert.equal(topic.getMinutesBetween(new Date('Jan 1, 2011 23:21:01 GMT')), -10);
  885. },
  886. '0 for same minute': function (topic) {
  887. assert.equal(topic.getMinutesBetween(new Date('Jan 1, 2011 23:31:01 GMT')), 0);
  888. },
  889. 'for time difference that spans days': function (topic) {
  890. assert.equal(topic.getMinutesBetween(new Date('Jan 2, 2011 00:01:01 GMT')), 30);
  891. }
  892. },
  893. 'getSecondsBetween works': {
  894. topic: function () { return new Date('Jan 1, 2011 23:31:01 GMT'); },
  895. '10 for 10 seconds': function (topic) {
  896. assert.equal(topic.getSecondsBetween(new Date('Jan 1, 2011 23:31:11 GMT')), 10);
  897. },
  898. '-10 for 10 seconds ago': function (topic) {
  899. assert.equal(topic.getSecondsBetween(new Date('Jan 1, 2011 23:30:51 GMT')), -10);
  900. },
  901. '0 for same second': function (topic) {
  902. assert.equal(topic.getSecondsBetween(new Date('Jan 1, 2011 23:31:01 GMT')), 0);
  903. }
  904. },
  905. 'getMillisecondsBetween works': {
  906. topic: function () { return new Date(); },
  907. '10 for 10 milliseconds': function (topic) {
  908. assert.equal(topic.getMillisecondsBetween(new Date(+topic + 10)), 10);
  909. },
  910. '-10 for 10 milliseconds ago': function (topic) {
  911. assert.equal(topic.getMillisecondsBetween(new Date(+topic - 10)), -10);
  912. },
  913. '0 for same millisecond': function (topic) {
  914. assert.equal(topic.getMillisecondsBetween(new Date(+topic)), 0);
  915. }
  916. },
  917. 'getHoursBetween works': {
  918. topic: function () { return new Date('Jan 1, 2011 23:31:01 GMT'); },
  919. '1 for 1 hour': function (topic) {
  920. assert.equal(topic.getHoursBetween(new Date('Jan 2, 2011 00:31:01 GMT')), 1);
  921. },
  922. '-1 for 1 hour ago': function (topic) {
  923. assert.equal(topic.getHoursBetween(new Date('Jan 1, 2011 22:31:01 GMT')), -1);
  924. },
  925. '0 for same hour': function (topic) {
  926. assert.equal(topic.getHoursBetween(new Date('Jan 1, 2011 23:31:01 GMT')), 0);
  927. }
  928. },
  929. 'getOrdinalNumber works': {
  930. 'returns correct day': function () {
  931. var date = new Date('02-01-2011');
  932. assert.equal(date.getOrdinalNumber(), 32);
  933. }
  934. },
  935. 'getOrdinalNumber works for january 1st': {
  936. 'returns correct day': function () {
  937. var date = new Date('01-01-2011');
  938. assert.equal(date.getOrdinalNumber(), 1);
  939. }
  940. },
  941. 'getDaysInMonth works': {
  942. 'january': function (topic) {
  943. assert.equal(Date.getDaysInMonth(2011, 0), 31);
  944. },
  945. 'february': function (topic) {
  946. assert.equal(Date.getDaysInMonth(2011, 1), 28);
  947. },
  948. 'february leap year': function (topic) {
  949. assert.equal(Date.getDaysInMonth(2008, 1), 29);
  950. },
  951. 'march': function (topic) {
  952. assert.equal(Date.getDaysInMonth(2011, 2), 31);
  953. },
  954. 'april': function (topic) {
  955. assert.equal(Date.getDaysInMonth(2011, 3), 30);
  956. },
  957. 'may': function (topic) {
  958. assert.equal(Date.getDaysInMonth(2011, 4), 31);
  959. },
  960. 'june': function (topic) {
  961. assert.equal(Date.getDaysInMonth(2011, 5), 30);
  962. },
  963. 'july': function (topic) {
  964. assert.equal(Date.getDaysInMonth(2011, 6), 31);
  965. },
  966. 'august': function (topic) {
  967. assert.equal(Date.getDaysInMonth(2011, 7), 31);
  968. },
  969. 'september': function (topic) {
  970. assert.equal(Date.getDaysInMonth(2011, 8), 30);
  971. },
  972. 'october': function (topic) {
  973. assert.equal(Date.getDaysInMonth(2011, 9), 31);
  974. },
  975. 'november': function (topic) {
  976. assert.equal(Date.getDaysInMonth(2011, 10), 30);
  977. },
  978. 'december': function (topic) {
  979. assert.equal(Date.getDaysInMonth(2011, 11), 31);
  980. }
  981. },
  982. 'getMonthsBetween works': {
  983. topic: function() { return new Date(Date.UTC(2013, 1, 28)); },
  984. 'different months': function(topic) {
  985. var eDate = new Date(Date.UTC(2013, 2, 30));
  986. assert.equal(topic.getMonthsBetween(eDate).toFixed(5), 1.06586);
  987. },
  988. 'different months and years': function(topic) {
  989. var eDate = new Date(Date.UTC(2014, 3, 4));
  990. assert.equal(topic.getMonthsBetween(eDate).toFixed(5), 13.22715);
  991. },
  992. 'same month': function( topic ) {
  993. var sDate = new Date(Date.UTC(2013, 1, 1));
  994. assert.equal(sDate.getMonthsBetween(topic).toFixed(5), 0.87097);
  995. },
  996. 'same date': function(topic) {
  997. var sameDate = new Date(topic.getTime());
  998. assert.equal(topic.getMonthsBetween(sameDate).toFixed(5), 0);
  999. },
  1000. 'same day and month but different years': function(topic) {
  1001. var differentYear = new Date(Date.UTC(2014, 1, 28));
  1002. assert.equal(topic.getMonthsBetween(differentYear).toFixed(5), 12);
  1003. }
  1004. }
  1005. }).export(module);