utils.py
changeset 117 f0b4097f5781
parent 115 751e3fcd11d2
child 129 67a30d680f60
equal deleted inserted replaced
116:81da986f6ed5 117:f0b4097f5781
    33 
    33 
    34         return chan.id
    34         return chan.id
    35 
    35 
    36 class URLDateType (URLType) :
    36 class URLDateType (URLType) :
    37     """
    37     """
    38         Handle dates in URLs as shortened UTC datetime timestamps
    38         Handle dates in URLs as naive datetime objects (with indeterminate time info)
    39     """
    39     """
    40 
       
    41     # percision = hour
       
    42     SCALE = 60 * 60
       
    43 
    40 
    44     def __init__ (self, date_fmt) :
    41     def __init__ (self, date_fmt) :
    45         """
    42         """
    46             Format/parse dates using the given format
    43             Format/parse dates using the given format
    47         """
    44         """
    48 
    45 
    49         self.date_fmt = date_fmt
    46         self.date_fmt = date_fmt
    50     
    47     
    51     def parse (self, sts_str) :
    48     def parse (self, date_str) :
    52         """
    49         """
    53             short_timestamp_str -> datetime.datetime
    50             date_str -> naive datetime.datetime
    54         """
    51         """
    55         
    52         
    56         # scale -> from_utc + return
    53         return datetime.datetime.strptime(date_str, self.date_fmt)
    57         return from_utc_timestamp(int(sts_str) * self.SCALE)
       
    58     
    54     
    59     def build (self, dtz) :
    55     def build (self, date) :
    60         """
    56         """
    61             datetime.datetime -> short_timestamp_str
    57             datetime.date -> date_str
    62         """
    58         """
    63 
    59 
    64         # force it to be interpreted as UTC
    60         return date.strftime(self.date_fmt)
    65         dt_utc = dtz.replace(tzinfo=pytz.utc)
       
    66         
       
    67         # scale the UTC timestamp
       
    68         sts = to_utc_timestamp(dt_utc) / self.SCALE
       
    69         
       
    70         # str
       
    71         return str(sts)
       
    72 
    61 
    73 class URLTimestampType (URLType) :
    62 class URLTimestampType (URLType) :
    74     """
    63     """
    75         Handles an integer UNIX timestamp as an UTC datetime
    64         Handles an integer UNIX timestamp as an UTC datetime
    76     """
    65     """