core.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* global module, test */
  2. /* eslint global-strict: 0, strict: 0 */
  3. 'use strict';
  4. module('admin.core');
  5. test('Date.getTwelveHours', function(assert) {
  6. assert.equal(new Date(2011, 0, 1, 0, 0).getTwelveHours(), 12, '0:00');
  7. assert.equal(new Date(2011, 0, 1, 11, 0).getTwelveHours(), 11, '11:00');
  8. assert.equal(new Date(2011, 0, 1, 16, 0).getTwelveHours(), 4, '16:00');
  9. });
  10. test('Date.getTwoDigitMonth', function(assert) {
  11. assert.equal(new Date(2011, 0, 1).getTwoDigitMonth(), '01', 'jan 1');
  12. assert.equal(new Date(2011, 9, 1).getTwoDigitMonth(), '10', 'oct 1');
  13. });
  14. test('Date.getTwoDigitDate', function(assert) {
  15. assert.equal(new Date(2011, 0, 1).getTwoDigitDate(), '01', 'jan 1');
  16. assert.equal(new Date(2011, 0, 15).getTwoDigitDate(), '15', 'jan 15');
  17. });
  18. test('Date.getTwoDigitTwelveHour', function(assert) {
  19. assert.equal(new Date(2011, 0, 1, 0, 0).getTwoDigitTwelveHour(), '12', '0:00');
  20. assert.equal(new Date(2011, 0, 1, 4, 0).getTwoDigitTwelveHour(), '04', '4:00');
  21. assert.equal(new Date(2011, 0, 1, 22, 0).getTwoDigitTwelveHour(), '10', '22:00');
  22. });
  23. test('Date.getTwoDigitHour', function(assert) {
  24. assert.equal(new Date(2014, 6, 1, 9, 0).getTwoDigitHour(), '09', '9:00 am is 09');
  25. assert.equal(new Date(2014, 6, 1, 11, 0).getTwoDigitHour(), '11', '11:00 am is 11');
  26. });
  27. test('Date.getTwoDigitMinute', function(assert) {
  28. assert.equal(new Date(2014, 6, 1, 0, 5).getTwoDigitMinute(), '05', '12:05 am is 05');
  29. assert.equal(new Date(2014, 6, 1, 0, 15).getTwoDigitMinute(), '15', '12:15 am is 15');
  30. });
  31. test('Date.getTwoDigitSecond', function(assert) {
  32. assert.equal(new Date(2014, 6, 1, 0, 0, 2).getTwoDigitSecond(), '02', '12:00:02 am is 02');
  33. assert.equal(new Date(2014, 6, 1, 0, 0, 20).getTwoDigitSecond(), '20', '12:00:20 am is 20');
  34. });
  35. test('Date.getHourMinute', function(assert) {
  36. assert.equal(new Date(2014, 6, 1, 11, 0).getHourMinute(), '11:00', '11:00 am is 11:00');
  37. assert.equal(new Date(2014, 6, 1, 13, 25).getHourMinute(), '13:25', '1:25 pm is 13:25');
  38. });
  39. test('Date.getHourMinuteSecond', function(assert) {
  40. assert.equal(new Date(2014, 6, 1, 11, 0, 0).getHourMinuteSecond(), '11:00:00', '11:00 am is 11:00:00');
  41. assert.equal(new Date(2014, 6, 1, 17, 45, 30).getHourMinuteSecond(), '17:45:30', '5:45:30 pm is 17:45:30');
  42. });
  43. test('Date.strftime', function(assert) {
  44. var date = new Date(2014, 6, 1, 11, 0, 5);
  45. assert.equal(date.strftime('%Y-%m-%d %H:%M:%S'), '2014-07-01 11:00:05');
  46. });
  47. test('String.strptime', function(assert) {
  48. // Use UTC functions for extracting dates since the calendar uses them as
  49. // well. Month numbering starts with 0 (January).
  50. var firstParsedDate = '1988-02-26'.strptime('%Y-%m-%d');
  51. assert.equal(firstParsedDate.getUTCDate(), 26);
  52. assert.equal(firstParsedDate.getUTCMonth(), 1);
  53. assert.equal(firstParsedDate.getUTCFullYear(), 1988);
  54. var secondParsedDate = '26/02/88'.strptime('%d/%m/%y');
  55. assert.equal(secondParsedDate.getUTCDate(), 26);
  56. assert.equal(secondParsedDate.getUTCMonth(), 1);
  57. assert.equal(secondParsedDate.getUTCFullYear(), 1988);
  58. var format = django.get_format('DATE_INPUT_FORMATS')[0];
  59. var thirdParsedDate = '1983-11-20'.strptime(format);
  60. assert.equal(thirdParsedDate.getUTCDate(), 20);
  61. assert.equal(thirdParsedDate.getUTCMonth(), 10);
  62. assert.equal(thirdParsedDate.getUTCFullYear(), 1983);
  63. // Extracting from a Date object with local time must give the correct
  64. // result. Without proper conversion, timezones from GMT+0100 to GMT+1200
  65. // gives a date one day earlier than necessary, e.g. converting local time
  66. // Feb 26, 1988 00:00:00 EEST is Feb 25, 21:00:00 UTC.
  67. // Checking timezones from GMT+0100 to GMT+1200
  68. var i, tz, date;
  69. for (i = 1; i <= 12; i++) {
  70. tz = i > 9 ? '' + i : '0' + i;
  71. date = new Date(Date.parse('Feb 26, 1988 00:00:00 GMT+' + tz + '00'));
  72. assert.notEqual(date.getUTCDate(), 26);
  73. assert.equal(date.getUTCDate(), 25);
  74. assert.equal(date.getUTCMonth(), 1);
  75. assert.equal(date.getUTCFullYear(), 1988);
  76. }
  77. // Checking timezones from GMT+0000 to GMT-1100
  78. for (i = 0; i <= 11; i++) {
  79. tz = i > 9 ? '' + i : '0' + i;
  80. date = new Date(Date.parse('Feb 26, 1988 00:00:00 GMT-' + tz + '00'));
  81. assert.equal(date.getUTCDate(), 26);
  82. assert.equal(date.getUTCMonth(), 1);
  83. assert.equal(date.getUTCFullYear(), 1988);
  84. }
  85. });