Editer le texte de cette page (date de la dernière modification: 7 Avril, 2007 15:05 (diff))
comme cela est utilisé dans la Tweetbar
function relative_time(time_value) { var parsed_date = Date.parse(time_value); var relative_to = (arguments.length > 1) ? arguments[1] : new Date(); var delta = parseInt((relative_to.getTime() - parsed_date) / 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'; } }
# This code uses DateTime as that's what I use. use DateTime::Format::HTTP; sub relative_time { my $time_value = shift; my $relative_to = shift || DateTime->now()->epoch; $relative_to = $relative_to->epoch if ref($relative_to) =~ /DateTime/; my $parsed_date = DateTime::Format::HTTP->parse_datetime($time_value)->epoch; my $delta = $relative_to - $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'; } }
Oui, je sais la partie d'enseignement est plus longue que le code. Mais c'est une bonne chose
from time import strptime, mktime, localtime _partialMinute = 45 * 60 _partialHour = 90 * 60 _fullDay = 24 * 60 * 60 _twoDays = _fullDay * 2 def relative_time(dateString, currentTime=None): """ Take the created_at timestamp string and convert it to a relative text expression >>> now = mktime((2007, 3, 20, 11, 00, 00, 1, 80, 1)) >>> relative_time('Tue Mar 20 10:59:30 +0000 2007', currentTime=now) 'less than a minute ago' >>> relative_time('Tue Mar 20 10:58:30 +0000 2007', currentTime=now) 'about a minute ago' >>> relative_time('Tue Mar 20 10:30:00 +0000 2007', currentTime=now) '30 minutes ago' >>> relative_time('Tue Mar 20 10:00:30 +0000 2007', currentTime=now) 'about an hour ago' >>> relative_time('Tue Mar 20 08:00:00 +0000 2007', currentTime=now) 'about 3 hours ago' >>> relative_time('Wed Mar 19 10:00:00 +0000 2007', currentTime=now) '1 day ago' >>> relative_time('Wed Mar 18 10:00:00 +0000 2007', currentTime=now) '2 days ago' """ if currentTime is not None: now = currentTime else: now = mktime(localtime()) date = dateString.replace(' +0000', '') parsed = mktime(strptime(date, '%a %b %d %H:%M:%S %Y')) 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) if __name__ == "__main__": import doctest doctest.testmod()
/** * 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])); } /** * 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.getTime()) / 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; } }
comme utilisé dans Twitterlicious
private String GetRelativeDate(DateTime date) { DateTime now = DateTime.Now; TimeSpan span = now - date; if (span <= TimeSpan.FromSeconds(60)) { return span.Seconds + " seconds ago"; } else if (span <= TimeSpan.FromMinutes(60)) { if (span.Minutes > 1) { return "about " + span.Minutes + " minutes ago"; } else { return "about a minute ago"; } } else if (span <= TimeSpan.FromHours(24)) { if (span.Hours > 1) { return "about " + span.Hours + " hours ago"; } else { return "about an hour ago"; } } else { if (span.Days > 1) { return "about " + span.Days + "days ago"; } else { return "about a day ago"; } } }
|
Les pages d'intérêt public
Accueil
A propos de Twitter ?
Célébrités Mineures
Chats
Commentaires des Clients
Evénements
FAQ
Les Fans
Les Faux
Pas des humains
Optimisons Twitter
Podcasteurs
Pourquoi ce site ?
Presse et média
Services
Twitter en bref
Brainstorming
Utilité de Twitter ?
Le coin des geeks
Docs API non officiels
script temps relatif
Design
animation: xtof
gabarit laurent
hébergement helmut sur son moulin ProWiki