]>
jfr.im git - irc/rizon/acid.git/blob - pyva/src/main/python/utils.py
3 from datetime
import datetime
4 from htmlentitydefs
import name2codepoint
6 def unix_time(datetime
):
7 return int(time
.mktime(datetime
.timetuple()))
9 def parse_timespan(text
):
15 duration
+= int(buffer) * 24 * 60 * 60
18 duration
+= int(buffer) * 60 * 60
21 duration
+= int(buffer) * 60
24 duration
+= int(buffer)
31 def get_timespan(date
):
32 td
= datetime
.now() - date
35 return '%d year%s' % (y
, 's' if y
> 1 else '')
38 return '%d day%s' % (d
, 's' if d
> 1 else '')
39 elif td
.seconds
> 3600:
40 h
= int(td
.seconds
/3600)
41 return '%d hour%s' % (h
, 's' if h
> 1 else '')
43 m
= int(td
.seconds
/60)
44 return '%d minute%s' % (m
, 's' if m
> 1 else '')
47 return '%d second%s' % (s
, 's' if s
> 1 else '')
49 def format_name(name
):
50 if name
.endswith('s'):
55 def format_thousand(number
, add_prefix
= False):
56 if not isinstance(number
, int):
59 text
= str(abs(number
))
62 count
= (length
- 1) / 3
64 for i
in xrange(1, count
+ 1):
65 text
= text
[:length
- (i
* 3)] + ',' + text
[length
- (i
* 3):]
74 def format_hms(seconds
):
75 hours
= seconds
/ 3600
77 minutes
= seconds
/ 60
80 return "%02d:%02d" % (minutes
, seconds
)
81 return "%02d:%02d:%02d" % (hours
, minutes
, seconds
)
83 def format_ascii_irc(message
):
84 return message
.replace('@errsep', '@b@c4::@o').replace('@nsep', '@b@c7::@o').replace('@sep', '@b@c3::@o').replace('@b', chr(2)).replace('@c', chr(3)).replace('@o', chr(15)).replace('@u', chr(31))
86 def strip_ascii_irc(message
):
90 if char
not in [chr(2), chr(15), chr(22), chr(31)]: #Not actually stripping color codes, but we don't need that (yet)
96 "unescape HTML code refs; c.f. http://wiki.python.org/moin/EscapingHtml"
98 # XXX: There must be a better way than hardcoding these
99 name2codepoint
['#39'] = 39
100 name2codepoint
['#215'] = 215
101 name2codepoint
['#8260'] = 8260
103 return re
.sub('&(%s);' % '|'.join(name2codepoint
),
104 lambda m
: unichr(name2codepoint
[m
.group(1)]), s
)