[Tutor] Time - sum/difference

Kent Johnson kent37 at tds.net
Fri Oct 21 01:45:18 CEST 2005


Jonas Melian wrote:
> I would get the local time of a country, using UTC (Universal Time 
> Coordinated) and DST (Daylight SavingTime) of that country.
> 
> An example, utc time -02:30 and dst +1 :
> 
> country_utc = datetime.time(2,30)
> isUTCNegative = True
> dst = datetime.time(1,0)
> 
> Now I would the difference of both times.
> -02:30 + 01:00 -> -01:30
> 
> Is possible get sum/difference of time values? How?

I'm not exactly sure what you are looking for, but you can subtract datetime.datetime instances. If you are trying to find the difference between local time and utc this is one way:

 >>> import datetime as dt
 >>> dt.datetime.now()
datetime.datetime(2005, 10, 20, 19, 41, 30, 393000)
 >>> dt.datetime.utcnow()
datetime.datetime(2005, 10, 20, 23, 41, 52, 195000)
 >>> dt.datetime.utcnow()-dt.datetime.now()
datetime.timedelta(0, 14400)

though there is a race condition here that might give you an error of a millisecond sometimes.

Kent



More information about the Tutor mailing list