annotate modules/utils.js @ 322:3940ef5aa170

let users choose a period of time from which to see messages in the river view
author Myk Melez <myk@mozilla.org>
date Tue, 14 Oct 2008 11:34:46 -0700
parents b09b7f436e1a
children 3a8dfaf1ac31
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
258
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
1 /* ***** BEGIN LICENSE BLOCK *****
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
3 *
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
4 * The contents of this file are subject to the Mozilla Public License Version
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
5 * 1.1 (the "License"); you may not use this file except in compliance with
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
6 * the License. You may obtain a copy of the License at
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
7 * http://www.mozilla.org/MPL/
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
8 *
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
9 * Software distributed under the License is distributed on an "AS IS" basis,
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
11 * for the specific language governing rights and limitations under the
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
12 * License.
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
13 *
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
14 * The Original Code is Snowl.
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
15 *
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
16 * The Initial Developer of the Original Code is Mozilla.
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
17 * Portions created by the Initial Developer are Copyright (C) 2008
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
18 * the Initial Developer. All Rights Reserved.
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
19 *
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
20 * Contributor(s):
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
21 * Myk Melez <myk@mozilla.org>
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
22 *
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
23 * Alternatively, the contents of this file may be used under the terms of
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
26 * in which case the provisions of the GPL or the LGPL are applicable instead
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
27 * of those above. If you wish to allow use of your version of this file only
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
28 * under the terms of either the GPL or the LGPL, and not to allow others to
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
29 * use your version of this file under the terms of the MPL, indicate your
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
30 * decision by deleting the provisions above and replace them with the notice
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
31 * and other provisions required by the GPL or the LGPL. If you do not delete
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
32 * the provisions above, a recipient may use your version of this file under
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
33 * the terms of any one of the MPL, the GPL or the LGPL.
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
34 *
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
35 * ***** END LICENSE BLOCK ***** */
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
36
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
37 const EXPORTED_SYMBOLS = ["SnowlUtils"];
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
38
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
39 const Cc = Components.classes;
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
40 const Ci = Components.interfaces;
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
41 const Cr = Components.results;
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
42 const Cu = Components.utils;
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
43
320
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
44 let msInDay = (1000 * 60 * 60 * 24);
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
45
258
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
46 let SnowlUtils = {
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
47 jsToJulianDate: function(date) {
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
48 // Divide by 1000 to get seconds since Unix epoch, divide by 86400
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
49 // to get days since Unix epoch, add the difference between the Unix epoch
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
50 // and the Julian epoch.
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
51 return date.getTime() / 1000 / 86400 + 2440587.5;
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
52 },
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
53
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
54 julianToJSDate: function(date) {
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
55 // Invert the function in jsToJulianDate, but round its result before
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
56 // constructing a Date object, as the Date object would truncate (floor)
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
57 // the non-integer result of the calculation, potentially resulting in
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
58 // an off-by-one error.
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
59 return new Date(Math.round((date - 2440587.5) * 86400 * 1000));
263
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
60 },
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
61
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
62 // Date Formatting Service
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
63 get _dfSvc() {
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
64 delete this._dfSvc;
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
65 return this._dfSvc = Cc["@mozilla.org/intl/scriptabledateformat;1"].
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
66 getService(Ci.nsIScriptableDateFormat);
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
67 },
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
68
320
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
69 // FIXME: make this localizable.
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
70 days: {
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
71 0: "Sunday",
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
72 1: "Monday",
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
73 2: "Tuesday",
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
74 3: "Wednesday",
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
75 4: "Thursday",
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
76 5: "Friday",
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
77 6: "Saturday"
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
78 },
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
79
290
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
80 get today() {
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
81 let sometimeToday = new Date();
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
82 return new Date(sometimeToday.getFullYear(),
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
83 sometimeToday.getMonth(),
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
84 sometimeToday.getDate());
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
85 },
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
86
320
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
87 // FIXME: accommodate daylight savings time, which could cause
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
88 // these calculations to be incorrect at times.
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
89
290
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
90 get tomorrow() {
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
91 let sometimeTomorrow = new Date(new Date() + (1000 * 60 * 60 * 24));
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
92 return new Date(sometimeTomorrow.getFullYear(),
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
93 sometimeTomorrow.getMonth(),
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
94 sometimeTomorrow.getDate());
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
95 },
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
96
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
97 get yesterday() {
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
98 let sometimeYesterday = new Date(new Date() - (1000 * 60 * 60 * 24));
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
99 return new Date(sometimeYesterday.getFullYear(),
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
100 sometimeYesterday.getMonth(),
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
101 sometimeYesterday.getDate());
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
102 },
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
103
320
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
104 twoDaysAgo: {
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
105 get epoch() { return new Date(SnowlUtils.today - (msInDay * 2)) },
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
106 get name() { return SnowlUtils.days[this.epoch.getDay()] }
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
107 },
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
108
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
109 threeDaysAgo: {
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
110 get epoch() { return new Date(SnowlUtils.today - (msInDay * 3)) },
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
111 get name() { return SnowlUtils.days[this.epoch.getDay()] }
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
112 },
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
113
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
114 fourDaysAgo: {
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
115 get epoch() { return new Date(SnowlUtils.today - (msInDay * 4)) },
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
116 get name() { return SnowlUtils.days[this.epoch.getDay()] }
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
117 },
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
118
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
119 fiveDaysAgo: {
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
120 get epoch() { return new Date(SnowlUtils.today - (msInDay * 5)) },
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
121 get name() { return SnowlUtils.days[this.epoch.getDay()] }
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
122 },
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
123
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
124 sixDaysAgo: {
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
125 get epoch() { return new Date(SnowlUtils.today - (msInDay * 6)) },
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
126 get name() { return SnowlUtils.days[this.epoch.getDay()] }
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
127 },
b09b7f436e1a only show a week's worth of messages, and group them by date, in the stream view
Myk Melez <myk@mozilla.org>
parents: 290
diff changeset
128
322
3940ef5aa170 let users choose a period of time from which to see messages in the river view
Myk Melez <myk@mozilla.org>
parents: 320
diff changeset
129 twentyNineDaysAgo: {
3940ef5aa170 let users choose a period of time from which to see messages in the river view
Myk Melez <myk@mozilla.org>
parents: 320
diff changeset
130 get epoch() { return new Date(SnowlUtils.today - (msInDay * 29)) },
3940ef5aa170 let users choose a period of time from which to see messages in the river view
Myk Melez <myk@mozilla.org>
parents: 320
diff changeset
131 get name() { return "Twenty Nine Days Ago" }
3940ef5aa170 let users choose a period of time from which to see messages in the river view
Myk Melez <myk@mozilla.org>
parents: 320
diff changeset
132 },
3940ef5aa170 let users choose a period of time from which to see messages in the river view
Myk Melez <myk@mozilla.org>
parents: 320
diff changeset
133
263
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
134 /**
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
135 * Formats a date for human consumption using the date formatting service
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
136 * for locale-specific formatting along with some additional smarts for more
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
137 * human-readable representations of recent dates.
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
138 * @param date {Date} the date to format
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
139 * @returns a human-readable string representing the date
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
140 */
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
141 _formatDate: function(date) {
264
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
142 let day = new Date(date.getFullYear(), date.getMonth(), date.getDate());
263
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
143 let now = new Date();
264
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
144 let today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
263
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
145
290
913ecc9089e1 primitive implementation of temporal grouping in the stream view
Myk Melez <myk@mozilla.org>
parents: 264
diff changeset
146 let yesterday = this.yesterday;
263
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
147
264
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
148 let sixDaysAgo = new Date(now - 1000 * 60 * 60 * 24 * 6);
263
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
149 sixDaysAgo = new Date(sixDaysAgo.getFullYear(),
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
150 sixDaysAgo.getMonth(),
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
151 sixDaysAgo.getDate());
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
152
264
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
153 // If it's in the future or more than six days in the past, format it
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
154 // as a full date/time string, i.e.: 2008-05-13 15:37:42.
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
155 if (day > today || day < sixDaysAgo)
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
156 return this._dfSvc.FormatDateTime("",
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
157 Ci.nsIScriptableDateFormat.dateFormatShort,
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
158 Ci.nsIScriptableDateFormat.timeFormatNoSeconds,
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
159 date.getFullYear(),
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
160 date.getMonth() + 1,
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
161 date.getDate(),
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
162 date.getHours(),
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
163 date.getMinutes(),
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
164 date.getSeconds());
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
165
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
166 // If it's today, only show the time.
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
167 if (day.getTime() == today.getTime())
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
168 return this._dfSvc.FormatTime("",
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
169 Ci.nsIScriptableDateFormat.timeFormatNoSeconds,
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
170 date.getHours(),
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
171 date.getMinutes(),
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
172 null);
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
173
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
174 // If it's yesterday, show "Yesterday" plus the time.
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
175 // FIXME: make this localizable.
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
176 if (day.getTime() == yesterday.getTime())
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
177 return "Yesterday " +
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
178 this._dfSvc.FormatTime("",
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
179 Ci.nsIScriptableDateFormat.timeFormatNoSeconds,
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
180 date.getHours(),
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
181 date.getMinutes(),
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
182 null);
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
183
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
184 // It's two to six days ago, so show the day of the week plus the time.
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
185 return this._dfSvc.FormatDateTime("",
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
186 Ci.nsIScriptableDateFormat.dateFormatWeekday,
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
187 Ci.nsIScriptableDateFormat.timeFormatNoSeconds,
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
188 date.getFullYear(),
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
189 date.getMonth() + 1,
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
190 date.getDate(),
263
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
191 date.getHours(),
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
192 date.getMinutes(),
264
e294475faeb2 properly format dates in the future
Myk Melez <myk@mozilla.org>
parents: 263
diff changeset
193 date.getSeconds());
258
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
194 }
263
848e068587c5 factor out date formatting into shared util function
Myk Melez <myk@mozilla.org>
parents: 258
diff changeset
195
258
919d2f75a7b6 factor out js to julian date conversions into a utils library
Myk Melez <myk@mozilla.org>
parents:
diff changeset
196 };