-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenhancedDateFormatter.js
202 lines (176 loc) · 7.89 KB
/
enhancedDateFormatter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* Enhanced Date Formatter namespace.
*/
if (typeof EnhancedDateFormatter == "undefined") {
var EnhancedDateFormatter = {};
};
EnhancedDateFormatter.getDateFormatForDate = function (date) {
var dateFormat = EnhancedDateFormatter.preferences.getValue('defaultDateFormat', '');
// JS dates are stored as milliseconds since midnight on
// January 1, 1970, UTC, and there are 86400000 in a day, so
// to get the number of days, just divide by 86400000 and
// floor the result
// Additionally, in order to correct for the timezone (so we're
// not getting day numbers in UTC), we need to subtract off the
// timezone offset (which is given in minutes, so we need to
// multiply by 60000 to get milliseconds)
var todayDate = new Date();
var todayDay = Math.floor((todayDate.valueOf()-todayDate.getTimezoneOffset()*60000)/86400000);
var dateDay = Math.floor((date.valueOf()-date.getTimezoneOffset()*60000)/86400000);
if (EnhancedDateFormatter.preferences.getValue('useCustomFormatForLastWeek', false) &&
dateDay > (todayDay-6)) {
dateFormat = EnhancedDateFormatter.preferences.getValue('lastWeekDateFormat', '');
}
if (EnhancedDateFormatter.preferences.getValue('useCustomFormatForYesterday', false) &&
dateDay == (todayDay-1)) {
dateFormat = EnhancedDateFormatter.preferences.getValue('yesterdayDateFormat', '');
}
if (EnhancedDateFormatter.preferences.getValue('useCustomFormatForToday', false) &&
dateDay == todayDay) {
dateFormat = EnhancedDateFormatter.preferences.getValue('todayDateFormat', '');
}
return dateFormat;
};
EnhancedDateFormatter.strftime = function(sFormat, date) {
if (!(date instanceof Date)) date = new Date();
var nDay = date.getDay(),
nDate = date.getDate(),
nMonth = date.getMonth(),
nYear = date.getFullYear(),
nHour = date.getHours(),
aDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
aMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
aDayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
isLeapYear = function() {
return (nYear%4===0 && nYear%100!==0) || nYear%400===0;
},
getThursday = function() {
var target = new Date(date);
target.setDate(nDate - ((nDay+6)%7) + 3);
return target;
},
zeroPad = function(nNum, nPad) {
return ((Math.pow(10, nPad) + nNum) + '').slice(1);
};
return sFormat.replace(/%[a-z]/gi, function(sMatch) {
return (({
'%a': aDays[nDay].slice(0,3),
'%A': aDays[nDay],
'%b': aMonths[nMonth].slice(0,3),
'%B': aMonths[nMonth],
'%c': date.toUTCString(),
'%C': Math.floor(nYear/100),
'%d': zeroPad(nDate, 2),
'%e': nDate,
'%F': date.toISOString().slice(0,10),
'%G': getThursday().getFullYear(),
'%g': (getThursday().getFullYear() + '').slice(2),
'%H': zeroPad(nHour, 2),
'%I': zeroPad((nHour+11)%12 + 1, 2),
'%j': zeroPad(aDayCount[nMonth] + nDate + ((nMonth>1 && isLeapYear()) ? 1 : 0), 3),
'%k': nHour,
'%l': (nHour+11)%12 + 1,
'%m': zeroPad(nMonth + 1, 2),
'%n': nMonth + 1,
'%M': zeroPad(date.getMinutes(), 2),
'%p': (nHour<12) ? 'AM' : 'PM',
'%P': (nHour<12) ? 'am' : 'pm',
'%s': Math.round(date.getTime()/1000),
'%S': zeroPad(date.getSeconds(), 2),
'%u': nDay || 7,
'%V': (function() {
var target = getThursday(),
n1stThu = target.valueOf();
target.setMonth(0, 1);
var nJan1 = target.getDay();
if (nJan1!==4) target.setMonth(0, 1 + ((4-nJan1)+7)%7);
return zeroPad(1 + Math.ceil((n1stThu-target)/604800000), 2);
})(),
'%w': nDay,
'%x': date.toLocaleDateString(),
'%X': date.toLocaleTimeString(),
'%y': (nYear + '').slice(2),
'%Y': nYear,
'%z': date.toTimeString().replace(/.+GMT([+-]\d+).+/, '$1'),
'%Z': date.toTimeString().replace(/.+\((.+?)\)$/, '$1')
}[sMatch] || '') + '') || sMatch;
});
};
EnhancedDateFormatter.PreferencesManager = function() {
var startPoint="extensions.enhancedDateFormatter.";
var pref=Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefService).
getBranch(startPoint);
var observers={};
// whether a preference exists
this.exists=function(prefName) {
return pref.getPrefType(prefName) != 0;
}
// returns the named preference, or defaultValue if it does not exist
this.getValue=function(prefName, defaultValue) {
var prefType=pref.getPrefType(prefName);
// underlying preferences object throws an exception if pref doesn't exist
if (prefType==pref.PREF_INVALID) {
return defaultValue;
}
switch (prefType) {
case pref.PREF_STRING: return pref.getCharPref(prefName);
case pref.PREF_BOOL: return pref.getBoolPref(prefName);
case pref.PREF_INT: return pref.getIntPref(prefName);
}
}
// sets the named preference to the specified value. values must be strings,
// booleans, or integers.
this.setValue=function(prefName, value) {
var prefType=typeof(value);
switch (prefType) {
case "string":
case "boolean":
break;
case "number":
if (value % 1 != 0) {
throw new Error("Cannot set preference to non integral number");
}
break;
default:
throw new Error("Cannot set preference with datatype: " + prefType);
}
// underlying preferences object throws an exception if new pref has a
// different type than old one. i think we should not do this, so delete
// old pref first if this is the case.
if (this.exists(prefName) && prefType != typeof(this.getValue(prefName))) {
this.remove(prefName);
}
// set new value using correct method
switch (prefType) {
case "string": pref.setCharPref(prefName, value); break;
case "boolean": pref.setBoolPref(prefName, value); break;
case "number": pref.setIntPref(prefName, Math.floor(value)); break;
}
}
// deletes the named preference or subtree
this.remove=function(prefName) {
pref.deleteBranch(prefName);
}
// call a function whenever the named preference subtree changes
this.watch=function(prefName, watcher) {
// construct an observer
var observer={
observe:function(subject, topic, prefName) {
watcher(prefName);
}
};
// store the observer in case we need to remove it later
observers[watcher]=observer;
pref.QueryInterface(Components.interfaces.nsIPrefBranchInternal).
addObserver(prefName, observer, false);
}
// stop watching
this.unwatch=function(prefName, watcher) {
if (observers[watcher]) {
pref.QueryInterface(Components.interfaces.nsIPrefBranchInternal)
.removeObserver(prefName, observers[watcher]);
}
}
}
EnhancedDateFormatter.preferences = new EnhancedDateFormatter.PreferencesManager();