1
# gozerbot/utils/timeutils.py
5
""" time related helper functions """
7
__copyright__ = 'this file is in the public domain'
9
from exception import handle_exception
10
import time, re, calendar
12
leapfactor = float(6*60*60)/float(365*24*60*60)
14
def elapsedstring(nsec, ywd = None):
15
nsec = int(float(nsec))
21
nsec -= nsec * leapfactor
22
years = int(nsec/year)
24
weeks = int(nsec/week)
28
hours = int(nsec/hour)
30
minutes = int(nsec/minute)
31
sec = int(nsec - minutes*minute)
34
result = str(years) + " years "
38
result += str(weeks) + " weeks "
43
result += 'and '+ str(days) + " days"
45
result += str(days) + " days "
54
result += str(hours) + " hours "
58
result += str(minutes) + " minutes "
68
result += "and 1 second "
73
result += "and " + str(sec) + " seconds"
75
result = str(sec) + " seconds"
80
timeres = time.localtime(ttime)
82
result += "0" + str(timeres[3]) + ":"
84
result += str(timeres[3]) + ":"
86
result += "0" + str(timeres[4])
88
result += str(timeres[4])
91
timere = re.compile('(\S+)\s+(\S+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)')
93
bdmonths = ['Bo', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', \
94
'Sep', 'Oct', 'Nov', 'Dec']
97
""" strip time indicators from string """
99
what = re.sub('\d+-\d+-\d+', '', what)
100
what = re.sub('\d+-\d+', '', what)
101
what = re.sub('\d+:\d+', '', what)
102
what = re.sub('\s+', ' ', what)
107
ttime = time.ctime(time.time() + int(time.timezone) + 3600)
109
ttime = time.ctime(time.time() + int(time.timezone))
114
ttime = time.ctime(time.time() + int(time.timezone) + 3600)
116
ttime = time.ctime(time.time() + int(time.timezone))
117
matched = re.search(timere, ttime)
119
temp = "%s %s %s" % (matched.group(3), matched.group(2), \
121
timestring = time.strptime(temp, "%d %b %Y")
122
result = time.mktime(timestring)
129
dmyre = re.search('(\d+)-(\d+)-(\d+)', str(what))
131
(day, month, year) = dmyre.groups()
135
if day <= calendar.monthrange(year, month)[1]:
136
date = "%s %s %s" % (day, bdmonths[month], year)
137
daymonthyear = time.mktime(time.strptime(date, "%d %b %Y"))
141
dmre = re.search('(\d+)-(\d+)', str(what))
143
year = time.localtime()[0]
144
(day, month) = dmre.groups()
147
if day <= calendar.monthrange(year, month)[1]:
148
date = "%s %s %s" % (day, bdmonths[month], year)
149
daymonthyear = time.mktime(time.strptime(date, "%d %b %Y"))
152
hmsre = re.search('(\d+):(\d+):(\d+)', str(what))
154
(h, m, s) = hmsre.groups()
158
if h > 24 or h < 0 or m > 60 or m < 0 or s > 60 or s < 0:
160
hours = 60 * 60 * (int(hmsre.group(1)))
161
hoursmin = hours + int(hmsre.group(2)) * 60
162
hms = hoursmin + int(hmsre.group(3))
164
hmre = re.search('(\d+):(\d+)', str(what))
166
(h, m) = hmre.groups()
169
if h > 24 or h < 0 or m > 60 or m < 0:
171
hours = 60 * 60 * (int(hmre.group(1)))
172
hms = hours + int(hmre.group(2)) * 60
175
if not daymonthyear and not hms:
177
if daymonthyear == 0:
182
except OverflowError:
186
except Exception, ex:
190
def uurminsec(ttime):
192
timeres = time.localtime(ttime)
194
result += "0" + str(timeres[3]) + ":"
196
result += str(timeres[3]) + ":"
198
result += "0" + str(timeres[4]) + ":"
200
result += str(timeres[4]) + ":"
202
result += "0" + str(timeres[5])
204
result += str(timeres[5])
208
def getdaymonth(ttime):
209
timestr = time.ctime(ttime)
210
result = re.search(timere, timestr)
212
return (result.group(3), result.group(2))
216
def getdaymonthyear(ttime):
217
timestr = time.ctime(ttime)
218
result = re.search(timere, timestr)
220
return (result.group(3), result.group(2), result.group[7])
222
return (None, None, None)
225
timestr = time.ctime(ttime)
226
result = re.search(timere, timestr)
228
return "%s %s %s" % (result.group(3), result.group(2), result.group(7))