WebHelpers / webhelpers / time.py

"""Time module from web.util.time for turning a string into seconds based on code submited to the PythonWeb mailing list by Felix Schwartz and adapted by James Gardner


    >>> from webhelpers.time import seconds
    >>> print seconds('10sec')
    >>> print seconds('4hr')


import re, string

class Time:
    def __init__(self):
    def getSeconds(self, parameter):
        if parameter in self.seconds:   return 1
        elif parameter in self.minutes: return 60
        elif parameter in self.hours:   return 60 * 60
        elif parameter in self.days:    return 60 * 60 * 24
        elif parameter in self.weeks:   return 60 * 60 * 24 * 7
        elif parameter in self.months:  return 60 * 60 * 24 * 30
        else: return 0
    def convertIntoSeconds(self, age):
        if age == None or age == '': return 0
        if re.match('\d+$', age): return int(age)
        match = re.match('(\d+)(' + string.join(self.seconds + self.minutes + self.hours + self.days + self.weeks + self.months, '|') + ')$', age)
        if match != None:
            number = int(match.group(1))
            return number * self.getSeconds(match.group(2))
        return -1
def seconds(value):
    t = Time()
    v = t.convertIntoSeconds(str(value))
    if v == -1:
        raise Exception('Invalid time, %s'%repr(value))
    return v
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.