formatted 'time' data in calculations

R

Ross

There seems to be no shortage of information around on how to use the
time module, for example to use time.ctime() and push it into strftime
and get something nice out the other side, but I haven't found anything
helpful in going the other way.

That is, given some formatted text describing times - is there something
that makes it easy to calculate time differences, or do I have to index
my way through the string pulling out characters, converting to integers
etc...

Data is formatted:

t1 = 09:12:10
t2 = 11:22:14

I want to calculate tdiff = t2-t1

Any suggestions? (Thanks for anything you can offer)

-Ross
 
S

Stephen Chapman

Here is how I have done adjustments to time in the past. This is mostly
Date related but it may help

today = datetime.date.today()
wkdiff = datetime.timedelta(weeks=1)
daydiff = datetime.timedelta(days=1)
startdate=(today-wkdiff)-daydiff

this will subtract 1 week and 1 day from today.
Stephen
 
D

Diez B. Roggisch

Ross said:
There seems to be no shortage of information around on how to use the
time module, for example to use time.ctime() and push it into strftime
and get something nice out the other side, but I haven't found anything
helpful in going the other way.

That is, given some formatted text describing times - is there something
that makes it easy to calculate time differences, or do I have to index
my way through the string pulling out characters, converting to integers
etc...

Data is formatted:

t1 = 09:12:10
t2 = 11:22:14

I want to calculate tdiff = t2-t1

Any suggestions? (Thanks for anything you can offer)

The datetime module contains everything you need. Look at the
strptime-function that will allow you to parse the above string to an
actual datetime.time-object, and the you can subtract these to yield a
datetime.timedelta-object.

Diez
 
C

Chris Rebert

There seems to be no shortage of information around on how to use the time
module, for example to use time.ctime() and push it into strftime and get
something nice out the other side, but I haven't found anything helpful in
going the other way.

That is, given some formatted text describing times - is there something
that makes it easy to calculate time differences, or do I have to index my
way through the string pulling out characters, converting to integers etc...

Data is formatted:

t1 = 09:12:10
t2 = 11:22:14

I want to calculate tdiff = t2-t1

Any suggestions? (Thanks for anything you can offer)

Use the `datetime` class in the `datetime` module
(http://docs.python.org/library/datetime.html). It has a class method
..strptime() to parse a string into a `datetime` object. You can then
subtract one `datetime` from another to produce a `timedelta` object
representing the difference between them.

Cheers,
Chris
 
J

John Machin

As to a paucity of conversion formatting, there is no magic way to take
everyone's way of putting date and time information in text and convert
it to unambiguous format, in part because there are too many different
and contradictory formats.  When I write dates, I know what I intended;
when I read dates, I guess what the author intended.

Have you read the entire time module document?  If so, which functions
in that module take strings as arguments?



Do you do any work yourself?  Show us your attempts.  This looks like
a trivial exercise.  It seems that for less than four times the effort
of asking your question you might have found the answer.

Perhaps I am being too cranky this morning.

Indeed. Be not cranky at clueless bludgers and cargo-cultists lest
they rise high in the serried ranks of IT management and remember your
name inclusive-or the net-nannies sally forth and wallop thee with a
balloon on a stick.

To the OP: Your Lordship did not specify whether output should be
expressed in hours and a fraction, or in hours, minutes, and seconds.
So that the eminent one may avoid having to make a decision in public,
I humbly submit answers to both possibilities:
.... h, m, s = [a - b for (a, b) in zip(map(int, t2.split(":")), map
(int,t1.split(":")))]
.... if s < 0:
.... s += 60
.... m -= 1
.... if m < 0:
.... m += 60
.... h -= 1
.... return h, m, s
....(0, 58, 59)

HTH,
John
 
R

Ross

Scott said:
As to a paucity of conversion formatting, there is no magic way to take
everyone's way of putting date and time information in text and convert
it to unambiguous format, in part because there are too many different
and contradictory formats. When I write dates, I know what I intended;
when I read dates, I guess what the author intended.

Have you read the entire time module document? If so, which functions
in that module take strings as arguments?


Do you do any work yourself? Show us your attempts. This looks like
a trivial exercise. It seems that for less than four times the effort
of asking your question you might have found the answer.

Perhaps I am being too cranky this morning.
--Scott David Daniels
(e-mail address removed)


Jeeze, you're quite an ass aren't you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top