473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

A little help with time calculations

I am trying to make a very simple program and am very new to the whole
programming thing. my program is supposed to ask a user for any time in
the for format XX:XX:XX and then ask for a time corrrection to add or
subtract to this. my only problem is that once the user inputs the time
and the correction its adding it like it was 100 not to 60 any help?

Oct 18 '05 #1
5 3612
iminal wrote:
I am trying to make a very simple program and am very new to the whole
programming thing. my program is supposed to ask a user for any time in
the for format XX:XX:XX and then ask for a time corrrection to add or
subtract to this. my only problem is that once the user inputs the time
and the correction its adding it like it was 100 not to 60 any help?


Without code, nobody can help you.

Regards,

Diez
Oct 18 '05 #2
iminal wrote:
I am trying to make a very simple program and am very new to the whole
programming thing. my program is supposed to ask a user for any time in
the for format XX:XX:XX and then ask for a time corrrection to add or
subtract to this. my only problem is that once the user inputs the time
and the correction its adding it like it was 100 not to 60 any help?

If you're new to programming you may not yet have realised that many
problems come down to finding appropriate representations for things.

Since you want to do arithmetic on times, why not store them as seconds?
Then you just need to work out how to convert times to seconds, and
seconds to times - Python's arithmetic will do the rest.

See if you can make anything of these functions (which I haven't tested,
so you're allowed to complain if they don't work ;-):

def timetosecs(s):
hms = s.split(":") # [hh, mm, ss]
secs = 0
for t in hms:
secs = secs * 60 + int(t)
return secs

def secstotime(secs):
hms = []
while secs:
hms.append(str(secs % 60))
secs = secs // 60
return ":".join(hms)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 18 '05 #3
what i have so far is :

# Get values needed to make time calculations
CT = input("input your chronometer time (ex. 07:21:46): ")
CE = input("input your chronometer correction (ex. 00:01:32): ")
CEfastslow = raw_input("is your chronometer correction fast or
slow: ")

#decide either to subtract or add CE from/to CT
if CEfastslow == "fast":
CEfastslow = CT - CE
if CEfastslow == "slow":
CEfastslow = CT + CE

but this just doesnt deal with the numbers in time format its acting
like they are just regualr integers adding them up like regular numbers

i am trying to figure out what u posted and it seems a little
complicated im trying to add it in somehow and figure out exactly what
its doing but still looking for a little easier of a way

thanks so far

Oct 18 '05 #4
iminal wrote:
what i have so far is :

# Get values needed to make time calculations
CT = input("input your chronometer time (ex. 07:21:46): ")
CE = input("input your chronometer correction (ex. 00:01:32): ")
CEfastslow = raw_input("is your chronometer correction fast or
slow: ")

#decide either to subtract or add CE from/to CT
if CEfastslow == "fast":
CEfastslow = CT - CE
if CEfastslow == "slow":
CEfastslow = CT + CE

but this just doesnt deal with the numbers in time format its acting
like they are just regualr integers adding them up like regular numbers
Well, how is the interpreter supposed to know that they are times?
Remember that the Python language doesn't have times as a basic data
type, and input(...) treats what you enter as Python data (unlike
raw_input()).
i am trying to figure out what u posted and it seems a little
complicated im trying to add it in somehow and figure out exactly what
its doing but still looking for a little easier of a way
Well, the code I posted was untested, and I find two things wrong with
it straight away: Firstly, it won't include leading zeros when
converting seconds to a time, and secondly it puts the hours, minutes
and seconds in the wrong order.
thanks so far

The idea, though, is to read strings lime "07:20:44" and convert them
into something that Python *can* do arithmetic on. I defined a function,
timetosecs, that would let you do this.

So your program should look something like:

# Put function definitions here ...
CT = raw_input("input your chronometer time (ex. 07:21:46): ")
CE = raw_input("input your chronometer correction (ex. 00:01:32): ")
CEfastslow = raw_input("is your chronometer correction fast or slow: ")

Tsecs = timetosecs(CT)
Esecs = timetosecs(CE)

#decide either to subtract or add CE from/to CT
if CEfastslow == "fast":
CEfastslow = Tsecs - Esecs
if CEfastslow == "slow":
CEfastslow = Tsecs + Esecs

print "New time:", secstotime(CEfastslow)

Hope this gets you a bit closer to a solution.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 19 '05 #5
thanks

Oct 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: wireless200 | last post by:
I've got a table with some datetime fields in it. One field (call it field 1) is of the form mm/dd/yyyy and the other two (fields 2 and 3) are in the form of hh:mm:ss:xx where xx is hundreths of...
11
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m...
10
by: Marc Pelletier | last post by:
Hello, I am writing an application that does some simple astronomical calculations. One of the variables I need is the number of hours passed in this year. I've written the following function ...
1
by: rgarf | last post by:
I have a web service that does some long calculations, I want to know if there is a way for the web service to return progress strings while it's doing the calculations, so the client can display...
5
by: cvisal | last post by:
Hi all Im working on productivity calculations (Time calculations) and need some help in coding. Database Tool:MS-Access 2003. The general operator punch-in time is 5:30 AM and the punch-out...
4
by: yawnmoth | last post by:
Is it possible to send http requests with curl but not have curl wait for the response? The reason I ask is because I'd like to code a web app that can sorta start time consuming processes...
9
by: Mohd Al Junaibi | last post by:
Hello all, my first post here...hope it goes well. I'm currently working on stored procedure where I translated some reporting language into T-SQL The logic: I have a group of tables...
35
by: bala.pandu | last post by:
Hello Everyone, When i am assigning a float value and try to print the same, i found the value stored is not the exact value which i gave. int main() { float f1; printf("Enter a float value...
16
by: shreyansghia | last post by:
Hello, I am a new member and am glad to a part of this forum. I am facing a slight problem while doing time calculations in MS access. I ve prepared a time sheet for my workers. Their regular...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.