From 6a27c1bf7805e75febe107093b414ead45588121 Mon Sep 17 00:00:00 2001 From: Max Goodman Date: Fri, 25 Apr 2014 14:15:25 -0700 Subject: [PATCH] Add iso8601 polyfill necessary for timetext in old browsers. --- r2/r2/lib/js.py | 1 + r2/r2/public/static/js/lib/iso8601.js | 41 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 r2/r2/public/static/js/lib/iso8601.js diff --git a/r2/r2/lib/js.py b/r2/r2/lib/js.py index 45466c1ba..6b898dad5 100755 --- a/r2/r2/lib/js.py +++ b/r2/r2/lib/js.py @@ -450,6 +450,7 @@ module["reddit"] = LocalizedModule("reddit.js", "lib/jquery.cookie.js", "lib/jquery.url.js", "lib/backbone-1.0.0.js", + "lib/iso8601.js", "templates.js", "scrollupdater.js", "timetext.js", diff --git a/r2/r2/public/static/js/lib/iso8601.js b/r2/r2/public/static/js/lib/iso8601.js new file mode 100644 index 000000000..8b2e9a810 --- /dev/null +++ b/r2/r2/public/static/js/lib/iso8601.js @@ -0,0 +1,41 @@ +/** + * Date.parse with progressive enhancement for ISO 8601 + * © 2011 Colin Snover + * Released under MIT license. + */ +(function (Date, undefined) { + var origParse = Date.parse, numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ]; + Date.parse = function (date) { + var timestamp, struct, minutesOffset = 0; + + // ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string + // before falling back to any implementation-specific date parsing, so that’s what we do, even if native + // implementations could be faster + // 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm + if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) { + // avoid NaN timestamps caused by “undefined” values being passed to Date.UTC + for (var i = 0, k; (k = numericKeys[i]); ++i) { + struct[k] = +struct[k] || 0; + } + + // allow undefined days and months + struct[2] = (+struct[2] || 1) - 1; + struct[3] = +struct[3] || 1; + + if (struct[8] !== 'Z' && struct[9] !== undefined) { + minutesOffset = struct[10] * 60 + struct[11]; + + if (struct[9] === '+') { + minutesOffset = 0 - minutesOffset; + } + } + + timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]); + } + else { + timestamp = origParse ? origParse(date) : NaN; + } + + return timestamp; + }; +}(Date)); \ No newline at end of file