From: Ant Zucaro Date: Wed, 1 Jun 2011 14:57:45 +0000 (-0400) Subject: Add fuzzy dates. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=22a187404c2191896208a7cae658315d11e31c0d;p=xonotic%2Fxonstat.git Add fuzzy dates. --- diff --git a/xonstat/models.py b/xonstat/models.py index bcebb9c..7945362 100755 --- a/xonstat/models.py +++ b/xonstat/models.py @@ -3,7 +3,7 @@ from sqlalchemy.orm import mapper from sqlalchemy.orm import scoped_session from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base -from xonstat.util import strip_colors, html_colors +from xonstat.util import strip_colors, html_colors, pretty_date DBSession = scoped_session(sessionmaker()) Base = declarative_base() @@ -72,6 +72,9 @@ class Game(object): return "" % (self.game_id, self.start_dt, self.game_type_cd, self.server_id) + def fuzzy_date(self): + return pretty_date(self.start_dt) + class PlayerGameStat(object): def __init__(self, create_dt=None): diff --git a/xonstat/templates/player_info.mako b/xonstat/templates/player_info.mako index 6982911..ad29f12 100755 --- a/xonstat/templates/player_info.mako +++ b/xonstat/templates/player_info.mako @@ -68,23 +68,16 @@ ${accuracy(weapon_stats)} % endif -##### RECENT GAMES ##### +##### RECENT GAMES (v2) #### % if recent_games:

Recent Games

-% for (gamestat, game, server, map) in recent_games: - #${game.game_id}: ${map.name} on ${server.name} -
-% endfor -More games played by ${player.nick_html_colors()}... -% endif - -##### RECENT GAMES (v2) #### - + + % for (gamestat, game, server, map) in recent_games: @@ -97,6 +90,10 @@ ${accuracy(weapon_stats)} Loss % endif - + + % endfor +
Game Type Map Result PlayedPermalink
${game.start_dt}${game.fuzzy_date()}View
+More games played by ${player.nick_html_colors()}... +% endif diff --git a/xonstat/util.py b/xonstat/util.py old mode 100644 new mode 100755 index 1b54426..e9d179a --- a/xonstat/util.py +++ b/xonstat/util.py @@ -1,10 +1,12 @@ import re +from datetime import datetime def strip_colors(str=None): str = re.sub(r'\^x\w\w\w', '', str) str = re.sub(r'\^\d', '', str) return str + def html_colors(str=None): orig = str str = re.sub(r'\^x(\w)(\w)(\w)', @@ -25,5 +27,59 @@ def html_colors(str=None): return str + def page_url(page): return current_route_url(request, page=page, _query=request.GET) + + +def pretty_date(time=False): + """ + Get a datetime object or a int() Epoch timestamp and return a + pretty string like 'an hour ago', 'Yesterday', '3 months ago', + 'just now', etc + """ + now = datetime.now() + if type(time) is int: + diff = now - datetime.fromtimestamp(time) + elif isinstance(time,datetime): + diff = now - time + elif not time: + diff = now - now + second_diff = diff.seconds + day_diff = diff.days + + if day_diff < 0: + return '' + + if day_diff == 0: + if second_diff < 10: + return "just now" + if second_diff < 60: + return str(second_diff) + " seconds ago" + if second_diff < 120: + return "a minute ago" + if second_diff < 3600: + return str( second_diff / 60 ) + " minutes ago" + if second_diff < 7200: + return "an hour ago" + if second_diff < 86400: + return str( second_diff / 3600 ) + " hours ago" + if day_diff == 1: + return "Yesterday" + if day_diff < 7: + return str(day_diff) + " days ago" + if day_diff < 31: + if day_diff/7 == 1: + return "a week ago" + else: + return str(day_diff/7) + " weeks ago" + if day_diff < 365: + if day_diff/30 == 1: + return "a month ago" + else: + return str(day_diff/30) + " months ago" + else: + if day_diff/365 == 1: + return "a year ago" + else: + return str(day_diff/365) + " years ago"