date-format-test.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. var vows = require('vows');
  2. var assert = require('assert');
  3. require('../lib/date-utils.js');
  4. function pad(str, length) {
  5. str = String(str);
  6. while (str.length < length) {
  7. str = '0' + str;
  8. }
  9. return str;
  10. }
  11. vows.describe('Date Format').addBatch({
  12. 'can return month abbreviations as static method': {
  13. topic: function () { return new Date(123456789); },
  14. 'returns the correct abbreviated month': function (date) {
  15. assert.equal(date.getMonthAbbr(), 'Jan');
  16. }
  17. },
  18. 'can return month as static method': {
  19. topic: function () { return new Date(123456789); },
  20. 'returns the correct month': function (date) {
  21. assert.equal(date.getMonthAbbr(), 'Jan');
  22. }
  23. },
  24. 'can return common log formatted string': {
  25. topic: function () { return new Date(Date.UTC(2011, 0, 1, 1, 1, 1, 0)); },
  26. 'returns the correct clf string': function (date) {
  27. var tz = pad(Math.abs(date.getTimezoneOffset() / 0.6), 4);
  28. if (date.getTimezoneOffset() > 0) {
  29. tz = '-' + tz;
  30. }
  31. date = new Date(date.valueOf() + date.getTimezoneOffset() * 60000);
  32. assert.equal(date.toCLFString(), '01/Jan/2011:01:01:01 ' + tz);
  33. }
  34. },
  35. 'can return correctly formatted toFormat': {
  36. topic: function () { var topic = new Date(2011, 2, 1);
  37. topic.addHours(13)
  38. .addMinutes(11)
  39. .addSeconds(41)
  40. .addMilliseconds(23);
  41. return topic;
  42. },
  43. 'returns correctly': function (date) {
  44. assert.equal(date.toFormat('YYYY'), '2011');
  45. assert.equal(date.toFormat('YY'), '11');
  46. assert.equal(date.toFormat('M'), '3');
  47. assert.equal(date.toFormat('MM'), '03');
  48. assert.equal(date.toFormat('MMM'), 'Mar');
  49. assert.equal(date.toFormat('MMMM'), 'March');
  50. assert.equal(date.toFormat('D'), '1');
  51. assert.equal(date.toFormat('DD'), '01');
  52. assert.equal(date.toFormat('DDD'), 'Tue');
  53. assert.equal(date.toFormat('DDDD'), 'Tuesday');
  54. assert.equal(date.toFormat('H'), '1');
  55. assert.equal(date.toFormat('HH'), '01');
  56. assert.equal(date.toFormat('HH24'), '13');
  57. assert.equal(date.toFormat('MI'), '11');
  58. assert.equal(date.toFormat('SS'), '41');
  59. assert.equal(date.toFormat('LL'), '023');
  60. },
  61. 'returns complex formats correct': function (date) {
  62. assert.equal(date.toFormat('DDD-MMM-YYYY'), 'Tue-Mar-2011');
  63. assert.equal(date.toFormat('DD/MM/YY'), '01/03/11');
  64. assert.equal(date.toFormat('D:M:YY'), '1:3:11');
  65. }
  66. },
  67. 'can return correctly formatted toUTCFormat': {
  68. topic: function () {
  69. return topic = new Date(Date.UTC(2011, 2, 1, 13, 11, 41, 23));
  70. },
  71. 'returns correctly': function (date) {
  72. assert.equal(date.toUTCFormat('YYYY'), '2011');
  73. assert.equal(date.toUTCFormat('YY'), '11');
  74. assert.equal(date.toUTCFormat('M'), '3');
  75. assert.equal(date.toUTCFormat('MM'), '03');
  76. assert.equal(date.toUTCFormat('MMM'), 'Mar');
  77. assert.equal(date.toUTCFormat('MMMM'), 'March');
  78. assert.equal(date.toUTCFormat('D'), '1');
  79. assert.equal(date.toUTCFormat('DD'), '01');
  80. assert.equal(date.toUTCFormat('DDD'), 'Tue');
  81. assert.equal(date.toUTCFormat('DDDD'), 'Tuesday');
  82. assert.equal(date.toUTCFormat('H'), '1');
  83. assert.equal(date.toUTCFormat('HH'), '01');
  84. assert.equal(date.toUTCFormat('HH24'), '13');
  85. assert.equal(date.toUTCFormat('MI'), '11');
  86. assert.equal(date.toUTCFormat('SS'), '41');
  87. assert.equal(date.toUTCFormat('LL'), '023');
  88. },
  89. 'returns complex formats correct': function (date) {
  90. assert.equal(date.toUTCFormat('DDD-MMM-YYYY'), 'Tue-Mar-2011');
  91. assert.equal(date.toUTCFormat('DD/MM/YY'), '01/03/11');
  92. assert.equal(date.toUTCFormat('D:M:YY'), '1:3:11');
  93. }
  94. },
  95. 'can return correct months': {
  96. topic: function () { return new Date(2011, 0, 1); },
  97. 'returns Jan correcty': function (date) { assert.equal(date.addMonths(0).toFormat('MMM'), 'Jan'); },
  98. 'returns Feb correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Feb'); },
  99. 'returns Mar correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Mar'); },
  100. 'returns Apr correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Apr'); },
  101. 'returns May correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'May'); },
  102. 'returns Jun correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Jun'); },
  103. 'returns Jul correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Jul'); },
  104. 'returns Aug correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Aug'); },
  105. 'returns Sep correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Sep'); },
  106. 'returns Oct correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Oct'); },
  107. 'returns Nov correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Nov'); },
  108. 'returns Dec correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Dec'); },
  109. },
  110. 'can return database formatted string': {
  111. topic: function () { return new Date(Date.UTC(2011, 0, 1, 1, 1, 1, 0)); },
  112. 'returns the correct database string': function (date) {
  113. assert.equal(date.toDBString(), '2011-01-01 01:01:01');
  114. }
  115. },
  116. 'when passing an argument to toYMD': {
  117. topic: function () { return new Date(2011, 0, 1, 1, 1, 1, 0).toYMD('-'); },
  118. 'the correct string is returned': function (topic) {
  119. assert.equal(topic, '2011-01-01');
  120. }
  121. },
  122. 'when passing an empty argument to toYMD': {
  123. topic: function () { return new Date(2011, 0, 1, 1, 1, 1, 0).toYMD(''); },
  124. 'the correct string is returned': function (topic) {
  125. assert.equal(topic, '20110101');
  126. }
  127. },
  128. 'when passing no argument to toYMD': {
  129. topic: function () { return new Date(2011, 0, 1, 1, 1, 1, 0).toYMD(); },
  130. 'the correct default is chosen and the string is returned': function (topic) {
  131. assert.equal(topic, '2011-01-01');
  132. }
  133. }
  134. }).export(module);