Remoto - VFS: date.js Source File
Remoto - VFS
date.js
Go to the documentation of this file.
1 
2 define( [
3  'remoto!stdlib:js/include/utils.js',
4  ],
5  function(utils)
6  {
7  'use strict';
8 
35  function date(year, month, day, hours, minutes, seconds, milliseconds)
36  {
37  var _date;
38 
39  if (arguments.length > 1)
40  _date = new Date(year, month, day, hours, minutes, seconds, milliseconds);
41  else if (arguments.length == 1)
42  { if (arguments[0] instanceof Date)
43  _date = new Date(arguments[0]);
44  else
45  { _date = new Date();
46  _date.setTime(arguments[0]);
47  //console.log(_date);
48  }
49  }
50  else
51  _date = new Date();
52 
53  _date.setMilliseconds(0); //our dates only have a resolution of 1 second.
54  _date.setSeconds(0); //in fact, of only one minute.
55 
56  this._date = _date;
57 
58  //console.log(_date);
59 
60  /*
61  var THIS = this;
62  var v = function()
63  {
64  return THIS.toString();
65  }
66 
67  this.value = v.bind(this);
68  */
69  }
70 
71  date.wkdays = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ];
72  date.weekdays = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];
73  date.months = [ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" ];
74  date.monthsFull = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
75 
76  date.msMinute = 1000*60; //milliseconds in a minute
77  date.msHour = 1000*60*60; //milliseconds in an hour
78  date.msDay = 1000*60*60*24; //milliseconds in a day
79  date.ms15 = 1000*60*15; //milliseconds in 15 minutes
80  date.msWeek = 1000*60*60*24*7; //milliseconds in a week
81 
82  //FIXME: offset should be used in certain calculations, but which!
83  date.offset = 0; //number of milliseconds of offset between the server and the local machine.
84 
85  date.spad = function(s,l)
86  {
87  l = l || 6;
88  s = "000000"+s;
89  s = s.substr(s.length-l,l);
90  return s;
91  };
92 
93  date.prototype = {
94 
95  /*
96  value: function()
97  {
98  return this.toString();
99  },
100  */
101 
102  firstDayOfWeek: function()
103  {
104  var d = new Date( this._date );
105  var day = d.getDay();
106  var diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
107  d.setDate(diff);
108  d.setHours(0);
109  d.setMinutes(0);
110  d.setSeconds(0);
111  d.setMilliseconds(0);
112  return new date(d);
113  },
114 
115  lastDayOfWeek: function()
116  {
117  var d = new Date( this._date );
118  var day = d.getDay();
119  var diff = d.getDate() - day + (day == 0 ? 0:7);
120  d.setDate(diff);
121  d.setHours(0);
122  d.setMinutes(0);
123  d.setSeconds(0);
124  d.setMilliseconds(0);
125  return new date(d);
126  },
127 
128  fieldString: function(sep) //for safari/browser compatibility, as toLocaleString is not reliable enough to reverse-parse.
129  {
130  sep = sep || "/";
131 
132  var d = this._date;
133  var _st = ""
134  _st += date.spad(d.getMonth()+1,2);
135  _st += sep + date.spad(d.getDate(),2);
136  _st += sep + d.getFullYear();
137  return _st;
138  },
139 
140  timeString: function(use12)
141  {
142  var d = this._date;
143  var vv = "";
144 
145  if (use12)
146  {
147  vv += date.spad(d.getHours()%12,2);
148  vv += ":" + date.spad(d.getMinutes(),2);
149  vv += d.getHours() > 11 ? " pm" : " am";
150  }
151  else
152  {
153  vv += date.spad(d.getHours(),2);
154  vv += ":" + date.spad(d.getMinutes(),2);
155  }
156 
157  return vv;
158  },
159 
160  lengthString: function()
161  {
162  var t = this.time;
163  var m = Math.round( t / (1000*60) );
164  var h = Math.floor( m / 60 );
165  return (h>0 ? h+" hr"+(h>1?"s":"")+", " : "")+(m%60)+" min"+((m%60)>1?"s":"");
166  },
167 
168  lengthString2: function()
169  {
170  var t = this.time;
171  var m = Math.round( t / (1000*60) );
172  var h = Math.floor( m / 60 );
173  return (h>0 ? h : "")+":"+date.spad((m%60),2);
174  },
175 
176  daysHoursMinutesString: function() //from zero, expected to be used in the context of: var h_m = (new date(e.time - s.time)).hoursMinutesString();
177  {
178  var t = this.time;
179  var m = Math.floor( t / (1000*60) );
180  var h = Math.floor( m / 60 );
181  var d = Math.floor( h / 24 );
182  var _s = "";
183  _s += d > 0 ? d +":" : "";
184  _s += date.spad( h % 24, 2 );
185  _s += ":" + date.spad( m % 60, 2 );
186 
187  return _s;
188  },
189 
190  fullString: function(withSeconds)
191  {
192  var d = this._date;
193  var vv = "";
194 
195  vv += date.spad(d.getMonth()+1,2);
196  vv += "/" + date.spad(d.getDate(),2);
197  vv += "/" + d.getFullYear();
198  vv += " " + date.spad(d.getHours(),2);
199  vv += ":" + date.spad(d.getMinutes(),2);
200 
201  if (withSeconds)
202  vv += ":" + date.spad(d.getSeconds(),2);
203  //vv += " " + d.getTimezone();
204 
205  var zone = d.toLocaleTimeString('en-us',{timeZoneName:'short'}).split(' ')[2];
206  //console.log(zone)
207  vv += " "+zone;
208 
209  return vv;
210  },
211 
212  prettyString: function()
213  {
214  //var d = this._date.getDay();
215  var m = this._date.getMonth();
216  //d = date.weekdays[ d ];
217  m = date.months[ m ];
218 
219  return m+" "+this._date.getDate()+", "+this._date.getFullYear();
220  },
221 
222  dayMonthString: function()
223  {
224  return (this._date.getMonth()+1)+"/"+date.spad(this._date.getDate(),2);
225  },
226 
228  // getters/setters
230 
241  get id() {
242  return this._date.valueOf();
243  },
244 
245  /*
246  set id(_id) {
247  console.dir(this);
248  return this.time = _id;
249  },
250  */
251 
258  get year() { return this._date.getFullYear();
259  },
260 
261  get date() { return this._date.getDate();
262  },
263 
264  set date(d) { return this._date.setDate(d);
265  },
266 
267  get monthString() { return date.months[ this._date.getMonth() ];
268  },
269 
270  get monthStringFull() { return date.monthsFull[ this._date.getMonth() ];
271  },
272 
273  get month() { return this._date.getMonth();
274  },
275 
276  set month(m) { return this._date.setMonth(m);
277  },
278 
279  get daysInMonth() { var d = new Date(this._date);
280  d.setMonth( this._date.getMonth()+1 );
281  d.setDate(-1);
282  return d.getDate()+1;
283  },
284 
285  get day() { return this._date.getDay();
286  },
287 
288  get weekday() { return date.wkdays[ this._date.getDay() ];
289  },
290 
291  get yearday() { //return date.
292  },
293 
294  set minutes(m) { this._date.setHours(Math.floor(m/60));
295  this._date.setMinutes(m%60,0,0);
296  },
297 
298  set seconds(s) { this._date.setSeconds(s);
299  },
300 
301  get minutes() { return this._date.getHours()*60+this._date.getMinutes();
302  },
303 
304  get time() { return this._date.getTime();
305  },
306 
307  set time(t) { return this._date.setTime(t);
308  },
309 
312  headerString: function()
313  {
314  var d = this._date.getDay();
315  var m = this._date.getMonth();
316  d = date.weekdays[ d ];
317  m = date.months[ m ];
318 
319  return d+", "+m+" "+this._date.getDate();
320  },
321 
322  daysTo: function(end)
323  {
324  var t = this._date.valueOf();
325  var e = end._date.valueOf();
326  var d = e-t;
327  var days = Math.round(d/(1000*60*60*24));
328  return days;
329  },
330 
331  minutesTo: function(end)
332  {
333  var t = this._date.valueOf();
334  var e = end._date.valueOf();
335  var d = e-t;
336  var minutes = d/(1000*60);
337  return minutes;
338  },
339 
340  /*
341  timeToNextMinute: function()
342  {
343  var d = new date( this._date );
344  d.seconds = 0;
345  d.minutes = d.minutes+1;
346 
347  return d.time - this.time;
348  },
349  */
350 
351  isWeekend: function()
352  {
353  var d = this._date.getDay();
354 
355  return (d==0 || d==6);
356  },
357 
358  nextDay: function()
359  {
360  var d = new Date( this._date );
361  var diff = d.getDate() + 1;
362  d.setDate(diff);
363  d.setHours(0);
364  d.setMinutes(0);
365  d.setSeconds(0);
366  d.setMilliseconds(0);
367  return new date(d);
368  },
369 
370  prevDay: function()
371  {
372  var d = new Date( this._date );
373  var diff = d.getDate() - 1;
374  d.setDate(diff);
375  d.setHours(0);
376  d.setMinutes(0);
377  d.setSeconds(0);
378  d.setMilliseconds(0);
379  return new date(d);
380  },
381 
382  thisDay: function()
383  {
384  var d = new Date( this._date );
385  d.setHours(0);
386  d.setMinutes(0);
387  d.setSeconds(0);
388  d.setMilliseconds(0);
389  return new date(d);
390  },
391 
392  offsetByDays: function(off)
393  {
394  /*
395  var d = this._date.getTime();
396  d += (off * 24 * 60 * 60 * 1000); //offset by a number of days
397  this._date.setTime(d);
398  */
399 
400  var d = this._date.getDate();
401  var h = this._date.getHours();
402  d += off;
403 
404  var t = new Date(this._date);
405  t.setDate(d);
406  t.setHours(h);
407 
408  return new date(t);
409  },
410 
411  startUTC: function()
412  {
413  return this.thisDay()._date.getTime();
414  },
415 
416  endUTC: function()
417  {
418  return this.nextDay().startUTC() - 1;
419  },
420 
421  intersects: function(s,e,l) //if this _date including l minutes is anywhere inside s,e
422  {
423  var S = s._date.getTime() || Date.now(); //the start time that we're intersecting, but zero is considered now()
424  var E = e._date.getTime() || Date.now(); //the end time that we're intersecting, but zero is considered now()
425  var TS = this._date.getTime(); //the time of this, at the start
426  var TE = TS+l; //the time of this, at the end of the range
427 
428  if (E < S) //this can happen, but we leave the S=E case as ok.
429  return false;
430 
431  //three cases:
432  //start is within range
433  //end is witin range
434  //start and end are before and after range
435 
436  if (TS <= S && TE > S)
437  return true;
438 
439  if (TS < E && TE > E)
440  return true;
441 
442  if (TS >= S && TE <= E)
443  return true;
444 
445  return false;
446  },
447 
448  amountWithin: function(s,n,l)
449  {
450  s = utils.bound( this.time, s, this.time + l );
451  n = utils.bound( this.time, n, this.time + l );
452  //console.log(s+" "+n+" "+(n-s));
453 
454  return n-s; //in milliseconds
455  },
456 
457  toString: function()
458  {
459  return this._date.getTime();
460  //return this._date.toString();
461  },
462  };
463 
471  date.timeToNextMinute = function()
472  {
473  var n = new Date();
474  var d = new Date();
475  d.setSeconds(0);
476  d.setMinutes(d.getMinutes()+1);
477  return d.getTime() - n.getTime();
478  }
479 
480  return date;
481  }
482 );
483 
The remoto date object, which has useful features not in the default javascript Date object.
getter id
returns the number of milliseconds since midnight January 1, 1970 UTC
timeToNextMinute
Calculate the time remaining until the next wallclock minute.
getter date
a getter DOCME
Utility functions for javascript clients.
bound(min, n, max)
Return a number where n is bounded by [min,max].
time(value, options)