src/pyams_utils/date.py
changeset 64 70f2321e267a
parent 36 7398e25bad99
child 72 9049384a2bd4
equal deleted inserted replaced
63:f188db1a1ce7 64:70f2321e267a
    92 def format_datetime(value, format=EXT_DATETIME_FORMAT, request=None):
    92 def format_datetime(value, format=EXT_DATETIME_FORMAT, request=None):
    93     """Format given datetime with the given format"""
    93     """Format given datetime with the given format"""
    94     return format_date(value, format, request)
    94     return format_date(value, format, request)
    95 
    95 
    96 
    96 
    97 def get_age(value):
    97 def get_age(value, request=None):
    98     """Get age of a given datetime (including timezone) compared to current datetime (in UTC)
    98     """Get age of a given datetime (including timezone) compared to current datetime (in UTC)"""
    99     
    99     if request is None:
   100     @param value: a datetime value, including timezone
   100         request = check_request()
   101     @type value: datetime
       
   102     @return: string representing value age
       
   103     @rtype: gettext translated string
       
   104     """
       
   105     request = check_request()
       
   106     translate = request.localizer.translate
   101     translate = request.localizer.translate
   107     now = gmtime(datetime.utcnow())
   102     now = gmtime(datetime.utcnow())
   108     delta = now - value
   103     delta = now - gmtime(value)
   109     if delta.days > 60:
   104     if delta.days > 60:
   110         return translate(_("%d months ago")) % int(round(delta.days * 1.0 / 30))
   105         return translate(_("%d months ago")) % int(round(delta.days * 1.0 / 30))
   111     elif delta.days > 10:
   106     elif delta.days > 10:
   112         return translate(_("%d weeks ago")) % int(round(delta.days * 1.0 / 7))
   107         return translate(_("%d weeks ago")) % int(round(delta.days * 1.0 / 7))
   113     elif delta.days > 2:
   108     elif delta.days > 2: