Skip to content

Commit 6ec68d1

Browse files
Xotic750Xotic750
authored andcommitted
Fix date methods
1 parent 35403c7 commit 6ec68d1

File tree

2 files changed

+510
-50
lines changed

2 files changed

+510
-50
lines changed

es5-shim.js

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,202 @@ defineProperties($Object, {
10761076
// ====
10771077
//
10781078

1079+
var hasNegativeMonthYearBug = new Date(-3509827329600292).getUTCMonth() !== 0;
1080+
var aNegativeTestDate = new Date(-1509842289600292);
1081+
var aPositiveTestDate = new Date(1449662400000);
1082+
var hasToUTCStringFormatBug = aNegativeTestDate.toUTCString() !== 'Mon, 01 Jan -45875 11:59:59 GMT';
1083+
var hasToDateStringFormatBug;
1084+
var hasToStringFormatBug;
1085+
var timeZoneOffset = aNegativeTestDate.getTimezoneOffset();
1086+
if (timeZoneOffset < -720) {
1087+
hasToDateStringFormatBug = aNegativeTestDate.toDateString() !== 'Tue Jan 02 -45875';
1088+
hasToStringFormatBug = !(/^Thu Dec 10 2015 \d\d:\d\d:\d\d GMT[-\+]\d\d\d\d(?: |$)/).test(aPositiveTestDate.toString());
1089+
} else {
1090+
hasToDateStringFormatBug = aNegativeTestDate.toDateString() !== 'Mon Jan 01 -45875';
1091+
hasToStringFormatBug = !(/^Wed Dec 09 2015 \d\d:\d\d:\d\d GMT[-\+]\d\d\d\d(?: |$)/).test(aPositiveTestDate.toString());
1092+
}
1093+
1094+
var originalGetFullYear;
1095+
var originalGetMonth;
1096+
var originalGetDate;
1097+
var originalGetUTCFullYear;
1098+
var originalGetUTCMonth;
1099+
var originalGetUTCDate;
1100+
var originalToUTCString;
1101+
var originalToDateString;
1102+
var originalToString;
1103+
var dayName;
1104+
var monthName;
1105+
var daysInMonth;
1106+
if (hasNegativeMonthYearBug || hasToDateStringFormatBug || hasToUTCStringFormatBug || hasToStringFormatBug) {
1107+
originalGetFullYear = Date.prototype.getFullYear;
1108+
originalGetMonth = Date.prototype.getMonth;
1109+
originalGetDate = Date.prototype.getDate;
1110+
originalGetUTCFullYear = Date.prototype.getUTCFullYear;
1111+
originalGetUTCMonth = Date.prototype.getUTCMonth;
1112+
originalGetUTCDate = Date.prototype.getUTCDate;
1113+
originalToUTCString = Date.prototype.toUTCString;
1114+
originalToDateString = Date.prototype.toDateString;
1115+
originalToString = Date.prototype.toString;
1116+
dayName = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
1117+
monthName = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
1118+
daysInMonth = function dim(month, year) {
1119+
return originalGetDate.call(new Date(year, month, 0));
1120+
};
1121+
}
1122+
1123+
defineProperties(Date.prototype, {
1124+
getFullYear: function getFullYear() {
1125+
if (!this || !(this instanceof Date)) {
1126+
throw new TypeError('this is not a Date object.');
1127+
}
1128+
var year = originalGetFullYear.call(this);
1129+
if (year < 0 && originalGetMonth.call(this) > 11) {
1130+
return year + 1;
1131+
}
1132+
return year;
1133+
},
1134+
getMonth: function getMonth() {
1135+
if (!this || !(this instanceof Date)) {
1136+
throw new TypeError('this is not a Date object.');
1137+
}
1138+
var year = originalGetFullYear.call(this);
1139+
var month = originalGetMonth.call(this);
1140+
if (year < 0 && month > 11) {
1141+
return 0;
1142+
}
1143+
return month;
1144+
},
1145+
getDate: function getDate() {
1146+
if (!this || !(this instanceof Date)) {
1147+
throw new TypeError('this is not a Date object.');
1148+
}
1149+
var year = originalGetFullYear.call(this);
1150+
var month = originalGetMonth.call(this);
1151+
var date = originalGetDate.call(this);
1152+
if (year < 0 && month > 11) {
1153+
if (month === 12) {
1154+
return date;
1155+
}
1156+
var days = daysInMonth(0, year + 1);
1157+
return (days - date) + 1;
1158+
}
1159+
return date;
1160+
},
1161+
getUTCFullYear: function getUTCFullYear() {
1162+
if (!this || !(this instanceof Date)) {
1163+
throw new TypeError('this is not a Date object.');
1164+
}
1165+
var year = originalGetUTCFullYear.call(this);
1166+
if (year < 0 && originalGetUTCMonth.call(this) > 11) {
1167+
return year + 1;
1168+
}
1169+
return year;
1170+
},
1171+
getUTCMonth: function getUTCMonth() {
1172+
if (!this || !(this instanceof Date)) {
1173+
throw new TypeError('this is not a Date object.');
1174+
}
1175+
var year = originalGetUTCFullYear.call(this);
1176+
var month = originalGetUTCMonth.call(this);
1177+
if (year < 0 && month > 11) {
1178+
return 0;
1179+
}
1180+
return month;
1181+
},
1182+
getUTCDate: function getUTCDate() {
1183+
if (!this || !(this instanceof Date)) {
1184+
throw new TypeError('this is not a Date object.');
1185+
}
1186+
var year = originalGetUTCFullYear.call(this);
1187+
var month = originalGetUTCMonth.call(this);
1188+
var date = originalGetUTCDate.call(this);
1189+
if (year < 0 && month > 11) {
1190+
if (month === 12) {
1191+
return date;
1192+
}
1193+
var days = daysInMonth(0, year + 1);
1194+
return (days - date) + 1;
1195+
}
1196+
return date;
1197+
}
1198+
}, hasNegativeMonthYearBug);
1199+
1200+
defineProperties(Date.prototype, {
1201+
toUTCString: function toUTCString() {
1202+
if (!this || !(this instanceof Date)) {
1203+
throw new TypeError('this is not a Date object.');
1204+
}
1205+
var day = this.getUTCDay();
1206+
var date = this.getUTCDate();
1207+
var month = this.getUTCMonth();
1208+
var year = this.getUTCFullYear();
1209+
var hour = this.getUTCHours();
1210+
var minute = this.getUTCMinutes();
1211+
var second = this.getUTCSeconds();
1212+
return dayName[day] + ', ' +
1213+
(date < 10 ? '0' + date : date) + ' ' +
1214+
monthName[month] + ' ' +
1215+
year + ' ' +
1216+
(hour < 10 ? '0' + hour : hour) + ':' +
1217+
(minute < 10 ? '0' + minute : minute) + ':' +
1218+
(second < 10 ? '0' + second : second) + ' GMT';
1219+
}
1220+
}, hasNegativeMonthYearBug || hasToUTCStringFormatBug);
1221+
1222+
// Opera 12 has `,`
1223+
defineProperties(Date.prototype, {
1224+
toDateString: function toDateString() {
1225+
if (!this || !(this instanceof Date)) {
1226+
throw new TypeError('this is not a Date object.');
1227+
}
1228+
var day = this.getDay();
1229+
var date = this.getDate();
1230+
var month = this.getMonth();
1231+
var year = this.getFullYear();
1232+
return dayName[day] + ' ' +
1233+
monthName[month] + ' ' +
1234+
(date < 10 ? '0' + date : date) + ' ' +
1235+
year;
1236+
}
1237+
}, hasNegativeMonthYearBug || hasToDateStringFormatBug);
1238+
1239+
// can't use defineProperties here because of toString enumeration issue in IE <= 8
1240+
if (hasNegativeMonthYearBug || hasToStringFormatBug) {
1241+
Date.prototype.toString = function toString() {
1242+
if (!this || !(this instanceof Date)) {
1243+
throw new TypeError('this is not a Date object.');
1244+
}
1245+
var day = this.getDay();
1246+
var date = this.getDate();
1247+
var month = this.getMonth();
1248+
var year = this.getFullYear();
1249+
var hour = this.getHours();
1250+
var minute = this.getMinutes();
1251+
var second = this.getSeconds();
1252+
var timezoneOffset = this.getTimezoneOffset();
1253+
var hoursOffset = Math.floor(Math.abs(timezoneOffset) / 60);
1254+
var minutesOffset = Math.floor(Math.abs(timezoneOffset) % 60);
1255+
return dayName[day] + ' ' +
1256+
monthName[month] + ' ' +
1257+
(date < 10 ? '0' + date : date) + ' ' +
1258+
year + ' ' +
1259+
(hour < 10 ? '0' + hour : hour) + ':' +
1260+
(minute < 10 ? '0' + minute : minute) + ':' +
1261+
(second < 10 ? '0' + second : second) + ' GMT' +
1262+
(timezoneOffset > 0 ? '-' : '+') +
1263+
(hoursOffset < 10 ? '0' + hoursOffset : hoursOffset) +
1264+
(minutesOffset < 10 ? '0' + minutesOffset : minutesOffset);
1265+
};
1266+
if (supportsDescriptors) {
1267+
$Object.defineProperty(Date.prototype, 'toString', {
1268+
configurable: true,
1269+
enumerable: false,
1270+
writable: true
1271+
});
1272+
}
1273+
}
1274+
10791275
// ES5 15.9.5.43
10801276
// http://es5.github.com/#x15.9.5.43
10811277
// This function returns a String value represent the instance in time

0 commit comments

Comments
 (0)