Editing Twitter relative time parsing This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script> <script> var tweets; $.getJSON('http://twitter.com/statuses/user_timeline/TheOnion.json?count=100&callback=?', function(data, textStatus, jqXHR) { tweets = data; }); // https://gist.github.com/1123968 function timetowords(timestamp) { var words = { "seconds": "less than a minute", "minute": "about a minute", "minutes": "%d minutes", "hour": "about an hour", "hours": "about %d hours", "day": "a day", "days": "%d days", "month": "about a month", "months": "%d months", "year": "about a year", "years": "%d years" }, deltaMilliseconds = (new Date().getTime() - timestamp), seconds = deltaMilliseconds / 1000, minutes = seconds / 60, hours = minutes / 60, days = hours / 24, years = days / 365; return seconds < 45 && words.seconds.replace(/%d/i, Math.round(seconds)) || seconds < 90 && words.minute.replace(/%d/i, 1) || minutes < 45 && words.minutes.replace(/%d/i, Math.round(minutes)) || minutes < 90 && words.hour.replace(/%d/i, 1) || hours < 24 && words.hours.replace(/%d/i, Math.round(hours)) || hours < 48 && words.day.replace(/%d/i, 1) || days < 30 && words.days.replace(/%d/i, Math.floor(days)) || days < 60 && words.month.replace(/%d/i, 1) || days < 365 && words.months.replace(/%d/i, Math.floor(days / 30)) || years < 2 && words.year.replace(/%d/i, 1) || words.years.replace(/%d/i, Math.floor(years)); } // Part of https://github.com/seaofclouds/tweet function relative_time(date) { var relative_to = new Date(); var delta = parseInt((relative_to.getTime() - date) / 1000, 10); var r = ''; if (delta < 60) { r = delta + ' seconds ago'; } else if (delta < 120) { r = 'a minute ago'; } else if (delta < (45 * 60)) { r = (parseInt(delta / 60, 10)).toString() + ' minutes ago'; } else if (delta < (2 * 60 * 60)) { r = 'an hour ago'; } else if (delta < (24 * 60 * 60)) { r = '' + (parseInt(delta / 3600, 10)).toString() + ' hours ago'; } else if (delta < (48 * 60 * 60)) { r = 'a day ago'; } else { r = (parseInt(delta / 86400, 10)).toString() + ' days ago'; } return 'about ' + r; } </script> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code $.each(tweets, function(i, tweet) { timetowords(Date.parse(tweet.created_at)); }); Test 2 Title Async (check if this is an asynchronous test) Code $.each(tweets, function(i, tweet) { relative_time(Date.parse(tweet.created_at)); });