diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..6848728 --- /dev/null +++ b/Rakefile @@ -0,0 +1,11 @@ +SETTINGS = { + 'rsync_server' => ENV['rsync_server'] || 'timeago@yarp.com:/var/www/timeago/', + 'rsync_options' => ENV['rsync_options'] || '-e ssh -avz --delete --exclude=.git' +} + +desc 'Publishes to server (edit Rakefile to config)' +task :publish do + cmd = "rsync #{SETTINGS['rsync_options']} ./ #{SETTINGS['rsync_server']}" + puts "\nSyncing with server: #{cmd}\n\n" + system(cmd) +end diff --git a/clock.png b/clock.png new file mode 100644 index 0000000..4225cef Binary files /dev/null and b/clock.png differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..8971bda --- /dev/null +++ b/index.html @@ -0,0 +1,155 @@ + + + + + + timeago jQuery plugin + + + + + + + +
+

timeago

+

a jQuery plugin

+ +

What?

+

+ Timeago is a jQuery plugin that makes it easy to support automatically updating fuzzy + timestamps (e.g. "4 minutes ago" or "about 1 day ago"). Download, view + the examples, and enjoy. +

+ +

+ You opened this page sometime before now (turn on javascript, loser). (This will update every minute. Wait for it.) +

+ +

+ This page was last modified sometime before now (turn on javascript, loser). +

+ +

+ Ryan was born Dec 18, 1978. +

+ +

How?

+

+ First, load jQuery and the plugin: +

+ +
+  <script src="jquery.min.js" type="text/javascript"></script>
+  <script src="jquery.timeago.js" type="text/javascript"></script>
+ +

+ Now, let's attach it to your timestamps on DOM ready: +

+ +
+  jQuery(document).ready(function() {
+    jQuery('abbr[class*=timeago]').timeago();
+  });
+ +

+ This will turn all abbr elements with a class + of timeago and an ISO 8601 timestamp in the title: +

+ +
+  <abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>
+ +

+ into something like this: +

+ +
+  <abbr class="timeago" title="2008-07-17T09:24:17Z">6 hours ago</abbr>
+ +

+ As time passes, the timestamps will automatically update. +

+ +

Why?

+

Timeago was originally built for use with Yarp.com (coming soon) to timestamp comments.

+ + +

Who?

+

+ Timeago was built by Ryan McGeary + while standing on the shoulders of giants. John Resig wrote about + a similar + approach. The verbiage was based on + the distance_of_time_in_words ActionView helper + in Ruby on Rails. +

+ +

When?

+

+ Timeago was conceived on July 17, 2008. (Yup, that's powered by timeago too) +

+
+ + + + + + + diff --git a/jquery.timeago.js b/jquery.timeago.js new file mode 100644 index 0000000..6ed7803 --- /dev/null +++ b/jquery.timeago.js @@ -0,0 +1,76 @@ +/* + * Time Ago (for jQuery) version: 0.1 (07/18/2008) + * @requires jQuery v1.2 or later + * + * Timeago is a jQuery plugin that makes it easy to support automatically + * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago"). + * + * For usage and examples, visit: + * http://timeago.yarp.com/ + * + * Licensed under the MIT: + * http://www.opensource.org/licenses/mit-license.php + * + * Copyright (c) 2008, Ryan McGeary (ryanonjavascript -[at]- mcgeary [*dot*] org) + */ +(function($) { + $.timeago = function(timestamp) { + // TODO: should take a Date, ISO8601, or element[title=iso8601] + // return words if date or iso8601; convert and return element if element + alert("jQuery.timeago helper not implemented yet"); + }; + + $.extend($.timeago, { + settings: { + refreshSeconds: 60 + }, + inWords: function(distanceMillis) { + var seconds = distanceMillis / 1000; + var minutes = seconds / 60; + var hours = minutes / 60; + var days = hours / 24; + var years = days / 365; + + var words = seconds < 45 && "less than a minute" || + seconds < 90 && "about 1 minute" || + minutes < 45 && Math.round(minutes) + " minutes" || + minutes < 90 && "about 1 hour" || + hours < 24 && Math.round(hours) + " hours" || + hours < 48 && "about 1 day" || + days < 30 && Math.floor(days) + " days" || + days < 60 && "about 1 month" || + days < 365 && Math.floor(days / 30) + " months" || + years < 2 && "about 1 year" || + Math.floor(years) + " years"; + + return words + " ago"; + }, + parse: function(iso8601) { + var s = iso8601.replace(/^\s+/, '').replace(/\s+$/, ''); + s = s.replace(/-/,"/").replace(/-/,"/"); + s = s.replace(/T/," ").replace(/Z/," UTC"); + s = s.replace(/([\+-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400 + return new Date(s); + } + }); + + $.fn.timeago = function() { + var self = this; + self.each(refresh); + + var $s = $.timeago.settings; + if ($s.refreshSeconds > 0) { + setInterval(function() { self.each(refresh); }, ($s.refreshSeconds * 1000)); + } + }; + + function refresh() { + var date = $.timeago.parse(this.title); + $(this).text($.timeago.inWords(distance(date))); + } + + function distance(date) { + return (new Date().getTime() - date.getTime()); + } +})(jQuery); + diff --git a/test.html b/test.html new file mode 100644 index 0000000..b4464d7 --- /dev/null +++ b/test.html @@ -0,0 +1,44 @@ + + + + + Timeago jQuery Plugin Tests + + + + + +

Functional tests:

+ +

Long term

+ Jett was born bleh [from Z]
+ Jett was born bleh [from -0500]
+
+ Logan was born bleh [from Z]
+ Logan was born bleh [from -0400]
+
+ Ryan was born bleh [from Z]
+ Ryan was born bleh [from -0500]
+ +

Parsing unit tests:

+ Ryan was born...
+ [from Z]
+ [from -00:00]
+ [from -05:00]
+ [from -0500]
+ [from +09:00]
+ [from +0900]
+ +