/usr/share/pyshared/jsb/utils/timeutils.py is in jsonbot 0.84.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | # lib/utils/timeutils.py
#
#
""" time related helper functions. """
## jsb imports
from exception import handle_exception
## basic imports
import time
import re
import calendar
## defines
leapfactor = float(6*60*60)/float(365*24*60*60)
timere = re.compile('(\S+)\s+(\S+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)')
bdmonths = ['Bo', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
## elapsedstring function
def elapsedstring(nsec, ywd = None):
""" given the number of seconds return a string of the elapsed time. """
nsec = int(float(nsec))
year = 365*24*60*60
week = 7*24*60*60
day = 24*60*60
hour = 60*60
minute = 60
nsec -= nsec * leapfactor
years = int(nsec/year)
nsec -= years*year
weeks = int(nsec/week)
nsec -= weeks*week
days = int(nsec/day)
nsec -= days*day
hours = int(nsec/hour)
nsec -= hours*hour
minutes = int(nsec/minute)
sec = int(nsec - minutes*minute)
result = ''
if (years > 1): result = str(years) + " years "
if (years == 1): result = "1 year "
if (weeks > 1): result += str(weeks) + " weeks "
if (weeks == 1): result += "1 week "
if (days > 1):
if ywd: result += 'and '+ str(days) + " days"
else: result += str(days) + " days "
if (days == 1):
if ywd: result += 'and 1 day'
else: result += "1 day "
if ywd: return result
if (hours > 1): result += str(hours) + " hours "
if (hours == 1): result += "1 hour "
if (minutes > 1): result += str(minutes) + " minutes "
if (minutes == 1): result += "1 minute "
if sec == 0:
if result: return result
else: return 0
if (sec == 1):
if result: result += "and 1 second "
else: result = "1 second"
else:
if result: result += "and " + str(sec) + " seconds"
else: result = str(sec) + " seconds"
return result.strip()
## hourmin function
def hourmin(ttime):
""" return the hours:minutes of a unix timestamp. """
result = ""
timeres = time.localtime(ttime)
if timeres[3] < 10: result += "0" + str(timeres[3]) + ":"
else: result += str(timeres[3]) + ":"
if timeres[4] < 10: result += "0" + str(timeres[4])
else: result += str(timeres[4])
return result
## striptime function
def striptime(what):
""" strip time indicators from string. """
what = str(what)
what = re.sub('\d+-\d+-\d+', '', what)
what = re.sub('\d+-\d+', '', what)
what = re.sub('\d+:\d+', '', what)
what = re.sub('\s+', ' ', what)
return what.strip()
## now function
def now():
""" return current time. """
if time.daylight: ttime = time.ctime(time.time() + int(time.timezone) + 3600)
else: ttime = time.ctime(time.time() + int(time.timezone))
return ttime
## today function
def today():
""" return time of 0:00 today. """
if time.daylight: ttime = time.ctime(time.time() + int(time.timezone) + 3600)
else: ttime = time.ctime(time.time() + int(time.timezone))
matched = re.search(timere, ttime)
if matched:
temp = "%s %s %s" % (matched.group(3), matched.group(2), matched.group(7))
timestring = time.strptime(temp, "%d %b %Y")
result = time.mktime(timestring)
return result
## strtotime function
def strtotime(what):
""" convert string to time. """
daymonthyear = 0
hoursmin = 0
try:
dmyre = re.search('(\d+)-(\d+)-(\d+)', str(what))
if dmyre:
(day, month, year) = dmyre.groups()
day = int(day)
month = int(month)
year = int(year)
if day <= calendar.monthrange(year, month)[1]:
date = "%s %s %s" % (day, bdmonths[month], year)
daymonthyear = time.mktime(time.strptime(date, "%d %b %Y"))
else: return None
else:
dmre = re.search('(\d+)-(\d+)', str(what))
if dmre:
year = time.localtime()[0]
(day, month) = dmre.groups()
day = int(day)
month = int(month)
if day <= calendar.monthrange(year, month)[1]:
date = "%s %s %s" % (day, bdmonths[month], year)
daymonthyear = time.mktime(time.strptime(date, "%d %b %Y"))
else: return None
hmsre = re.search('(\d+):(\d+):(\d+)', str(what))
if hmsre:
(h, m, s) = hmsre.groups()
h = int(h)
m = int(m)
s = int(s)
if h > 24 or h < 0 or m > 60 or m < 0 or s > 60 or s < 0: return None
hours = 60 * 60 * (int(hmsre.group(1)))
hoursmin = hours + int(hmsre.group(2)) * 60
hms = hoursmin + int(hmsre.group(3))
else:
hmre = re.search('(\d+):(\d+)', str(what))
if hmre:
(h, m) = hmre.groups()
h = int(h)
m = int(m)
if h > 24 or h < 0 or m > 60 or m < 0: return None
hours = 60 * 60 * (int(hmre.group(1)))
hms = hours + int(hmre.group(2)) * 60
else: hms = 0
if not daymonthyear and not hms: return None
if daymonthyear == 0: heute = today()
else: heute = daymonthyear
return heute + hms
except OverflowError: return None
except ValueError:return None
except Exception, ex: pass
def strtotime2(what):
""" convert string to time. """
daymonthyear = 0
hoursmin = 0
try:
dmyre = re.search('(\d+)-(\d+)-(\d+)', str(what))
if dmyre:
(year, month, day) = dmyre.groups()
day = int(day)
month = int(month)
year = int(year)
if day <= calendar.monthrange(year, month)[1]:
date = "%s %s %s" % (day, bdmonths[month], year)
daymonthyear = time.mktime(time.strptime(date, "%d %b %Y"))
else: return None
else:
dmre = re.search('(\d+)-(\d+)', str(what))
if dmre:
year = time.localtime()[0]
(day, month) = dmre.groups()
day = int(day)
month = int(month)
if day <= calendar.monthrange(year, month)[1]:
date = "%s %s %s" % (day, bdmonths[month], year)
daymonthyear = time.mktime(time.strptime(date, "%d %b %Y"))
else: return None
hmsre = re.search('(\d+):(\d+):(\d+)', str(what))
if hmsre:
(h, m, s) = hmsre.groups()
h = int(h)
m = int(m)
s = int(s)
if h > 24 or h < 0 or m > 60 or m < 0 or s > 60 or s < 0: return None
hours = 60 * 60 * (int(hmsre.group(1)))
hoursmin = hours + int(hmsre.group(2)) * 60
hms = hoursmin + int(hmsre.group(3))
else:
hmre = re.search('(\d+):(\d+)', str(what))
if hmre:
(h, m) = hmre.groups()
h = int(h)
m = int(m)
if h > 24 or h < 0 or m > 60 or m < 0: return None
hours = 60 * 60 * (int(hmre.group(1)))
hms = hours + int(hmre.group(2)) * 60
else: hms = 0
if not daymonthyear and not hms: return None
if daymonthyear == 0: heute = today()
else: heute = daymonthyear
return heute + hms
except OverflowError: return None
except ValueError:return None
except Exception, ex: pass
## uurminsec function
def uurminsec(ttime):
""" return hours:minutes:seconds of the given time. """
result = ""
timeres = time.localtime(ttime)
if timeres[3] < 10: result += "0" + str(timeres[3]) + ":"
else: result += str(timeres[3]) + ":"
if timeres[4] < 10: result += "0" + str(timeres[4]) + ":"
else: result += str(timeres[4]) + ":"
if timeres[5] < 10: result += "0" + str(timeres[5])
else: result += str(timeres[5])
return result
## getdaymonth function
def getdaymonth(ttime):
""" return day-month of the given time. """
timestr = time.ctime(ttime)
result = re.search(timere, timestr)
if result: return (result.group(3), result.group(2))
else: return (None, None)
## getdaymonthyear function
def getdaymonthyear(ttime):
""" return day-month-year of the given time. """
timestr = time.ctime(ttime)
result = re.search(timere, timestr)
if result: return (result.group(3), result.group(2), result.group[7])
else: return (None, None, None)
## dmy function
def dmy(ttime):
""" return day month year as a string. """
timestr = time.ctime(ttime)
result = re.search(timere, timestr)
if result: return "%s %s %s" % (result.group(3), result.group(2), result.group(7))
|