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