[ Index ]

PHP Cross Reference of phpwcms V1.4.7 _r403 (01.11.10)

title

Body

[close]

/template/lib/mootools/more/Native/ -> Date.Extras.js (source)

   1  /*
   2  ---
   3  
   4  script: Date.Extras.js
   5  
   6  name: Date.Extras
   7  
   8  description: Extends the Date native object to include extra methods (on top of those in Date.js).
   9  
  10  license: MIT-style license
  11  
  12  authors:
  13    - Aaron Newton
  14    - Scott Kyle
  15  
  16  requires:
  17    - /Date
  18  
  19  provides: [Date.Extras]
  20  
  21  ...
  22  */
  23  
  24  Date.implement({
  25  
  26      timeDiffInWords: function(relative_to){
  27          return Date.distanceOfTimeInWords(this, relative_to || new Date);
  28      },
  29  
  30      timeDiff: function(to, joiner){
  31          if (to == null) to = new Date;
  32          var delta = ((to - this) / 1000).toInt();
  33          if (!delta) return '0s';
  34          
  35          var durations = {s: 60, m: 60, h: 24, d: 365, y: 0};
  36          var duration, vals = [];
  37          
  38          for (var step in durations){
  39              if (!delta) break;
  40              if ((duration = durations[step])){
  41                  vals.unshift((delta % duration) + step);
  42                  delta = (delta / duration).toInt();
  43              } else {
  44                  vals.unshift(delta + step);
  45              }
  46          }
  47          
  48          return vals.join(joiner || ':');
  49      }
  50  
  51  });
  52  
  53  Date.alias('timeDiffInWords', 'timeAgoInWords');
  54  
  55  Date.extend({
  56  
  57      distanceOfTimeInWords: function(from, to){
  58          return Date.getTimePhrase(((to - from) / 1000).toInt());
  59      },
  60  
  61      getTimePhrase: function(delta){
  62          var suffix = (delta < 0) ? 'Until' : 'Ago';
  63          if (delta < 0) delta *= -1;
  64          
  65          var units = {
  66              minute: 60,
  67              hour: 60,
  68              day: 24,
  69              week: 7,
  70              month: 52 / 12,
  71              year: 12,
  72              eon: Infinity
  73          };
  74          
  75          var msg = 'lessThanMinute';
  76          
  77          for (var unit in units){
  78              var interval = units[unit];
  79              if (delta < 1.5 * interval){
  80                  if (delta > 0.75 * interval) msg = unit;
  81                  break;
  82              }
  83              delta /= interval;
  84              msg = unit + 's';
  85          }
  86          
  87          return Date.getMsg(msg + suffix, delta).substitute({delta: delta.round()});
  88      }
  89  
  90  });
  91  
  92  
  93  Date.defineParsers(
  94  
  95      {
  96          // "today", "tomorrow", "yesterday"
  97          re: /^(?:tod|tom|yes)/i,
  98          handler: function(bits){
  99              var d = new Date().clearTime();
 100              switch(bits[0]){
 101                  case 'tom': return d.increment();
 102                  case 'yes': return d.decrement();
 103                  default:     return d;
 104              }
 105          }
 106      },
 107  
 108      {
 109          // "next Wednesday", "last Thursday"
 110          re: /^(next|last) ([a-z]+)$/i,
 111          handler: function(bits){
 112              var d = new Date().clearTime();
 113              var day = d.getDay();
 114              var newDay = Date.parseDay(bits[2], true);
 115              var addDays = newDay - day;
 116              if (newDay <= day) addDays += 7;
 117              if (bits[1] == 'last') addDays -= 7;
 118              return d.set('date', d.getDate() + addDays);
 119          }
 120      }
 121  
 122  );


Generated: Tue Nov 16 22:51:00 2010 Cross-referenced by PHPXref 0.7