src/pyams_utils/date.py
changeset 207 c164b59809c8
parent 72 9049384a2bd4
child 243 94a457eeaeaa
child 272 e2a747608ef6
equal deleted inserted replaced
206:5ac9be953a8f 207:c164b59809c8
    15 
    15 
    16 # import standard packages
    16 # import standard packages
    17 from datetime import datetime
    17 from datetime import datetime
    18 
    18 
    19 # import interfaces
    19 # import interfaces
       
    20 from pyams_utils.interfaces.tales import ITALESExtension
       
    21 from zope.dublincore.interfaces import IZopeDublinCore
    20 
    22 
    21 # import packages
    23 # import packages
       
    24 from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
    22 from pyams_utils.request import check_request
    25 from pyams_utils.request import check_request
    23 from pyams_utils.timezone import gmtime, tztime
    26 from pyams_utils.timezone import gmtime, tztime
    24 from zope.datetime import parseDatetimetz
    27 from zope.datetime import parseDatetimetz
       
    28 from zope.interface import Interface
    25 
    29 
    26 from pyams_utils import _
    30 from pyams_utils import _
    27 
    31 
    28 
    32 
    29 def unidate(value):
    33 def unidate(value):
   243                 minutes = int(round(delta.seconds * 1.0 / 60))
   247                 minutes = int(round(delta.seconds * 1.0 / 60))
   244                 if minutes > 2:
   248                 if minutes > 2:
   245                     return translate(_("%d minutes")) % minutes
   249                     return translate(_("%d minutes")) % minutes
   246                 else:
   250                 else:
   247                     return translate(_("%d seconds")) % delta.seconds
   251                     return translate(_("%d seconds")) % delta.seconds
       
   252 
       
   253 
       
   254 @adapter_config(name='timestamp', context=(Interface, Interface, Interface), provides=ITALESExtension)
       
   255 class TimestampTalesAdapter(ContextRequestViewAdapter):
       
   256     """extension:timestamp(context) TALES adapter
       
   257 
       
   258     A PyAMS TALES extension to get timestamp based on last context modification date.
       
   259     """
       
   260 
       
   261     def render(self, context=None):
       
   262         if context is None:
       
   263             context = self.request.context
       
   264         dc = IZopeDublinCore(context, None)
       
   265         if dc is None:
       
   266             return datetime.utcnow().timestamp()
       
   267         else:
       
   268             return dc.modified.timestamp()