utils.py
changeset 115 751e3fcd11d2
parent 95 ebdbda3dd5d0
child 117 f0b4097f5781
equal deleted inserted replaced
114:d4848d807fd1 115:751e3fcd11d2
    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 naive datetime objects (with indeterminate time info)
    38         Handle dates in URLs as shortened UTC datetime timestamps
    39     """
    39     """
       
    40 
       
    41     # percision = hour
       
    42     SCALE = 60 * 60
    40 
    43 
    41     def __init__ (self, date_fmt) :
    44     def __init__ (self, date_fmt) :
    42         """
    45         """
    43             Format/parse dates using the given format
    46             Format/parse dates using the given format
    44         """
    47         """
    45 
    48 
    46         self.date_fmt = date_fmt
    49         self.date_fmt = date_fmt
    47     
    50     
    48     def parse (self, date_str) :
    51     def parse (self, sts_str) :
    49         """
    52         """
    50             date_str -> naive datetime.datetime
    53             short_timestamp_str -> datetime.datetime
    51         """
    54         """
    52         
    55         
    53         return datetime.datetime.strptime(date_str, self.date_fmt)
    56         # scale -> from_utc + return
       
    57         return from_utc_timestamp(int(sts_str) * self.SCALE)
    54     
    58     
    55     def build (self, date) :
    59     def build (self, dtz) :
    56         """
    60         """
    57             datetime.date -> date_str
    61             datetime.datetime -> short_timestamp_str
    58         """
    62         """
    59 
    63 
    60         return date.strftime(self.date_fmt)
    64         # force it to be interpreted as UTC
       
    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)
    61 
    72 
    62 class URLTimestampType (URLType) :
    73 class URLTimestampType (URLType) :
    63     """
    74     """
    64         Handles an integer UNIX timestamp as an UTC datetime
    75         Handles an integer UNIX timestamp as an UTC datetime
    65     """
    76     """