~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to gozerbot/utils/timeutils.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/utils/timeutils.py
 
2
#
 
3
#
 
4
 
 
5
""" time related helper functions """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
from exception import handle_exception
 
10
import time, re, calendar
 
11
 
 
12
leapfactor = float(6*60*60)/float(365*24*60*60)
 
13
 
 
14
def elapsedstring(nsec, ywd = None):
 
15
    nsec = int(float(nsec))
 
16
    year = 365*24*60*60
 
17
    week = 7*24*60*60
 
18
    day = 24*60*60
 
19
    hour = 60*60
 
20
    minute = 60
 
21
    nsec -= nsec * leapfactor
 
22
    years = int(nsec/year)
 
23
    nsec -= years*year
 
24
    weeks = int(nsec/week)
 
25
    nsec -= weeks*week
 
26
    days = int(nsec/day)
 
27
    nsec -= days*day
 
28
    hours = int(nsec/hour)
 
29
    nsec -= hours*hour
 
30
    minutes = int(nsec/minute)
 
31
    sec = int(nsec - minutes*minute)   
 
32
    result = ''
 
33
    if (years > 1):
 
34
        result = str(years) + " years "
 
35
    if (years == 1):
 
36
        result = "1 year "
 
37
    if (weeks > 1):
 
38
        result += str(weeks) + " weeks "
 
39
    if (weeks == 1):
 
40
        result += "1 week "
 
41
    if (days > 1):
 
42
        if ywd:
 
43
            result += 'and '+ str(days) + " days"
 
44
        else:
 
45
            result += str(days) + " days "
 
46
    if (days == 1):
 
47
        if ywd:
 
48
            result += 'and 1 day'
 
49
        else:
 
50
            result += "1 day "
 
51
    if ywd:
 
52
        return result
 
53
    if (hours > 1):
 
54
        result += str(hours) + " hours "
 
55
    if (hours == 1):
 
56
        result += "1 hour "
 
57
    if (minutes > 1):
 
58
        result += str(minutes) + " minutes "
 
59
    if (minutes == 1):
 
60
        result += "1 minute "
 
61
    if sec == 0:
 
62
        if result:
 
63
            return result
 
64
        else:
 
65
            return 0
 
66
    if (sec == 1):
 
67
        if result:
 
68
            result += "and 1 second "
 
69
        else:
 
70
            result = "1 second"
 
71
    else:
 
72
        if result:
 
73
            result += "and " + str(sec) + " seconds"
 
74
        else: 
 
75
            result = str(sec) + " seconds"
 
76
    return result.strip()
 
77
 
 
78
def hourmin(ttime):
 
79
    result = ""
 
80
    timeres = time.localtime(ttime)
 
81
    if timeres[3] < 10:
 
82
        result += "0" + str(timeres[3]) + ":"
 
83
    else:
 
84
        result += str(timeres[3]) + ":"
 
85
    if timeres[4] < 10:
 
86
        result += "0" + str(timeres[4])
 
87
    else:
 
88
        result += str(timeres[4])
 
89
    return result
 
90
        
 
91
timere = re.compile('(\S+)\s+(\S+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)')
 
92
 
 
93
bdmonths = ['Bo', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', \
 
94
'Sep', 'Oct', 'Nov', 'Dec']
 
95
 
 
96
def striptime(what):
 
97
    """ strip time indicators from string """
 
98
    what = str(what)
 
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)
 
103
    return what.strip()
 
104
 
 
105
def now():
 
106
    if time.daylight:
 
107
        ttime = time.ctime(time.time() + int(time.timezone) + 3600)
 
108
    else:
 
109
        ttime = time.ctime(time.time() + int(time.timezone))
 
110
    return ttime
 
111
 
 
112
def today():
 
113
    if time.daylight:
 
114
        ttime = time.ctime(time.time() + int(time.timezone) + 3600)
 
115
    else:
 
116
        ttime = time.ctime(time.time() + int(time.timezone))
 
117
    matched = re.search(timere, ttime)
 
118
    if matched:
 
119
        temp = "%s %s %s" % (matched.group(3), matched.group(2), \
 
120
matched.group(7))
 
