]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/date.js
Merge pull request #215 from tobiasd/fix_1180
[friendica-addons.git] / jappixmini / jappix / js / date.js
1 /*
2
3 Jappix - An open social platform
4 These are the date related JS scripts for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 17/08/11
11
12 */
13
14 // Gets a stamp from a date
15 function extractStamp(date) {
16         return Math.round(date.getTime() / 1000);
17 }
18
19 // Gets the time from a date
20 function extractTime(date) {
21         return date.toLocaleTimeString();
22 }
23
24 // Gets the actual date stamp
25 function getTimeStamp() {
26         return extractStamp(new Date());
27 }
28
29 // Gets the last user activity in seconds
30 var LAST_ACTIVITY = 0;
31
32 function getLastActivity() {
33         // Last activity not yet initialized?
34         if(LAST_ACTIVITY == 0)
35                 return 0;
36         
37         return getTimeStamp() - LAST_ACTIVITY;
38 }
39
40 // Gets the last user available presence in seconds
41 var PRESENCE_LAST_ACTIVITY = 0;
42
43 function getPresenceLast() {
44         // Last presence stamp not yet initialized?
45         if(PRESENCE_LAST_ACTIVITY == 0)
46                 return 0;
47         
48         return getTimeStamp() - PRESENCE_LAST_ACTIVITY;
49 }
50
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 */
54         
55         // Initialize
56         var jInit = new Date();
57         var year, month, day, hours, minutes, seconds;
58         
59         // Gets the UTC date
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();
67         }
68         
69         // Gets the local date
70         else {
71                 year = jInit.getFullYear();
72                 month = jInit.getMonth();
73                 day = jInit.getDate();
74                 hours = jInit.getHours();
75                 minutes = jInit.getMinutes();
76                 seconds = jInit.getSeconds();
77         }
78         
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';
86         
87         // Returns the date string
88         return jDate;
89 }
90
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());
97         
98         return time;
99 }
100
101 // Gets the TZO of a date
102 function getDateTZO() {
103         // Get the date
104         var date = new Date();
105         var offset = date.getTimezoneOffset();
106         
107         // Default vars
108         var sign = '';
109         var hours = 0;
110         var minutes = 0;
111         
112         // Process a neutral offset
113         if(offset < 0) {
114                 offset = offset * -1;
115                 sign = '+';
116         }
117         
118         // Get the values
119         var n_date = new Date(offset * 60 * 1000);
120         hours = n_date.getHours() - 1;
121         minutes = n_date.getMinutes();
122         
123         // Process the TZO
124         tzo = sign + padZero(hours) + ':' + padZero(minutes);
125         
126         // Return the processed value
127         return tzo;
128 }
129
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() + ')';
134         
135         return parsed; 
136 }
137
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();
142         
143         return parsed; 
144 }
145
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();
150         
151         return parsed; 
152 }
153
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();
160         
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();
166         
167         // Get the day number between the two dates
168         var days = Math.round((current_stamp - old_stamp) / 86400000);
169         
170         // Invalid date?
171         if(isNaN(old_stamp) || isNaN(days))
172                 return getCompleteTime();
173         
174         // Is it today?
175         if(current_day == old_day)
176                 return old_time;
177         
178         // It is yesterday?
179         if(days <= 1)
180                 return _e("Yesterday") + ' - ' + old_time;
181         
182         // Is it less than a week ago?
183         if(days <= 7)
184                 return printf(_e("%s days ago"), days) + ' - ' + old_time;
185         
186         // Another longer period
187         return old_date.toLocaleDateString() + ' - ' + old_time;
188 }
189
190 // Reads a message delay
191 function readMessageDelay(node) {
192     try {
193         // Initialize
194         var delay, d_delay;
195         
196         // Read the delay
197         d_delay = jQuery(node).find('delay[xmlns=' + NS_URN_DELAY + ']:first').attr('stamp');
198         
199         // New delay (valid XEP)
200         if(d_delay)
201                 delay = d_delay;
202         
203         // Old delay (obsolete XEP!)
204         else {
205                 // Try to read the old-school delay
206                 var x_delay = jQuery(node).find('x[xmlns=' + NS_DELAY + ']:first').attr('stamp');
207                 
208                 if(x_delay)
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');
210         }
211         
212         return delay;
213     } catch(e) {}
214 }