Back to index

plone3  3.1.7
Public Member Functions | Public Attributes | Static Public Attributes
icalendar.prop.vDuration Class Reference

List of all members.

Public Member Functions

def __init__
def ical
def from_ical
def __str__

Public Attributes

 td
 params

Static Public Attributes

tuple from_ical = staticmethod(from_ical)

Detailed Description

Subclass of timedelta that renders itself in the iCalendar DURATION format.

>>> vDuration(timedelta(11)).ical()
'P11D'
>>> vDuration(timedelta(-14)).ical()
'-P14D'
>>> vDuration(timedelta(1, 7384)).ical()
'P1DT2H3M4S'
>>> vDuration(timedelta(1, 7380)).ical()
'P1DT2H3M'
>>> vDuration(timedelta(1, 7200)).ical()
'P1DT2H'
>>> vDuration(timedelta(0, 7200)).ical()
'PT2H'
>>> vDuration(timedelta(0, 7384)).ical()
'PT2H3M4S'
>>> vDuration(timedelta(0, 184)).ical()
'PT3M4S'
>>> vDuration(timedelta(0, 22)).ical()
'PT22S'
>>> vDuration(timedelta(0, 3622)).ical()
'PT1H0M22S'

How does the parsing work?
>>> vDuration.from_ical('PT1H0M22S')
datetime.timedelta(0, 3622)

>>> vDuration.from_ical('kox')
Traceback (most recent call last):
    ...
ValueError: Invalid iCalendar duration: kox

>>> vDuration.from_ical('-P14D')
datetime.timedelta(-14)

>>> vDuration(11)
Traceback (most recent call last):
    ...
ValueError: Value MUST be a timedelta instance

Definition at line 376 of file prop.py.


Constructor & Destructor Documentation

def icalendar.prop.vDuration.__init__ (   self,
  td 
)

Definition at line 419 of file prop.py.

00419 
00420     def __init__(self, td):
00421         if not isinstance(td, timedelta):
00422             raise ValueError('Value MUST be a timedelta instance')
00423         self.td = td
00424         self.params = Parameters()


Member Function Documentation

Definition at line 467 of file prop.py.

00467 
00468     def __str__(self):
00469         return self.ical()
00470 
00471 

Here is the call graph for this function:

Parses the data format from ical text format.

Definition at line 446 of file prop.py.

00446 
00447     def from_ical(ical):
00448         """
00449         Parses the data format from ical text format.
00450         """
00451         try:
00452             match = DURATION_REGEX.match(ical)
00453             sign, weeks, days, hours, minutes, seconds = match.groups()
00454             if weeks:
00455                 value = timedelta(weeks=int(weeks))
00456             else:
00457                 value = timedelta(days=int(days or 0),
00458                                   hours=int(hours or 0),
00459                                   minutes=int(minutes or 0),
00460                                   seconds=int(seconds or 0))
00461             if sign == '-':
00462                 value = -value
00463             return value
00464         except:
            raise ValueError('Invalid iCalendar duration: %s' % ical)

Definition at line 425 of file prop.py.

00425 
00426     def ical(self):
00427         sign = ""
00428         if self.td.days < 0:
00429             sign = "-"
00430         timepart = ""
00431         if self.td.seconds:
00432             timepart = "T"
00433             hours = self.td.seconds // 3600
00434             minutes = self.td.seconds % 3600 // 60
00435             seconds = self.td.seconds % 60
00436             if hours:
00437                 timepart += "%dH" % hours
00438             if minutes or (hours and seconds):
00439                 timepart += "%dM" % minutes
00440             if seconds:
00441                 timepart += "%dS" % seconds
00442         if self.td.days == 0 and timepart:
00443             return "%sP%s" % (sign, timepart)
00444         else:
00445             return "%sP%dD%s" % (sign, abs(self.td.days), timepart)

Here is the caller graph for this function:


Member Data Documentation

tuple icalendar.prop.vDuration.from_ical = staticmethod(from_ical) [static]

Definition at line 465 of file prop.py.

Definition at line 423 of file prop.py.

Definition at line 422 of file prop.py.


The documentation for this class was generated from the following file: