core.test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* global QUnit */
  2. 'use strict';
  3. QUnit.module('admin.core');
  4. QUnit.test('Date.getTwelveHours', function(assert) {
  5. assert.equal(new Date(2011, 0, 1, 0, 0).getTwelveHours(), 12, '0:00');
  6. assert.equal(new Date(2011, 0, 1, 11, 0).getTwelveHours(), 11, '11:00');
  7. assert.equal(new Date(2011, 0, 1, 16, 0).getTwelveHours(), 4, '16:00');
  8. });
  9. QUnit.test('Date.getTwoDigitMonth', function(assert) {
  10. assert.equal(new Date(2011, 0, 1).getTwoDigitMonth(), '01', 'jan 1');
  11. assert.equal(new Date(2011, 9, 1).getTwoDigitMonth(), '10', 'oct 1');
  12. });
  13. QUnit.test('Date.getTwoDigitDate', function(assert) {
  14. assert.equal(new Date(2011, 0, 1).getTwoDigitDate(), '01', 'jan 1');
  15. assert.equal(new Date(2011, 0, 15).getTwoDigitDate(), '15', 'jan 15');
  16. });
  17. QUnit.test('Date.getTwoDigitTwelveHour', function(assert) {
  18. assert.equal(new Date(2011, 0, 1, 0, 0).getTwoDigitTwelveHour(), '12', '0:00');
  19. assert.equal(new Date(2011, 0, 1, 4, 0).getTwoDigitTwelveHour(), '04', '4:00');
  20. assert.equal(new Date(2011, 0, 1, 22, 0).getTwoDigitTwelveHour(), '10', '22:00');
  21. });
  22. QUnit.test('Date.getTwoDigitHour', function(assert) {
  23. assert.equal(new Date(2014, 6, 1, 9, 0).getTwoDigitHour(), '09', '9:00 am is 09');
  24. assert.equal(new Date(2014, 6, 1, 11, 0).getTwoDigitHour(), '11', '11:00 am is 11');
  25. });
  26. QUnit.test('Date.getTwoDigitMinute', function(assert) {
  27. assert.equal(new Date(2014, 6, 1, 0, 5).getTwoDigitMinute(), '05', '12:05 am is 05');
  28. assert.equal(new Date(2014, 6, 1, 0, 15).getTwoDigitMinute(), '15', '12:15 am is 15');
  29. });
  30. QUnit.test('Date.getTwoDigitSecond', function(assert) {
  31. assert.equal(new Date(2014, 6, 1, 0, 0, 2).getTwoDigitSecond(), '02', '12:00:02 am is 02');
  32. assert.equal(new Date(2014, 6, 1, 0, 0, 20).getTwoDigitSecond(), '20', '12:00:20 am is 20');
  33. });
  34. QUnit.test('Date.getAbbrevMonthName', function(assert) {
  35. assert.equal(new Date(2020, 0, 26).getAbbrevMonthName(), 'Jan', 'jan 26');
  36. assert.equal(new Date(2020, 9, 26).getAbbrevMonthName(), 'Oct', 'oct 26');
  37. });
  38. QUnit.test('Date.getFullMonthName', function(assert) {
  39. assert.equal(new Date(2020, 0, 26).getFullMonthName(), 'January', 'jan 26');
  40. assert.equal(new Date(2020, 9, 26).getFullMonthName(), 'October', 'oct 26');
  41. });
  42. QUnit.test('Date.getAbbrevDayName', function(assert) {
  43. assert.equal(new Date(2020, 0, 26).getAbbrevDayName(), 'Sun', 'jan 26 2020 is a Sunday');
  44. assert.equal(new Date(2020, 9, 26).getAbbrevDayName(), 'Mon', 'oct 26 2020 is a Monday');
  45. });
  46. QUnit.test('Date.getFullDayName', function(assert) {
  47. assert.equal(new Date(2020, 0, 26).getFullDayName(), 'Sunday', 'jan 26 2020 is a Sunday');
  48. assert.equal(new Date(2020, 9, 26).getFullDayName(), 'Monday', 'oct 26 2020 is a Monday');
  49. });
  50. QUnit.test('Date.strftime', function(assert) {
  51. const date = new Date(2014, 6, 1, 11, 0, 5);
  52. assert.equal(date.strftime('%Y-%m-%d %H:%M:%S'), '2014-07-01 11:00:05');
  53. assert.equal(date.strftime('%B %d, %Y'), 'July 01, 2014');
  54. assert.equal(date.strftime('%b %d, %Y'), 'Jul 01, 2014');
  55. assert.equal(date.strftime('%a %d %m %y'), 'Tue 01 07 14');
  56. assert.equal(date.strftime('%A (day %w of week) %I %p'), 'Tuesday (day 02 of week) 11 AM');
  57. });
  58. QUnit.test('String.strptime', function(assert) {
  59. // Use UTC functions for extracting dates since the calendar uses them as
  60. // well. Month numbering starts with 0 (January).
  61. const firstParsedDate = '1988-02-26'.strptime('%Y-%m-%d');
  62. assert.equal(firstParsedDate.getUTCDate(), 26);
  63. assert.equal(firstParsedDate.getUTCMonth(), 1);
  64. assert.equal(firstParsedDate.getUTCFullYear(), 1988);
  65. // A %y value in the range of [69, 99] is in the previous century.
  66. const secondParsedDate = '26/02/88'.strptime('%d/%m/%y');
  67. assert.equal(secondParsedDate.getUTCDate(), 26);
  68. assert.equal(secondParsedDate.getUTCMonth(), 1);
  69. assert.equal(secondParsedDate.getUTCFullYear(), 1988);
  70. const format = django.get_format('DATE_INPUT_FORMATS')[0];
  71. const thirdParsedDate = '1983-11-20'.strptime(format);
  72. assert.equal(thirdParsedDate.getUTCDate(), 20);
  73. assert.equal(thirdParsedDate.getUTCMonth(), 10);
  74. assert.equal(thirdParsedDate.getUTCFullYear(), 1983);
  75. // A %y value in the range of [00, 68] is in the current century.
  76. const fourthParsedDate = '27/09/68'.strptime('%d/%m/%y');
  77. assert.equal(fourthParsedDate.getUTCDate(), 27);
  78. assert.equal(fourthParsedDate.getUTCMonth(), 8);
  79. assert.equal(fourthParsedDate.getUTCFullYear(), 2068);
  80. // Extracting from a Date object with local time must give the correct
  81. // result. Without proper conversion, timezones from GMT+0100 to GMT+1200
  82. // gives a date one day earlier than necessary, e.g. converting local time
  83. // Feb 26, 1988 00:00:00 EEST is Feb 25, 21:00:00 UTC.
  84. // Checking timezones from GMT+0100 to GMT+1200
  85. for (let i = 1; i <= 12; i++) {
  86. const tz = i > 9 ? '' + i : '0' + i;
  87. const date = new Date(Date.parse('Feb 26, 1988 00:00:00 GMT+' + tz + '00'));
  88. assert.notEqual(date.getUTCDate(), 26);
  89. assert.equal(date.getUTCDate(), 25);
  90. assert.equal(date.getUTCMonth(), 1);
  91. assert.equal(date.getUTCFullYear(), 1988);
  92. }
  93. // Checking timezones from GMT+0000 to GMT-1100
  94. for (let i = 0; i <= 11; i++) {
  95. const tz = i > 9 ? '' + i : '0' + i;
  96. const date = new Date(Date.parse('Feb 26, 1988 00:00:00 GMT-' + tz + '00'));
  97. assert.equal(date.getUTCDate(), 26);
  98. assert.equal(date.getUTCMonth(), 1);
  99. assert.equal(date.getUTCFullYear(), 1988);
  100. }
  101. });