Hi everyone,

I've been lurking here for a while and absorbing all the information I can find here while learning TI-Basic.

I started making a simple little utility that calculates times and I'm having trouble writing something that will subtract a time duration from a point in time.

I want the program to allow the user to enter a time point (the current time perhaps) in hh:mm:ss and then the user enters a time duration in hh:mm:ss and have the program subtract this duration from that time point and spit out the new time of day. And it needs to take multiple days into account so I could subtract say..72 hours from 12:00 PM

Below is the code I have so far, and it works until you get beyond -1 days. I think it falls apart because that bit of code under the If statement only accounts for 86400(1-day)

I feel like this should be a simple matter of arithmetic but every time I try something new I get weird results and now I'm kind of stumped. Could someone maybe suggest what I'm doing wrong here or what I can I do to make this work? I feel like I missing something very simple here but I can't figure it out.

Below is my code with some comments about what it's doing.


Code:

ClrHome
Disp "24 CLOCK"
Disp " "

Disp "TIME "
Input "HOUR: ",A
Input "MINUTE: ",B
Input "SECOND: ",C

Disp " "
Disp "SUBTRACT TIME"
Input "HOURS: ",D
Input "MINUTES: ",E
Input "SECONDS: ",F

60A+B→B
60B+C→C
60D+E→E
60E+F→F
C-F→G
//Converts both timevalues into total seconds and then subtracts them and stores the value to G

0→H
//Resets H to 0 in case we don't use the If code below

If G<0
Then
86400-abs(G)→Z
iPart(86400/Z)→H
Z→G
End
//If the new time is in the previous day it subtracts those seconds from G and stores day value in H for display below


iPart(G/3600)→I
G-3600*I→G
iPart(G/60)→J
G-60*J→G
//Turns seconds back into hh:mm:ss format for display below

ClrHome
Disp "Days -",H
Disp "HOUR: ",abs(I)
Disp "MINUTE: ",J
Disp "SECOND: ",G
Pause

Code:

ClrHome
Disp "24 HR CLOCK"," "

Disp "TIME "
Input "HOUR: ",A
Input "MINUTE: ",B
Input "SECOND: ",C

Disp " "
Disp "SUBTRACT TIME"
Input "HOURS: ",D
Input "MINUTES: ",E
Input "SECONDS: ",F

E+((C-F)<0)->E
D+((B-E)<0)->D

A-D+24((A-D)<0)->A
B-E+60((B-E)<0)->B
C-F+60((C-F)<0)->C

ClrHome

Disp "HOUR: ",A
Disp "MINUTE: ",B
Disp "SECOND: ",C



See if this does what you want. I haven't actually tested it, so it might throw an error.
Thank you man, it does work. But I still kind of have the same issue with calculating the actual days. It's fine for calculating less than 24 hours but beyond that, it kind of falls apart.

For example with the code you provided - I enter 01:10:05 for START TIME and then 72:05:10 for SUBTRACT TIME this code shows:

Hour: -47
Minute: 04
Second: 55

But I would like it to instead show

Day: -3
Hour: 01
Minute: 04
Second: 55

So it's really the days elapsed that I'm getting hung up on here.

I did start looking at timeCNV and dbd commands after talking on Discord but I'm not sure that's going to do what I'm trying to do here.

Code:

ClrHome
Disp "24 HR CLOCK"," "

0->G

Disp "TIME "
Input "HOUR: ",A
Input "MINUTE: ",B
Input "SECOND: ",C

Disp " "
Disp "SUBTRACT TIME"
Input "HOURS: ",D
Input "MINUTES: ",E
Input "SECONDS: ",F

E+((C-F)<0)->E
D+((B-E)<0)->D
int(D/24)-((A-remainder(D,24))<0)->G

A-D+24((A-D)<0)->A
B-E+60((B-E)<0)->B
C-F+60((C-F)<0)->C

ClrHome

Disp "DAYS: ",G
Disp "HOUR: ",A
Disp "MINUTE: ",B
Disp "SECOND: ",C