121
        timestring = time.strptime(temp, "%d %b %Y")
 
122
        result = time.mktime(timestring)
 
123
        return result
 
124
 
 
125
def strtotime(what):
 
126
    daymonthyear = 0
 
127
    hoursmin = 0
 
128
    try:
 
129
        dmyre = re.search('(\d+)-(\d+)-(\d+)', str(what))
 
130
        if dmyre:
 
131
            (day, month, year) = dmyre.groups()
 
132
            day = int(day)
 
133
            month = int(month)
 
134
            year = int(year)
 
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"))
 
138
            else:
 
139
                return None
 
140
        else:
 
141
            dmre = re.search('(\d+)-(\d+)', str(what))
 
142
            if dmre:
 
143
                year = time.localtime()[0]
 
144
                (day, month) = dmre.groups()
 
145
                day = int(day)
 
146
                month = int(month)
 
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"))
 
150
                else:
 
151
                    return None
 
152
        hmsre = re.search('(\d+):(\d+):(\d+)', str(what))
 
153
        if hmsre:
 
154
            (h, m, s) = hmsre.groups()
 
155
            h = int(h)
 
156
            m = int(m)
 
157
            s = int(s)
 
158
            if h > 24 or h < 0 or m > 60 or m < 0 or s > 60 or s < 0:
 
159
                return None
 
160
            hours = 60 * 60 * (int(hmsre.group(1)))
 
161
            hoursmin = hours  + int(hmsre.group(2)) * 60
 
162
            hms = hoursmin + int(hmsre.group(3))
 
163
        else:
 
164
            hmre = re.search('(\d+):(\d+)', str(what))
 
165
            if hmre:
 
166
                (h, m) = hmre.groups()
 
167
                h = int(h)
 
168
                m = int(m)
 
169
                if h > 24 or h < 0 or m > 60 or m < 0:
 
170
                    return None
 
171
                hours = 60 * 60 * (int(hmre.group(1)))
 
172
                hms = hours  + int(hmre.group(2)) * 60
 
173
            else:
 
174
                hms = 0
 
175
        if not daymonthyear and not hms:
 
176
            return None
 
177
        if daymonthyear == 0:
 
178
            heute = today()
 
179
        else:
 
180
            heute = daymonthyear
 
181
        return heute + hms
 
182
    except OverflowError:
 
183
        return None
 
184
    except ValueError:
 
185
        return None
 
186
    except Exception, ex:
 
187
        handle_exception()
 
188
        return None
 
189
 
 
190
def uurminsec(ttime):
 
191
    result = ""
 
192
    timeres = time.localtime(ttime)
 
193
    if timeres[3] < 10:
 
194
        result += "0" + str(timeres[3]) + ":"
 
195
    else:
 
196
        result += str(timeres[3]) + ":"
 
197
    if timeres[4] < 10:
 
198
        result += "0" + str(timeres[4]) + ":"
 
199
    else:
 
200
        result += str(timeres[4]) + ":"
 
201
    if timeres[5] < 10:
 
202
        result += "0" + str(timeres[5])
 
203
    else:
 
204
        result += str(timeres[5])
 
205
    return result
 
206
 
 
207
 
 
208
def getdaymonth(ttime):
 
209
    timestr = time.ctime(ttime)
 
210
    result = re.search(timere, timestr)
 
211
    if result:
 
212
        return (result.group(3), result.group(2))
 
213
    else:
 
214
        return (None, None)
 
215
 
 
216
def getdaymonthyear(ttime):
 
217
    timestr = time.ctime(ttime)
 
218
    result = re.search(timere, timestr)
 
219
    if result:
 
220
        return (result.group(3), result.group(2), result.group[7])
 
221
    else:
 
222
        return (None, None, None)
 
223
 
 
224
def dmy(ttime):
 
225
    timestr = time.ctime(ttime)
 
226
    result = re.search(timere, timestr)
 
227
    if result:
 
228
        return "%s %s %s" % (result.group(3), result.group(2), result.group(7))
 
229
    else:
 
230
        return None