3 Jappix - An open social platform
4 These are the date related JS scripts for Jappix
6 -------------------------------------------------
10 Last revision: 17/08/11
14 // Gets a stamp from a date
15 function extractStamp(date) {
16 return Math.round(date.getTime() / 1000);
19 // Gets the time from a date
20 function extractTime(date) {
21 return date.toLocaleTimeString();
24 // Gets the actual date stamp
25 function getTimeStamp() {
26 return extractStamp(new Date());
29 // Gets the last user activity in seconds
30 var LAST_ACTIVITY = 0;
32 function getLastActivity() {
33 // Last activity not yet initialized?
34 if(LAST_ACTIVITY == 0)
37 return getTimeStamp() - LAST_ACTIVITY;
40 // Gets the last user available presence in seconds
41 var PRESENCE_LAST_ACTIVITY = 0;
43 function getPresenceLast() {
44 // Last presence stamp not yet initialized?
45 if(PRESENCE_LAST_ACTIVITY == 0)
48 return getTimeStamp() - PRESENCE_LAST_ACTIVITY;
51 // Generates the time for XMPP
52 function getXMPPTime(location) {
53 /* FROM : http://trac.jwchat.org/jsjac/browser/branches/jsjac_1.0/jsextras.js?rev=221 */
56 var jInit = new Date();
57 var year, month, day, hours, minutes, seconds;
60 if(location == 'utc') {
61 year = jInit.getUTCFullYear();
62 month = jInit.getUTCMonth();
63 day = jInit.getUTCDate();
64 hours = jInit.getUTCHours();
65 minutes = jInit.getUTCMinutes();
66 seconds = jInit.getUTCSeconds();
69 // Gets the local date
71 year = jInit.getFullYear();
72 month = jInit.getMonth();
73 day = jInit.getDate();
74 hours = jInit.getHours();
75 minutes = jInit.getMinutes();
76 seconds = jInit.getSeconds();
79 // Generates the date string
80 var jDate = year + '-';
81 jDate += padZero(month + 1) + '-';
82 jDate += padZero(day) + 'T';
83 jDate += padZero(hours) + ':';
84 jDate += padZero(minutes) + ':';
85 jDate += padZero(seconds) + 'Z';
87 // Returns the date string
91 // Generates then human time
92 function getCompleteTime() {
93 var init = new Date();
94 var time = padZero(init.getHours()) + ':';
95 time += padZero(init.getMinutes()) + ':';
96 time += padZero(init.getSeconds());
101 // Gets the TZO of a date
102 function getDateTZO() {
104 var date = new Date();
105 var offset = date.getTimezoneOffset();
112 // Process a neutral offset
114 offset = offset * -1;
119 var n_date = new Date(offset * 60 * 1000);
120 hours = n_date.getHours() - 1;
121 minutes = n_date.getMinutes();
124 tzo = sign + padZero(hours) + ':' + padZero(minutes);
126 // Return the processed value
130 // Parses a XMPP date (yyyy-mm-dd, hh-mm-ss) into an human-readable one
131 function parseDate(to_parse) {
132 var date = Date.jab2date(to_parse);
133 var parsed = date.toLocaleDateString() + ' (' + date.toLocaleTimeString() + ')';
138 // Parses a XMPP date (yyyy-mm-dd) into an human-readable one
139 function parseDay(to_parse) {
140 var date = Date.jab2date(to_parse);
141 var parsed = date.toLocaleDateString();
146 // Parses a XMPP date (hh-mm-ss) into an human-readable one
147 function parseTime(to_parse) {
148 var date = Date.jab2date(to_parse);
149 var parsed = date.toLocaleTimeString();
154 // Parses a XMPP date stamp into a relative one
155 function relativeDate(to_parse) {
156 // Get the current date
157 var current_date = Date.jab2date(getXMPPTime('utc'));
158 var current_day = current_date.getDate();
159 var current_stamp = current_date.getTime();
161 // Parse the given date
162 var old_date = Date.jab2date(to_parse);
163 var old_day = old_date.getDate();
164 var old_stamp = old_date.getTime();
165 var old_time = old_date.toLocaleTimeString();
167 // Get the day number between the two dates
168 var days = Math.round((current_stamp - old_stamp) / 86400000);
171 if(isNaN(old_stamp) || isNaN(days))
172 return getCompleteTime();
175 if(current_day == old_day)
180 return _e("Yesterday") + ' - ' + old_time;
182 // Is it less than a week ago?
184 return printf(_e("%s days ago"), days) + ' - ' + old_time;
186 // Another longer period
187 return old_date.toLocaleDateString() + ' - ' + old_time;
190 // Reads a message delay
191 function readMessageDelay(node) {
197 d_delay = jQuery(node).find('delay[xmlns=' + NS_URN_DELAY + ']:first').attr('stamp');
199 // New delay (valid XEP)
203 // Old delay (obsolete XEP!)
205 // Try to read the old-school delay
206 var x_delay = jQuery(node).find('x[xmlns=' + NS_DELAY + ']:first').attr('stamp');
209 delay = x_delay.replace(/^(\w{4})(\w{2})(\w{2})T(\w{2}):(\w{2}):(\w{2})Z?(\S+)?/, '$1-$2-$3T$4:$5:$6Z$7');