|
3 | 3 | <meta name="timeout" content="long">
|
4 | 4 | <script src="/resources/testharness.js"></script>
|
5 | 5 | <script src="/resources/testharnessreport.js"></script>
|
| 6 | +<script src="support/document-lastModified-utils.js"></script> |
6 | 7 | <div id="log"></div>
|
7 | 8 | <script>
|
8 | 9 | var last_modified = document.lastModified;
|
9 | 10 |
|
10 |
| - var pattern = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/; |
11 |
| - |
12 | 11 | test(function() {
|
13 |
| - assert_regexp_match(last_modified, pattern, |
| 12 | + assert_regexp_match(last_modified, DOCUMENT_LASTMODIFIED_REGEX, |
14 | 13 | "Format should match the pattern \"NN/NN/NNNN NN:NN:NN\".");
|
15 | 14 | }, "Date returned by lastModified is in the form \"MM/DD/YYYY hh:mm:ss\".");
|
16 | 15 |
|
17 |
| - function assert_date_string_approximately_now(str) { |
18 |
| - // We want to test that |str| was a time in the user's local |
19 |
| - // timezone generated within a few seconds prior to the present. |
20 |
| - // This requires some care, since it is possible that: |
21 |
| - // - the few second difference may have crossed a |
22 |
| - // year/month/day/hour/minute boundary |
23 |
| - // - the few second difference may have crossed a change in the |
24 |
| - // local timezone's UTC offset |
25 |
| - // - the local time might be one that has multiple valid UTC |
26 |
| - // representations (for example, because it's in the hour |
27 |
| - // following a shift from summer time to winter time) |
28 |
| - // We will make some assumptions to do this: |
29 |
| - // - local time's UTC offset doesn't change more than once per |
30 |
| - // minute |
31 |
| - // - local time's UTC offset only changes by integral numbers of |
32 |
| - // minutes |
33 |
| - |
34 |
| - // The date must be equal to or earlier than the present time. |
35 |
| - var dmax = new Date(); |
36 |
| - |
37 |
| - // The date must be equal to or later than 2.5 seconds ago. |
38 |
| - var TOLERANCE_MILLISECONDS = 2500; |
39 |
| - var dmin = new Date(); |
40 |
| - dmin.setTime(dmax.getTime() - TOLERANCE_MILLISECONDS); |
41 |
| - |
42 |
| - // Extract the year/month/date/hours/minutes/seconds from str. It |
43 |
| - // is important that we do *not* try to construct a Date object from |
44 |
| - // these, since the core of the date object is a timestamp in UTC, |
45 |
| - // and there are cases (such as the hour on each side of a change |
46 |
| - // from summer time to winter time) where there are multiple |
47 |
| - // possible UTC timestamps for a given YYYY-MM-DD HH:MM:SS, and |
48 |
| - // constructing a Date object would pick one of them, which might be |
49 |
| - // the wrong one. However, we already have the right one in dmin |
50 |
| - // and dmax, so we should instead extract local time from those |
51 |
| - // rather than converting these values to UTC. |
52 |
| - var m = pattern.exec(str); |
53 |
| - var syear = Number(m[3]); |
54 |
| - var smonth = Number(m[1]) - 1; // match Javascript 0-based months |
55 |
| - var sdate = Number(m[2]); |
56 |
| - var shours = Number(m[4]); |
57 |
| - var sminutes = Number(m[5]); |
58 |
| - var sseconds = Number(m[6]); |
59 |
| - |
60 |
| - if (dmin.getFullYear() == dmax.getFullYear() && |
61 |
| - dmin.getMonth() == dmax.getMonth() && |
62 |
| - dmin.getDate() == dmax.getDate() && |
63 |
| - dmin.getHours() == dmax.getHours() && |
64 |
| - dmin.getMinutes() == dmax.getMinutes()) { |
65 |
| - // min and max have the same minute |
66 |
| - assert_equals(smonth, dmin.getMonth(), "month"); |
67 |
| - assert_equals(sdate, dmin.getDate(), "date"); |
68 |
| - assert_equals(syear, dmin.getFullYear(), "year"); |
69 |
| - assert_equals(shours, dmin.getHours(), "hours"); |
70 |
| - assert_equals(sminutes, dmin.getMinutes(), "minutes"); |
71 |
| - assert_true(dmin.getSeconds() <= sseconds && |
72 |
| - sseconds <= dmax.getSeconds(), "seconds"); |
73 |
| - } else if (dmin.getFullYear() == syear && |
74 |
| - dmin.getMonth() == smonth && |
75 |
| - dmin.getDate() == sdate && |
76 |
| - dmin.getHours() == shours && |
77 |
| - dmin.getMinutes() == sminutes) { |
78 |
| - // actual value has the same minute as min |
79 |
| - assert_true(dmin.getSeconds() <= sseconds, "dmin.getSeconds() <= sseconds"); |
80 |
| - assert_true(57 <= dmin.getSeconds(), "unexpected local time rules (dmin match)"); |
81 |
| - } else if (dmax.getFullYear() == syear && |
82 |
| - dmax.getMonth() == smonth && |
83 |
| - dmax.getDate() == sdate && |
84 |
| - dmax.getHours() == shours && |
85 |
| - dmax.getMinutes() == sminutes) { |
86 |
| - // actual value has the same minute as max |
87 |
| - assert_true(sseconds <= dmax.getSeconds(), "sseconds <= dmax.getSeconds()"); |
88 |
| - assert_true(dmax.getSeconds() <= 2, "unexpected local time rules (dmax match)"); |
89 |
| - } else { |
90 |
| - assert_unreached("unexpected local time rules (no match)"); |
91 |
| - } |
92 |
| - } |
93 |
| - |
94 | 16 | test(function() {
|
95 |
| - assert_date_string_approximately_now(last_modified); |
| 17 | + assert_document_lastmodified_string_approximately_now(last_modified); |
96 | 18 | }, "Date returned by lastModified is current at page load");
|
97 | 19 |
|
98 | 20 | var t = async_test("Date returned by lastModified is current after timeout.");
|
99 | 21 | t.step_timeout(function() {
|
100 |
| - assert_date_string_approximately_now(document.lastModified); |
| 22 | + assert_document_lastmodified_string_approximately_now(document.lastModified); |
101 | 23 | t.done();
|
102 | 24 | }, 4000);
|
103 | 25 | </script>
|
0 commit comments