3 #======================================================================
10 # $Id: Time.py,v 1.3 2002-07-12 08:02:46 acampi Exp $
13 # (C) COPYRIGHT 2001, Eric Busboom <eric@softwarestudio.org>
14 # (C) COPYRIGHT 2001, Patrick Lewis <plewis@inetarena.com>
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of either:
19 # The LGPL as published by the Free Software Foundation, version
20 # 2.1, available at: http://www.fsf.org/copyleft/lesser.html
24 # The Mozilla Public License Version 1.0. You may obtain a copy of
25 # the License at http://www.mozilla.org/MPL/
26 #======================================================================
28 from LibicalWrap import *
29 from Property import Property
30 from types import DictType, StringType, IntType, FloatType
31 from Duration import Duration
33 UTC = icaltimezone_get_utc_timezone()
36 """ Represent iCalendar DATE, TIME and DATE-TIME """
37 def __init__(self, arg, name="DTSTART", zone=None):
39 Create a new Time from a string or number of seconds past the
42 Time("19970325T123000Z") Construct from an iCalendar string
43 Time(8349873494) Construct from seconds past POSIX epoch
46 e1=icalerror_supress("MALFORMEDDATA")
47 e2=icalerror_supress("BADARG")
49 if isinstance(arg, DictType):
50 # Dictionary -- used for creating from Component
51 self.tt = icaltime_from_string(arg['value'])
52 Property.__init__(self, ref=arg['ref'])
54 if isinstance(arg, StringType):
55 # Create from an iCal string
56 self.tt = icaltime_from_string(arg)
57 elif isinstance(arg, IntType) or \
58 isinstance(arg, FloatType):
59 # Create from seconds past the POSIX epoch
61 self.tt = icaltime_from_timet_with_zone(int(arg),0,icaltimezone_get_builtin_timezone(zone))
63 self.tt = icaltime_from_timet_with_zone(int(arg),0,icaltimezone_get_utc_timezone())
64 elif isinstance(arg, Time):
68 self.tt = icaltime_null_time()
70 Property.__init__(self,type=name)
72 icalerror_restore("MALFORMEDDATA",e1)
73 icalerror_restore("BADARG",e2)
75 if icaltime_is_null_time(self.tt):
76 raise Property.ConstructorFailedError("Failed to construct a Time")
80 except Property.UpdateFailedError:
81 raise Property.ConstructorFailedError("Failed to construct a Time")
83 def _update_value(self):
85 self.value(icaltime_as_ical_string(self.tt),"DATE-TIME")
88 " Return true if this is a valid time "
89 return not icaltime_is_null_time(self.tt)
91 def utc_seconds(self,v=None):
92 """ Return or set time in seconds past POSIX epoch"""
93 tz = icaltimezone_get_builtin_timezone(self.timezone())
95 self.tt = icaltime_from_timet_with_zone(v,0,tz)
98 return icaltime_as_timet_with_zone(self.tt, tz)
101 """ Return a boolean indicating if time is in UTC """
102 return icaltime_is_utc(self.tt)
105 """ Return a boolean indicating if time is actually a date """
106 return icaltime_is_date(self.tt)
108 def timezone(self,v=None):
109 """ Return, set (if none) or alter the timezone for this time """
111 origtz = icaltime_get_tzid(self.tt)
114 assert(isinstance(v,StringType) )
116 tz = icaltimezone_get_utc_timezone()
119 tz = icaltimezone_get_builtin_timezone(v)
122 self.tt = icaltime_set_timezone(self.tt, tz)
124 self.tt = icaltime_convert_to_zone(self.tt,tz)
126 if (icaltime_get_tzid(self.tt) != "UTC"):
127 self['TZID'] = icaltime_get_tzid(self.tt)
130 return icaltime_get_tzid(self.tt)
133 self.tt = icaltime_normalize(self.tt)
135 def __second_property(self,v=None):
136 """ Get or set the seconds component of this time """
140 return self.tt.second
141 second = property(__second_property, __second_property)
143 def __minute_property(self,v=None):
144 """ Get or set the minute component of this time """
148 return self.tt.minute
149 minute = property(__minute_property, __minute_property)
151 def __hour_property(self,v=None):
152 """ Get or set the hour component of this time """
157 hour = property(__hour_property, __hour_property)
159 def __day_property(self,v=None):
160 """ Get or set the month day component of this time """
165 day = property(__day_property, __day_property)
167 def __month_property(self,v=None):
168 """ Get or set the month component of this time. January is month 1 """
173 month = property(__month_property, __month_property)
175 def __year_property(self,v=None):
176 """ Get or set the year component of this time """
181 year = property(__year_property, __year_property)
184 def __cmp__(self,other):
187 return cmp(self.utc_seconds(),None)
189 return cmp(self.utc_seconds(),other.utc_seconds())
194 other = Duration(o,"DURATION")
196 if not other.valid():
197 return Duration(0,"DURATION")
199 print self.utc_seconds(), other.seconds()
200 seconds = self.utc_seconds() + other.seconds()
202 new = Time(seconds,self.name(),self.timezone())
207 return self.__add__(o)
213 if isinstance(o,Time):
214 # Subtract a time from this time and return a duration
215 seconds = self.utc_seconds() - other.utc_seconds()
216 return Duration(seconds)
217 elif isinstance(o,Duration):
218 # Subtract a duration from this time and return a time
220 if(not other.valid()):
223 seconds = self.utc_seconds() - other.seconds()
226 raise TypeError, "subtraction with Time reqires Time or Duration"