From: Ant Zucaro Date: Sun, 2 Sep 2012 17:08:36 +0000 (-0400) Subject: Use relative time everywhere, but add hover with absolute time. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=71c8d6efdc344d0fc9b83d834f02f31ef6293916;p=xonotic%2Fxonstat.git Use relative time everywhere, but add hover with absolute time. Use "fuzzy" dates everywhere (e.g. "an hour ago"), but allow for a hover showing the absolute UTC time when needed. This is done with a span and a title attribute. --- diff --git a/xonstat/models.py b/xonstat/models.py index 84a64ea..6de2fa0 100644 --- a/xonstat/models.py +++ b/xonstat/models.py @@ -67,6 +67,9 @@ class Server(object): def to_dict(self): return {'server_id':self.server_id, 'name':self.name.encode('utf-8')} + def fuzzy_date(self): + return pretty_date(self.create_dt) + class Map(object): def __init__(self, name=None): @@ -78,6 +81,8 @@ class Map(object): def to_dict(self): return {'map_id':self.map_id, 'name':self.name, 'version':self.version} + def fuzzy_date(self): + return pretty_date(self.create_dt) class Game(object): def __init__(self, game_id=None, start_dt=None, game_type_cd=None, diff --git a/xonstat/templates/game_info.mako b/xonstat/templates/game_info.mako index 67446bc..19234e9 100644 --- a/xonstat/templates/game_info.mako +++ b/xonstat/templates/game_info.mako @@ -31,7 +31,7 @@ Game Information

Game Detail

- Played on: ${game.start_dt.strftime('%m/%d/%Y at %I:%M %p')}
+ Played: ${game.fuzzy_date()} (${game.start_dt.strftime('%a, %d %b %Y %H:%M:%S UTC')})
Game Type: ${game.game_type_cd}
Server: ${server.name}
Map: ${map.name}
diff --git a/xonstat/templates/main_index.mako b/xonstat/templates/main_index.mako index c731a0e..3b9d453 100644 --- a/xonstat/templates/main_index.mako +++ b/xonstat/templates/main_index.mako @@ -226,7 +226,7 @@ Leaderboard ${game.game_type_cd} ${server.name} ${map.name} - ${game.start_dt.strftime('%m/%d/%Y %H:%M')} + ${game.fuzzy_date()} % if pgstat.player_id > 2: ${pgstat.nick_html_colors()|n} diff --git a/xonstat/templates/map_index.mako b/xonstat/templates/map_index.mako index bb297ee..49a97c6 100644 --- a/xonstat/templates/map_index.mako +++ b/xonstat/templates/map_index.mako @@ -29,7 +29,7 @@ Map Index % for map in maps: ${map.name} - ${map.create_dt.strftime('%m/%d/%Y at %H:%M')} + ${map.fuzzy_date()} % endfor diff --git a/xonstat/templates/map_info.mako b/xonstat/templates/map_info.mako index 7e975c1..21b8589 100644 --- a/xonstat/templates/map_info.mako +++ b/xonstat/templates/map_info.mako @@ -21,7 +21,7 @@ ${parent.title()} % else:

${gmap.name}

- Added on ${gmap.create_dt.strftime('%m/%d/%Y at %H:%M')} + Added ${gmap.fuzzy_date()}

@@ -126,7 +126,7 @@ ${parent.title()} View ${game.game_type_cd} - ${game.start_dt.strftime('%m/%d/%Y %H:%M')} + ${game.fuzzy_date()} % if pgstat.player_id > 2: ${pgstat.nick_html_colors()|n} diff --git a/xonstat/templates/player_index.mako b/xonstat/templates/player_index.mako index 2ef7d5c..78848d7 100644 --- a/xonstat/templates/player_index.mako +++ b/xonstat/templates/player_index.mako @@ -29,7 +29,7 @@ Player Index % for player in players: ${player.nick_html_colors()|n} - ${player.joined_pretty_date()} + ${player.joined_pretty_date()} % endfor diff --git a/xonstat/templates/player_info.mako b/xonstat/templates/player_info.mako index 1983509..f1237d0 100644 --- a/xonstat/templates/player_info.mako +++ b/xonstat/templates/player_info.mako @@ -182,7 +182,7 @@ Player Information

Member Since: ${player.create_dt.strftime('%m/%d/%Y at %I:%M %p')}
- Last Seen: ${recent_games[0][1].fuzzy_date()}
+ Last Seen: ${recent_games[0][1].fuzzy_date()}
Playing Time: ${total_stats['alivetime']}
@@ -402,7 +402,7 @@ Player Information % endif % endif - ${game.fuzzy_date()} + ${game.fuzzy_date()} % endfor diff --git a/xonstat/templates/search.mako b/xonstat/templates/search.mako index a7d5454..45223b2 100644 --- a/xonstat/templates/search.mako +++ b/xonstat/templates/search.mako @@ -47,7 +47,7 @@ % for player in results: ${player.nick_html_colors()|n} - ${player.joined_pretty_date()} + ${player.joined_pretty_date()} % endfor @@ -63,7 +63,7 @@ % for server in results: ${server.name} - ${server.create_dt.strftime('%m/%d/%Y at %I:%M %p')} + ${server.fuzzy_date()} % endfor @@ -79,7 +79,7 @@ % for map in results: ${map.name} - ${map.create_dt.strftime('%m/%d/%Y at %I:%M %p')} + ${map.fuzzy_date()} % endfor @@ -92,14 +92,14 @@ Map Server - Played On + Time % for (game, server, gmap) in results: View ${gmap.name} ${server.name} - ${game.create_dt.strftime('%m/%d/%Y at %I:%M %p')} + ${game.fuzzy_date()} % endfor diff --git a/xonstat/templates/server_index.mako b/xonstat/templates/server_index.mako index bc2b378..7559947 100644 --- a/xonstat/templates/server_index.mako +++ b/xonstat/templates/server_index.mako @@ -29,7 +29,7 @@ Server Index % for server in servers: ${server.name} - ${server.create_dt.strftime('%m/%d/%Y at %H:%M')} + ${server.fuzzy_date()} % endfor diff --git a/xonstat/templates/server_info.mako b/xonstat/templates/server_info.mako index 2c8d496..96875ca 100644 --- a/xonstat/templates/server_info.mako +++ b/xonstat/templates/server_info.mako @@ -22,7 +22,7 @@ Server Information

IP Address: ${server.ip_addr}
Revision: ${server.revision}
- Added on ${server.create_dt.strftime('%m/%d/%Y at %I:%M %p')}
+ Added ${server.fuzzy_date()}

@@ -139,7 +139,7 @@ Server Information View ${game.game_type_cd} ${map.name} - ${game.start_dt.strftime('%m/%d/%Y %H:%M')} + ${game.fuzzy_date()} % if pgstat.player_id > 2: ${pgstat.nick_html_colors()|n} diff --git a/xonstat/util.py b/xonstat/util.py index 1bea47c..f9aa1d5 100644 --- a/xonstat/util.py +++ b/xonstat/util.py @@ -1,7 +1,7 @@ import re from colorsys import rgb_to_hls, hls_to_rgb from cgi import escape as html_escape -from datetime import datetime +from datetime import datetime, timedelta # Map of special chars to ascii from Darkplace's console.c. _qfont_table = [ @@ -113,53 +113,41 @@ def page_url(page): 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 - """ + '''Returns a human-readable relative date.''' now = datetime.utcnow() if type(time) is int: diff = now - datetime.fromtimestamp(time) elif isinstance(time,datetime): - diff = now - time + diff = now - time elif not time: + print "not a time value" 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" + + dim = round(diff.seconds/60.0 + diff.days*1440.0) + + if dim == 0: + return "less than a minute ago" + elif dim == 1: + return "1 minute ago" + elif dim >= 2 and dim <= 44: + return "{0} minutes ago".format(int(dim)) + elif dim >= 45 and dim <= 89: + return "about 1 hour ago" + elif dim >= 90 and dim <= 1439: + return "about {0} hours ago".format(int(round(dim/60.0))) + elif dim >= 1440 and dim <= 2519: + return "1 day ago" + elif dim >= 2520 and dim <= 43199: + return "{0} days ago".format(int(round(dim/1440.0))) + elif dim >= 43200 and dim <= 86399: + return "about 1 month ago" + elif dim >= 86400 and dim <= 525599: + return "{0} months ago".format(int(round(dim/43200.0))) + elif dim >= 525600 and dim <= 655199: + return "about 1 year ago" + elif dim >= 655200 and dim <= 914399: + return "over 1 year ago" + elif dim >= 914400 and dim <= 1051199: + return "almost 2 years ago" else: - if day_diff/365 == 1: - return "a year ago" - else: - return str(day_diff/365) + " years ago" + return "about {0} years ago".format(int(round(dim/525600.0)))