sivabudh
sivabudh

Reputation: 32635

How to use Python to calculate time

I want to write python script that acts as a time calculator.

For example:

Suppose the time is now 13:05:00

I want to add 1 hour, 23 minutes, and 10 seconds to it.

and I want to print the answer out.

How do I do this in Python?

What if date is also involved?

Upvotes: 13

Views: 80723

Answers (5)

Gavriel Cohen
Gavriel Cohen

Reputation: 4623

For calculating dates and times there are several options but I will write the simple way:

import datetime
import dateutil.relativedelta

# current time
date_and_time = datetime.datetime.now()
date_only = date.today()
time_only = datetime.datetime.now().time()

# calculate date and time
result = date_and_time - datetime.timedelta(hours=26, minutes=25, seconds=10)

# calculate dates: years (-/+)
result = date_only - dateutil.relativedelta.relativedelta(years=10)

# months
result = date_only - dateutil.relativedelta.relativedelta(months=10)

# days
result = date_only - dateutil.relativedelta.relativedelta(days=10)

# calculate time 
result = date_and_time - datetime.timedelta(hours=26, minutes=25, seconds=10)
result.time()

Hope it helps

Upvotes: 8

Roger Pate
Roger Pate

Reputation:

datetime.timedelta is designed for fixed time differences (e.g. 1 day is fixed, 1 month is not).

>>> import datetime
>>> t = datetime.time(13, 5)
>>> print t
13:05:00
>>> now = datetime.datetime.now()
>>> print now
2009-11-17 13:03:02.227375
>>> print now + datetime.timedelta(hours=1, minutes=23, seconds=10)
2009-11-17 14:26:12.227375

Note that it doesn't make sense to do addition on just a time (but you can combine a date and a time into a datetime object, use that, and then get the time). DST is the major culprit. For example, 12:01am + 5 hours could be 4:01am, 5:01am, or 6:01am on different days.

Upvotes: 17

Alexandru
Alexandru

Reputation: 25800

Look into datetime.timedelta.

Example
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
...                          minutes=50, seconds=600)  # adds up to 365 days
>>> year == another_year
True
>>> ten_years = 10 * year
>>> ten_years, ten_years.days // 365
(datetime.timedelta(3650), 10)
>>> nine_years = ten_years - year
>>> nine_years, nine_years.days // 365
(datetime.timedelta(3285), 9)
>>> three_years = nine_years // 3;
>>> three_years, three_years.days // 365
(datetime.timedelta(1095), 3)
>>> abs(three_years - ten_years) == 2 * three_years + year
True

Upvotes: 4

retracile
retracile

Reputation: 12339

Look at mx.DateTime, and DateTimeDelta in particular.

import mx.DateTime
d = mx.DateTime.DateTimeDelta(0, 1, 23, 10)
x = mx.DateTime.now() + d
x.strftime()

Keep in mind that time is actually a rather complicated thing to work with. Leap years and leap seconds are just the beginning...

Upvotes: 2

Tim Snyder
Tim Snyder

Reputation: 113

The datetime class in python will provide everything you need. It supports addition, subtraction and many other operations.

http://docs.python.org/library/datetime.html

Upvotes: 0

Related Questions