fix $.fudgeDateForProfileTimezone for safari

Fixes: CORE-3061

Safari doesn’t handle passing strings to `new Date(<string>)` like other
browsers do. When you pass a string like that to `new Date()` in Safari,
it gives you back `Invalid Date`  So for this method to continue working
like it historically did, in safari, we need to fall back to Date.js’s 
nonstandard monkeypatched version of Date.parse(<string>).

Test plan:
Dev test:
*run `yarn jspec-watch spec/coffeescripts/instructureDateAndTimeSpec.js`
* when the chrome browser opens, copy the url in the url bar and then
  open that in Safari 
  (it’ll be something like: http://localhost:9876/?id=87294339)
* all the tests should run in safari and they all should pass

Manual test:
* in Safari
* upload a file to your course
* go to /courses/x/files
* the page should load properly and the `Date Created` and 
  `Date Modified` columns for that uploaded file should be correct and 
  the same as what you see when you load the page in chrome

Change-Id: I11bf5291617d881254fc34c4555ec640a623e733
Reviewed-on: https://gerrit.instructure.com/196896
Tested-by: Jenkins
Reviewed-by: Brent Burgoyne <bburgoyne@instructure.com>
QA-Review: Brent Burgoyne <bburgoyne@instructure.com>
Product-Review: Brent Burgoyne <bburgoyne@instructure.com>
This commit is contained in:
Ryan Shaw 2019-06-07 11:31:54 -04:00
parent 0e0642e45b
commit cae895c9e4
2 changed files with 16 additions and 9 deletions

View File

@ -33,14 +33,23 @@ import 'jqueryui/datepicker'
$.fudgeDateForProfileTimezone = function(date) {
date = tz.parse(date);
if (!date) return null;
// format true date into profile timezone without tz-info, then parse in
// browser timezone. then, as desired:
// output.toString('yyyy-MM-dd hh:mm:ss') == tz.format(input, '%Y-%m-%d %H:%M:%S')
var year = tz.format(date, '%Y');
let year = tz.format(date, '%Y');
while (year.length < 4) year = "0" + year;
const formatted = tz.format(date, year + '-%m-%d %T')
const fudgedDate = new Date(formatted)
let fudgedDate = new Date(formatted)
// In Safari, the return value from new Date(<string>) might be `Invalid Date`.
// this is because, according to this note on:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Timestamp_string
// "Support for RFC 2822 format strings is by convention only."
// So for those cases, we fall back on date.js's monkeypatched version of Date.parse,
// which is what this method has always historically used before the speed optimization of using new Date()
// eslint-disable-next-line no-restricted-globals
if (isNaN(fudgedDate)) { // checking for isNaN(<date>) is how you check for `Invalid Date`
fudgedDate = Date.parse(formatted)
}
return fudgedDate
}

View File

@ -26,9 +26,8 @@ import 'jquery.instructure_date_and_time'
QUnit.module('fudgeDateForProfileTimezone', {
setup() {
let expectedTimestamp
this.snapshot = tz.snapshot()
this.original = new Date((expectedTimestamp = Date.UTC(2013, 8, 1)))
this.original = new Date(Date.UTC(2013, 8, 1))
},
teardown() {
tz.restore(this.snapshot)
@ -75,9 +74,8 @@ test('should be sensitive to profile time zone', function() {
QUnit.module('unfudgeDateForProfileTimezone', {
setup() {
let expectedTimestamp
this.snapshot = tz.snapshot()
this.original = new Date((expectedTimestamp = Date.UTC(2013, 8, 1)))
this.original = new Date(Date.UTC(2013, 8, 1))
},
teardown() {
tz.restore(this.snapshot)