utils.py
changeset 72 5ade0288f2ec
parent 53 8103d18907a0
child 73 5a7188bf2894
equal deleted inserted replaced
71:e909bde831e7 72:5ade0288f2ec
     1 """
     1 """
     2     Miscellaneous things
     2     Miscellaneous things
     3 """
     3 """
     4 
     4 
     5 import datetime
     5 import datetime, calendar, pytz
     6 
     6 
     7 from qmsk.web.urltree import URLType
     7 from qmsk.web.urltree import URLType
     8 
     8 
     9 class URLChannelName (URLType) :
     9 class URLChannelName (URLType) :
    10     """
    10     """
    56             datetime.date -> date_str
    56             datetime.date -> date_str
    57         """
    57         """
    58 
    58 
    59         return date.strftime(self.date_fmt)
    59         return date.strftime(self.date_fmt)
    60 
    60 
       
    61 class URLTimestampType (URLType) :
       
    62     """
       
    63         Handles an integer UNIX timestamp as an UTC datetime
       
    64     """
       
    65 
       
    66     def parse (self, timestamp_str) :
       
    67         """
       
    68             timestamp_str -> pytz.utc datetime.datetime
       
    69         """
       
    70 
       
    71         return datetime.datetime.utcfromtimestamp(int(timestamp_str)).replace(tzinfo=pytz.utc)
       
    72     
       
    73     def build (self, dtz) :
       
    74         """
       
    75             pytz.utc datetime.datetime -> timestamp_str
       
    76         """
       
    77 
       
    78         return str(calendar.timegm(dtz.utctimetuple()))
       
    79