Try this. In case you're wondering what math is going on here, the first chunk adds 1 to the subtracting values if the denomination below it will loop behind zero seconds when it is subtracted. The second chunk handles the looping back, so you won't get numbers like 26 hours or -44 minutes.
silentdaemon wrote:
I did start looking at timeCNV and dbd commands after talking on Discord but I'm not sure that's going to do what I'm trying to do here.
timeCnv( most certainly does.

mr womp womp wrote:
[mr womp womp] also fix this guy's code
[mr womp womp] it should be like 3 lines max
Let's see if we can make this happen!

First thing out of the window: pretty inputs and outputs here. Who needs them?

69 bytes:

Code:
Prompt |LA,|LB
|LB
For(A,0,~1,~1
T+Ans(4)+60Ans(3)+60^^2(Ans(2)+24Ans(1->T
|LA
End
Disp cos(angle(T))timeCnv(abs(T

45 bytes:

Code:
Prompt |LA,|LB
{86400,60^^2,60,1
sum(Ans|LA)-sum(Ans|LB
Pause cos(angle(Ans))timeCnv(abs(Ans

40 bytes:

Code:
Prompt |LA,|LB
sum({5!6!,60^^2,60,1}(|LA-|LB
Pause tanh(|E9Ans)timeCnv(abs(Ans

There it is: three lines. I hope you're happy womp Razz

This expects inputs and outputs in the form of lists in the order of {days,hours,minutes,seconds}.


Line-by-line breakdown:

Code:
Prompt |LA,|LB
Get inputs.

Code:
sum({5!6!,60^^2,60,1}(|LA-|LB
Convert the dates to seconds and subtract them. (|LA-|LB subtracts the two lists. {5!6!,60^^2,60,1} are the second multipliers in a day, hour, minute, and second. Finally, we just add all the values in the lists with a sum(.

Code:
Pause tanh(|E9Ans)timeCnv(abs(Ans
Calculate day values and display them. tanh(|E9Ans) is the sign function because timeCnv( doesn't like negative numbers, which is also why the absolute value is taken. most of the work here is done by timeCnv.


This is by no means optimal, but it'll get you started, at least.

Credit to womp for noticing 5!6!.
LogicalJoe wrote:
<very long post>

Heh, I'm very satisfied Very Happy
Take away the Pause, its a waste of bytes, last line will be displayed if its value-returning Wink
Also, when using Prompt, you can do

Code:
Prompt A,B

If the user inputs lists, they will be stored to |LA and |LB.
mr womp womp wrote:
Take away the Pause, its a waste of bytes, last line will be displayed if its value-returning
Good catch.
mr womp womp wrote:
Also, when using Prompt, you can do

Code:
Prompt A,B

If the user inputs lists, they will be stored to |LA and |LB.[/code]
I did not know this one.

37 bytes:

Code:
Prompt A,B
sum({5!6!,60^^2,60,1}(|LA-|LB
tanh(|E9Ans)timeCnv(abs(Ans
Now it returns the values in Ans rather than displaying them. If it is the last line of a program it will display the values in place of the "Done".

Probably still not optimal.
Lol well, I feel kind of dumb seeing how easy it was to turn all of that into three lines of code. I kind of liked my pretty inputs and outputs though Smile

What format is it looking for at the prompt? I tried entering time values a few different ways at prompt A and I keep getting ERROR: DATA TYPE - Wrong value or variable type entered. Goto takes me to the comma between LA and LB
It expects lists in the form of {DD,HH,MM,SS
So for example, you could do A={3,2,0,0 and B={2,5,0,0
and a list would be returned containing {0,21,0,0
Which means that at 2AM on day 3, 21 hours have elapsed since 5AM on day 2.
Got it, the thing is I'm trying to get the new time after subtracting B from A not the elapsed time

So if I enter A {00,01,10,05} and B {00,72,05,10} I want it to show something like {3,1,4,55} which is 01:04:55 3 days ago

edit - the resulting time should be 1:04:55 not 3:04:55
Sam wrote:

Code:

ClrHome
Disp "24 HR CLOCK"," "

0->G

Disp "TIME "
Input "HOUR: ",A
Input "MINUTE: ",B
Input "SECOND: ",C

Disp " "
Disp "SUBTRACT TIME"
Input "HOURS: ",D
Input "MINUTES: ",E
Input "SECONDS: ",F

E+((C-F)<0)->E
D+((B-E)<0)->D
int(D/24)-((A-remainder(D,24))<0)->G

A-D+24((A-D)<0)->A
B-E+60((B-E)<0)->B
C-F+60((C-F)<0)->C

ClrHome

Disp "DAYS: ",G
Disp "HOUR: ",A
Disp "MINUTE: ",B
Disp "SECOND: ",C



Try this. In case you're wondering what math is going on here, the first chunk adds 1 to the subtracting values if the denomination below it will loop behind zero seconds when it is subtracted. The second chunk handles the looping back, so you won't get numbers like 26 hours or -44 minutes.


This is really close but for some reason I'm still getting a negative hour value. So if I enter 01:10:05 for start and 72:05:10 for sub it returns
3 days
-47 hours
4 minutes
55 seconds

everything is right except the hours which would be (1)
I don't feel like golfing this down all night, so take it now before I do:

53 bytes:
Code:
6!5!->A
Prompt A,B
sum({A,60^^2,60,1}(|LA-|LB
timeCnv(abs(Ans-(0>Ans)2(A-remainder(abs(Ans),A

Do whatever you want with it womp :P
LogicalJoe wrote:
I don't feel like golfing this down all night, so take it now before I do:

53 bytes:
Code:
6!5!->A
Prompt A,B
sum({A,60^^2,60,1}(|LA-|LB
timeCnv(abs(Ans-(0>Ans)2(A-remainder(abs(Ans),A

Do whatever you want with it womp Razz


This did exactly what I was looking for, thank you LogicalJoe!
LogicalJoe wrote:
I don't feel like golfing this down all night, so take it now before I do:

53 bytes:
Code:
6!5!->A
Prompt A,B
sum({A,60^^2,60,1}(|LA-|LB
timeCnv(abs(Ans-(0>Ans)2(A-remainder(abs(Ans),A

Do whatever you want with it womp Razz

No complaints here Razz
I'm still convinced there's a clever way to pull the power of 60 out of the list but my little monkey brain can't figure it out Laughing
I guess on line 3, it would be slightly faster to use Ans instead of A without changing the number of bytes Rolling Eyes
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement