C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 14th, 2004, 08:33 PM
Evoltix Evoltix is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Nebraska
Posts: 14 Evoltix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Evoltix Send a message via Yahoo to Evoltix
Finding difference between two times. Anybody?

At the end of my program, I want to display the difference between two times. The times are written in hours, mintues
and seconds. Now, I want to manually write the code to find the difference between the two times, no shortcuts or built
functions.

Example:
First Time: 12:34:52 P.M.
Second Time: 8:17:32 P.M.

I'm trying to write code to find the difference between any given pair of times A.M./P.M. They can be any combonation
of A.M./P.M. Please help. Thanks.

Reply With Quote
  #2  
Old November 21st, 2004, 02:53 PM
code_geek code_geek is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 code_geek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i am working on this program
what it does is it splits the times apart into hours, minutes, seconds and then uses the am/pm info to convert it to military time (adding 12 to the hours if necessary)
then, i assume (haven't had time to write it yet) it is just a matter of simple subtraction
per your requirement, i am not using atoi() or itoa(), i wrote my own str_to_int() and int_to_str() functions. i also wrote my own splitstring() and sub_string() functions. it is hard not to use built-in functions--i would type one in, and then realize i couldn't use it!
as soon as i finish the first draft of my english essay, i will work on finishing up this program. stay tuned!

Reply With Quote
  #3  
Old November 21st, 2004, 04:57 PM
Kernel Mustard Kernel Mustard is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 32 Kernel Mustard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
The first thing that popped into my head was to start with a base time, say 12:00:00 AM, and call that time 0. Given a time figure out how many seconds that is since time 0:

10:32:34 AM = (10*60*60) + (32*60) + 34
or
10:32:34 PM = ((10+12)*60*60) + (32*60) + 34

once you have the number of seconds for each time, subtract the two, and convert back to h:m:s.

eg:

time1 = 10:32:34 PM = ((10+12)*60*60) + (32*60) + 34 = 81154
time2 = 3:03:46 AM = (3*60*60) + (3*60) + 46 = 11026

time1-time2 = 81154 - 11026 = 70128

CONVERT DIFFERENCE TO H:M:S

1) Convert to minutes and seconds
70128 / 60 = 1168 //integer division, number of minutes
70128 % 60 = 48 //modulus, number of seconds left

2) Convert minutes to hours
1168 / 60 = 19 //integer division, number of hours
1168 % 60 = 28 //modulus, remaining minutes

Time difference is then 19 hrs, 28 minutes, 48 seconds.

Might be a better way, but as I said, this is the way that I first thought of.

Kernel Mustard

Reply With Quote
  #4  
Old November 21st, 2004, 06:12 PM
code_geek code_geek is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 code_geek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
wow, i didn't think of using a "time 0"--that way is much better than mine
i may still write mine, just to see if i can get it to work

Reply With Quote
  #5  
Old November 22nd, 2004, 08:56 AM
Kernel Mustard Kernel Mustard is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 32 Kernel Mustard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
Definitley try it your way. Let me know how it goes. I think maybe some combination of the two would be best. Say, first convert to military time according to AM/PM, then figure out the total number of seconds, and continue on. I can't remember how midnight is handled in military time. It's 24 hundred hrs, isn't it? Or does it start at 0?

Reply With Quote
  #6  
Old November 22nd, 2004, 04:39 PM
code_geek code_geek is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 code_geek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
doesn't matter how midnight is handled--i am just adding 12 to the hours if the inputted value is P.M. (so yes, it is 24:00)
also, i don't see why i need to convert it to seconds--just subtract hours, minutes, and seconds separately

Reply With Quote
  #7  
Old November 24th, 2004, 01:02 AM
Evoltix Evoltix is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Nebraska
Posts: 14 Evoltix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Evoltix Send a message via Yahoo to Evoltix
Hey guys,

Yes, Kernel Mustard's approach was very useful in finishing up the end of my program. Thanks for the help.
Good work!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Finding difference between two times.


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap