Mercurial > snowl
changeset 258:919d2f75a7b6
factor out js to julian date conversions into a utils library
author | Myk Melez <myk@mozilla.org> |
---|---|
date | Fri, 22 Aug 2008 18:22:58 -0700 |
parents | 9ef95888f36e |
children | 531f02f9bc9f |
files | modules/collection.js modules/datastore.js modules/message.js modules/utils.js |
diffstat | 4 files changed, 75 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/modules/collection.js Fri Aug 22 18:01:39 2008 -0700 +++ b/modules/collection.js Fri Aug 22 18:22:58 2008 -0700 @@ -54,10 +54,12 @@ "text/plain": "text" }; +Cu.import("resource://snowl/modules/URI.js"); Cu.import("resource://snowl/modules/log4moz.js"); + Cu.import("resource://snowl/modules/datastore.js"); Cu.import("resource://snowl/modules/message.js"); -Cu.import("resource://snowl/modules/URI.js"); +Cu.import("resource://snowl/modules/utils.js"); /** * A group of messages. @@ -246,12 +248,11 @@ statement.row.subject, statement.row.author, statement.row.link, - // Convert the Julian date to a JS "ms since Unix epoch" value. - // FIXME: further convert this to a JS Date object. - Math.round((statement.row.timestamp - 2440587.5) * 86400 * 1000), + // FIXME: leave this as a JS Date object. + SnowlUtils.julianToJSDate(statement.row.timestamp).getTime(), (statement.row.read ? true : false), statement.row.authorIcon, - Math.round((statement.row.received - 2440587.5) * 86400 * 1000)); + SnowlUtils.julianToJSDate(statement.row.received).getTime()); this._messages.push(message); this._messageIndex[message.id] = message; }
--- a/modules/datastore.js Fri Aug 22 18:01:39 2008 -0700 +++ b/modules/datastore.js Fri Aug 22 18:22:58 2008 -0700 @@ -41,6 +41,8 @@ const Cr = Components.results; const Cu = Components.utils; +Cu.import("resource://snowl/modules/utils.js"); + const TABLE_TYPE_NORMAL = 0; const TABLE_TYPE_FULLTEXT = 1; @@ -472,10 +474,9 @@ this._insertMessageStatement.params.externalID = aExternalID; this._insertMessageStatement.params.subject = aSubject; this._insertMessageStatement.params.authorID = aAuthorID; - // Convert the timestamp to a Julian date. - let timestamp = aTimestamp ? aTimestamp.getTime() / 1000 / 86400 + 2440587.5 : null; + let timestamp = aTimestamp ? SnowlUtils.jsToJulianDate(aTimestamp) : null; this._insertMessageStatement.params.timestamp = timestamp; - this._insertMessageStatement.params.received = new Date().getTime() / 1000 / 86400 + 2440587.5; + this._insertMessageStatement.params.received = SnowlUtils.jsToJulianDate(new Date()); this._insertMessageStatement.params.link = aLink; this._insertMessageStatement.execute();
--- a/modules/message.js Fri Aug 22 18:01:39 2008 -0700 +++ b/modules/message.js Fri Aug 22 18:22:58 2008 -0700 @@ -54,9 +54,11 @@ "text/plain": "text" }; +Cu.import("resource://snowl/modules/URI.js"); + Cu.import("resource://snowl/modules/datastore.js"); Cu.import("resource://snowl/modules/source.js"); -Cu.import("resource://snowl/modules/URI.js"); +Cu.import("resource://snowl/modules/utils.js"); function SnowlMessage(aID, aSubject, aAuthor, aLink, aTimestamp, aRead, aAuthorIcon, aReceived) { this.id = aID; @@ -86,12 +88,11 @@ statement.row.subject, statement.row.author, statement.row.link, - // Convert the Julian date to a JS "ms since Unix epoch" value. - // FIXME: further convert this to a JS Date object. - Math.round((statement.row.timestamp - 2440587.5) * 86400 * 1000), + // FIXME: leave this as a JS Date object. + SnowlUtils.julianToJSDate(statement.row.timestamp).getTime(), (statement.row.read ? true : false), statement.row.authorIcon, - Math.round((statement.row.received - 2440587.5) * 86400 * 1000)); + SnowlUtils.julianToJSDate(statement.row.received).getTime()); } } finally {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/utils.js Fri Aug 22 18:22:58 2008 -0700 @@ -0,0 +1,59 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Snowl. + * + * The Initial Developer of the Original Code is Mozilla. + * Portions created by the Initial Developer are Copyright (C) 2008 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Myk Melez <myk@mozilla.org> + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +const EXPORTED_SYMBOLS = ["SnowlUtils"]; + +const Cc = Components.classes; +const Ci = Components.interfaces; +const Cr = Components.results; +const Cu = Components.utils; + +let SnowlUtils = { + jsToJulianDate: function(date) { + // Divide by 1000 to get seconds since Unix epoch, divide by 86400 + // to get days since Unix epoch, add the difference between the Unix epoch + // and the Julian epoch. + return date.getTime() / 1000 / 86400 + 2440587.5; + }, + + julianToJSDate: function(date) { + // Invert the function in jsToJulianDate, but round its result before + // constructing a Date object, as the Date object would truncate (floor) + // the non-integer result of the calculation, potentially resulting in + // an off-by-one error. + return new Date(Math.round((date - 2440587.5) * 86400 * 1000)); + } +};