419433 - r=sayrer, a=beltzner - add back my own ISO date parsing - see bug for more detals

This commit is contained in:
mkaply@us.ibm.com 2008-02-27 05:28:55 -08:00
Родитель 2e9ea3f983
Коммит 9cf0190ab8
2 изменённых файлов: 72 добавлений и 5 удалений

Просмотреть файл

@ -1,5 +1,3 @@
Components.utils.import("resource://gre/modules/ISO8601DateUtils.jsm");
var EXPORTED_SYMBOLS = ["Microformats", "adr", "tag", "hCard", "hCalendar", "geo"];
var Microformats = {
@ -864,6 +862,65 @@ var Microformats = {
return dateString;
}
},
/**
* Converts an ISO8601 date into a JavaScript date object, honoring the TZ
* offset and Z if present to convert the date to local time
* NOTE: I'm using an extra parameter on the date object for this function.
* I set date.time to true if there is a date, otherwise date.time is false.
*
* @param string ISO8601 formatted date
* @return JavaScript date object that represents the ISO date.
*/
dateFromISO8601: function dateFromISO8601(string) {
var dateArray = string.match(/(\d\d\d\d)(?:-?(\d\d)(?:-?(\d\d)(?:[T ](\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d+))?)?)?(?:([-+Z])(?:(\d\d)(?::?(\d\d))?)?)?)?)?)?/);
var date = new Date(dateArray[1], 0, 1);
date.time = false;
if (dateArray[2]) {
date.setMonth(dateArray[2] - 1);
}
if (dateArray[3]) {
date.setDate(dateArray[3]);
}
if (dateArray[4]) {
date.setHours(dateArray[4]);
date.time = true;
if (dateArray[5]) {
date.setMinutes(dateArray[5]);
if (dateArray[6]) {
date.setSeconds(dateArray[6]);
if (dateArray[7]) {
date.setMilliseconds(Number("0." + dateArray[7]) * 1000);
}
}
}
}
if (dateArray[8]) {
if (dateArray[8] == "-") {
if (dateArray[9] && dateArray[10]) {
date.setHours(date.getHours() + parseInt(dateArray[9], 10));
date.setMinutes(date.getMinutes() + parseInt(dateArray[10], 10));
}
} else if (dateArray[8] == "+") {
if (dateArray[9] && dateArray[10]) {
date.setHours(date.getHours() - parseInt(dateArray[9], 10));
date.setMinutes(date.getMinutes() - parseInt(dateArray[10], 10));
}
}
/* at this point we have the time in gmt */
/* convert to local if we had a Z - or + */
if (dateArray[8]) {
var tzOffset = date.getTimezoneOffset();
if (tzOffset < 0) {
date.setMinutes(date.getMinutes() + tzOffset);
} else if (tzOffset > 0) {
date.setMinutes(date.getMinutes() - tzOffset);
}
}
}
return date;
},
/**
* Converts a Javascript date object into an ISO 8601 formatted date
* NOTE: I'm using an extra parameter on the date object for this function.
@ -1323,7 +1380,7 @@ hCalendar.prototype.toString = function() {
if (summaries.length === 0) {
if (this.summary) {
if (this.dtstart) {
return this.summary + " (" + ISO8601DateUtils.parse(this.dtstart).toLocaleString() + ")";
return this.summary + " (" + Microformats.dateFromISO8601(this.dtstart).toLocaleString() + ")";
}
}
}

Просмотреть файл

@ -80,6 +80,11 @@
<span class="title">CEO</span>
</div>
<div class="vevent" id="date_vcal">
<span class="description">Mozilla's Birthday</span>
<span class="dtstart">1998-01-22</span>
</div>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
@ -91,7 +96,7 @@ function test_Microformats() {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
Components.utils.import("resource://gre/modules/Microformats.js");
ok(Microformats, "Check global access to Microformats");
var hCards = Microformats.get("hCard", document.getElementById("test_1"), {showHidden: true});
is(hCards.length, 1, "Check Microformats.get");
@ -141,7 +146,12 @@ function test_Microformats() {
is(nestCard2.title, "Executive Assistant", "nesting (title) 2");
is(nestCard3.fn, "Bob Smith", "nesting (fn) 3");
is(nestCard3.title, "Office Assistant", "nesting (title) 3");
is(nestCard1.agent[0].agent[0].fn, "Bob Smith", "nesting all");
is(nestCard1.agent[0].agent[0].fn, "Bob Smith", "nesting all");
var dateCal = new hCalendar(document.getElementById("date_vcal"));
jsdate = Microformats.dateFromISO8601(dateCal.dtstart);
origdate = Microformats.iso8601FromDate(jsdate, true);
is(dateCal.dtstart, origdate, "date round trip");
}
function test_hCard() {