We will be removing relative_created_at in seven days (Tuesday, April
27th). Please change your code accordingly.
Thanks, and do let us know if we can do anything date/time related to
make parsing easier for you.
--
Alex Payne
Obvious
http://twitter.com/al3x
--
Alex
hi all,
i hacked up this javascript "relative time" generator yesterday while
putting together the tweetbar... it's based on the time distance code
in Rails.
feel free to use it however you see fit:
function relative_time(time_value) {
var parsed_date = (new Date).setTime(Date.parse(time_value));
var delta = parseInt(((new Date).getTime() -
parsed_date.getTime()) / 1000);
if(delta < 60) {
return 'less than a minute ago';
} else if(delta < 120) {
return 'about a minute ago';
} else if(delta < (45*60)) {
return (parseInt(delta / 60)).toString() + ' minutes ago';
} else if(delta < (90*60)) {
return 'about an hour ago';
} else if(delta < (24*60*60)) {
return 'about ' + (parseInt(delta / 3600)).toString() + '
hours ago';
} else if(delta < (48*60*60)) {
return '1 day ago';
} else {
return (parseInt(delta / 86400)).toString() + ' days ago';
}
}
::md
Thanks mike .. that's excellent.
Below I've re-written the same logic in perl for those of us using perl.
Cheers!
Rick Measham
# This code uses DateTime as that's what I use.
use DateTime::Format::HTTP;
sub relative_time {
my $time_value = shift;
my $parsed_date =
DateTime::Format::HTTP->parse_datetime($time_value)->epoch;
my $now = DateTime->now()->epoch;
my $delta = $now - $parsed_date;
if($delta < 60) {
return 'less than a minute ago';
} elsif($delta < 120) {
return 'about a minute ago';
} elsif($delta < (45*60)) {
return int($delta / 60) . ' minutes ago';
} elsif($delta < (90*60)) {
return 'about an hour ago';
} elsif($delta < (24*60*60)) {
return 'about ' + int($delta / 3600) + '
hours ago';
} elsif($delta < (48*60*60)) {
return '1 day ago';
} else {
return int($delta / 86400) + ' days ago';
}
}
and here it is in python :) -- the only thing I hacked in was to
remove the +0000 from the string as I believe the time is returned to
us as localtime. If it's not then switch localtime() with gmtime()
below :)
from time import strptime, mktime, localtime
_partialMinute = 45 * 60
_partialHour = 90 * 60
_fullDay = 24 * 60 * 60
_twoDays = _fullDay * 2
def relative_time(dateString):
"""
Take the created_at timestamp string and convert
it to a relative text expression
"""
date = dateString.replace(' +0000', '')
parsed = mktime(strptime(date, '%a %b %d %H:%M:%S %Y'))
now = mktime(localtime())
delta = now - parsed
if delta < 60:
return 'less than a minute ago'
elif delta < 120:
return 'about a minute ago'
elif delta < _partialMinute:
return '%d minutes ago' % (delta / 60)
elif delta < _partialHour:
return 'about an hour ago'
elif delta < _fullDay:
return 'about %d hours ago' % (delta / 3600)
elif delta < _twoDays:
return '1 day ago'
else:
return '%d days ago' % (delta / 86400)
print relative_time('Wed Mar 07 09:32:51 +0000 2007')
print relative_time('Tue Mar 20 20:08:00 +0000 2007')
https://twitter.pbwiki.com/RelativeTimeScripts
Chris
--
Chris Messina
Citizen Provocateur &
Open Source Ambassador-at-Large
Work: http://citizenagency.com
Blog: http://factoryjoe.com/blog
Cell: 412 225-1051
Skype: factoryjoe
This email is: [ ] bloggable [X] ask first [ ] private
I've just edited the Python version to include some basic tests.
I don't read python too well, but looks good. The time for comparison is
a good idea .. I've added it to the other two examples we have there.
Cheers!
Rick Measham
I've also broken the date conversion out into a seperate utility
function. Also changed elseif's to a switch just because im anal and
like the cleanliness of a switch =).
/**
* getDateFromString()
* @param str String - date in Thu Jul 8 12:48:23 GMT+0800 2004
format
* @return date - date in flash format
*/
function getDateFromString(str:String):Date
{
var m = {Jan:0, Feb:1, Mar:2, Apr:3, May:4, Jun:5, Jul:6, Aug:
7, Sep:8, Oct:9, Nov:10, Dec:11};
var dArr:Array = str.split(' ');
var tArr:Array = dArr[3].split(':');
return new Date(Date.UTC(dArr[5], m[dArr[1]], dArr[2], tArr[0],
tArr[1], tArr[2]));
//return new Date(Date.UTC(Number(dArr[5]), m[dArr[1]],
Number(dArr[2]), (Number(tArr[0])-Number(dArr[4].substring(3, 6))),
(Number(tArr[1])-Number(dArr[4].substring(6, 8))), Number(tArr[2])));
}
/**
* relative_time()
* @param time_value - date in Thu Jul 8 12:48:23 GMT+0800 2004
format
* @return string
*/
function relative_time(time_value):String
{
var parsed_date:Date = getDateFromString(time_value);
var relative_to:Date = new Date();
var dateDiff:Number = (relative_to.getTime() - parsed_date) / 1000;
switch(true){
case dateDiff < 60:
return 'about a minute ago';
break;
case dateDiff < 120:
return 'about a minute ago';
break;
case dateDiff < (45*60):
return int(dateDiff / 60) + ' minutes ago';
break;
case dateDiff < (90*60):
return 'about an hour ago';
break;
case dateDiff < (24*60*60):
return 'about ' + int(dateDiff / 3600) + ' hours ago';
break;
case dateDiff < (48*60*60):
return '1 day ago';
break;
default:
return int(dateDiff / 86400) + ' days ago';
break;
}
}
On Mar 21, 4:09 pm, "DeWitt Clinton" <dclin...@gmail.com> wrote:
> Hi all,
>
> I updated the python-twitter library to automatically generate a value for
> relative_created_at value. The change should be transparent for existing
> users of the library.
>
> http://code.google.com/p/python-twitter/
>
> Also, I noticed that there are inconsistencies in the styles and formats
> people are using to generate the relative created at string. Since I
> haven't found a well-defined set of rules, I made up some of my own. When
> Twitter publishes something official then I will update the library to
> conform.
>
> The new version is available in the subversion trunk, and I'd still love
> someone to try it out and let me know if I missed anything. I'm holding off
> on a full release until I find out if there are any more API changes I
> should incorporate.
>
> Thanks!
>
> -DeWitt
>
> > This email is: [ ] bloggable [X] ask first [ ] private- Hide quoted text -
>
> - Show quoted text -
> > - Show quoted text -- Hide quoted text -
Thanks,
Jesse
On Mar 25, 2:37 pm, "Chris Messina" <chris.mess...@gmail.com> wrote:
> Can you proof this?
>
> http://twitter.pbwiki.com/RelativeTimeScripts#FlashActionscript
>
> Chris
>
The password is on the front page of the wiki.
> I've also broken the date conversion out into a seperate utility
> function.
I would have thought that as ActionScript is an ECMA script, the
Date.parse() would have worked. However from reading the docs, you
should be able to just give the HTTP format date (based on that in
RFC822) to the Date constructor:
var parsed_date:Date = new Date(time_value);
If not that, there appears to be a parseRFC822() method.
I also note that in your delta (dateDiff) algorithm you convert one Date
object into miliseconds but not the other. Of course it will still work
as applying a math function to a Date object will use the getTime()
result. But it would still be good to do one or the other to both dates.
Cheers!
Rick Measham
In AS2 I dont belive the Date constructor can accept the a date in a
"string" format. It's pretty specific in its format. Also flash starts
its month array with 0 so you have to have that offset in the numbers
there as well. Perhapse they've added the RFC822 into the contstructor
in AS3, one can hope.
Date constructor in AS2
public Date([yearOrTimevalue:Number], [month:Number], [date:Number],
[hour:Number], [minute:Number], [second:Number], [millisecond:Number])
Also thanks ill find the password and fix the typo mentioned above.
Jesse
I try to join the wiki with twitter and sweety and it says it does not
exist.
I try to edit the page and enter the password on the page sweety and
it says this wiki does not use that password =(
Jesse
> > Rick Measham- Hide quoted text -
Probably need to get Mr.Messina to look at that
Other than the deprecation of relative_created_at, there's nothing in
the API changes that's going to negatively impact existing
applications. Everything that's been added lately is all convenience
methods. I still intend to put up official docs. I'll try to knock
those out today.
--
Alex
One problem I'm having is that my relative date I have to compare it
against (my system clock) isn't the same as the one used on the site.
For example when I make an update at 9PM, it appears on the site as
8PM, so it's always an hour out.
This could be due to the fact that us in the UK have recently had our
daylight saving, switching to GMT+1 now. Is there anyway for Twitter
to know that we're now in BST (British Summer time) instead of GMT?
> This email is: [ ] bloggable [X] ask first [ ] private- Hide quoted text -
On Apr 2, 8:56 pm, "Blaine Cook" <rom...@gmail.com> wrote:
> We'll always provide our timestamps in UTC; you should convert that into
> whatever timezone is appropriate for your users.
>
> cheers!
>
> Blaine
> It's Obvious -http://twitter.com/blaine
>