[Tutor] Time - sum/difference

Jonas Melian jonasmg at SoftHome.net
Fri Oct 21 10:24:45 CEST 2005


I wanted to sum two time values:

-02:30
+01:00
--------------
-01:30

I found one solution:
 >>> time_local = dt.time(2,30)
 >>> time_str = str(time_local).split(':')

Now I'll can get the first value, convert to integer and sum it.

Kent Johnson wrote:

>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
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>  
>



More information about the Tutor mailing list