Skip to content

Commit

Permalink
Added support for future timestamps ... "from now"
Browse files Browse the repository at this point in the history
  • Loading branch information
rmm5t committed Aug 6, 2008
1 parent 38c11a1 commit 1f7b391
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 6 additions & 0 deletions index.html
Expand Up @@ -109,6 +109,12 @@ <h3>How?</h3>
jQuery.timeago("2008-07-17"); //=> &quot;<span id="prog_string"></span>&quot;
jQuery.timeago(jQuery("abbr#some_id")); //=> &quot;<span id="prog_element"></span>&quot; // [title=&quot;2008-07-20&quot;]</pre>

<p class="how">
To support timestamps in the future, use the <tt>allowFuture</tt> setting:
</p>
<pre>
jQuery.timeago.settings.allowFuture = true;</pre>

<h3>Where?</h3>
<p><a href="jquery.timeago.js">Download the &quot;stable&quot; release</a>.</p>
<p>
Expand Down
13 changes: 10 additions & 3 deletions jquery.timeago.js
@@ -1,5 +1,5 @@
/*
* timeago: a jQuery plugin, version: 0.3.2.99 (07/30/2008)
* timeago: a jQuery plugin, version: 0.4 (08/05/2008)
* @requires jQuery v1.2 or later
*
* Timeago is a jQuery plugin that makes it easy to support automatically
Expand All @@ -22,9 +22,16 @@

$.extend($.timeago, {
settings: {
refreshMillis: 60000
refreshMillis: 60000,
allowFuture: false
},
inWords: function(distanceMillis) {
var suffix = " ago";
if (this.settings.allowFuture) {
if (distanceMillis < 0) suffix = " from now";
distanceMillis = Math.abs(distanceMillis);
}

var seconds = distanceMillis / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
Expand All @@ -43,7 +50,7 @@
years < 2 && "about a year" ||
Math.floor(years) + " years";

return words + " ago";
return words + suffix;
},
parse: function(iso8601) {
var s = $.trim(iso8601);
Expand Down
2 changes: 2 additions & 0 deletions test.html
Expand Up @@ -8,6 +8,7 @@
<script src="jquery.timeago.js" type="text/javascript"></script>
<script src="test.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery.timeago.settings.allowFuture = true;
jQuery(document).ready(function($) {
// functional tests
$('abbr[class*=timeago]').timeago();
Expand Down Expand Up @@ -70,6 +71,7 @@ <h1>Parsing unit tests:</h1>
<abbr class="todate" title="1978-12-19T02:17:00+0900"></abbr> [from +0900]<br />

<h1>Wording unit tests:</h1>
<abbr class="towords" title="-7200"></abbr> [-120 min]<br />
<abbr class="towords" title="-60"></abbr> [-60 sec]<br />
<abbr class="towords" title="-30"></abbr> [-30 sec]<br />
<abbr class="towords" title="-1"></abbr> [-1 sec]<br />
Expand Down

0 comments on commit 1f7b391

Please sign in to comment.