Solved

How do I subtract time?

Posted on 2010-08-18
10
467 Views
Last Modified: 2013-12-14
I inputted some data from a text file such that nHour and nMinute is filled with an integer representing some hour and minute.

The time is represented using the 12h, not 24h, format.  So 1:30pm is reflected by nHour = 1, and nMinute = 30, and there is a string strAMPM containing "AM" or "PM".

My question is I want to calculate how many minutes has elapsed since X, where X is a time in the past BEFORE nHour/nMinute has occurred, but within the same day.  

So let's say nHour = 1, and nMinute = 30, and X = 9:00am.  X may be whatever data type you need it to be but it has to represent some time value that I can modify/change in the future.

The answer to how much time has elapsed should be 270 (representing the 270 minutes difference between 9:00am and 1:30pm.  How do I do this?

I can already envision the pseudo-code of how this would work, but I do not know the C++ functions that I would need to use and how to implement them.  Thus, actual code that I can follow would be much appreciated.
0
Comment
Question by:shaolinfunk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 33465547
The easiest would probably be to calculate the number of minutes since the beginning of the day (for both times), and then subtract those two values to get the difference in minutes.

Calculating the number of minutes is easy : (hour * 60) + min, and if it's "PM", just add (12 * 60).
0
 
LVL 86

Expert Comment

by:jkr
ID: 33465552
Use 'mktime()' (http://www.cplusplus.com/reference/clibrary/ctime/mktime/) to create a 'time_t' value for both times, (which is 'The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970') - you then can subtract these values and use 'gmtime()' (http://www.cplusplus.com/reference/clibrary/ctime/gmtime/) to obtain the amount of hours, minutes etc.
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 33465656
ok i'm interested in learning a new function, the mktime()...i looked at the example but it doesn't show how to create a 'time_t' value from the hour and minute variables that i have, nHour and nMinute...

it only shows year, month, and day.  how would i convert nHour and nMinute into a time_t value?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 33465749
You can do that like
time_t t1;
time_t t2;
struct tm tm1;
struct tm tm2;

memset(&tm1,0,sizeof(struct tm));
memset(&tm2,0,sizeof(struct tm));

tm1.tm_sec = 0; //Seconds after minute (0 – 59)
tm1.tm_min = 30; //Minutes after hour (0 – 59)
tm1.tm_hour = 12; //Hours since midnight (0 – 23)
tm1.tm_mday = 1; //Day of month (1 – 31)
tm1.tm_mon = 12; // Month (0 – 11; January = 0)
tm1.tm_year = 104;//Year (current year minus 1900)

tm2.tm_sec = 0; //Seconds after minute (0 – 59)
tm2.tm_min = 30; //Minutes after hour (0 – 59)
tm2.tm_hour = 12; //Hours since midnight (0 – 23)
tm2.tm_mday = 1; //Day of month (1 – 31)
tm2.tm_mon = 7; // Month (0 – 11; January = 0)
tm2.tm_year = 102;//Year (current year minus 1900)

t1 = mktime(&tm1);
t2 = mktime(&tm2);

double secs = difftime(t1,t2); // get difference in seconds - divide by 60 to get the minutes

Open in new window

0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 33478713
ok, can you explain to me what this line does? and why we need it?

memset(&tm1,0,sizeof(struct tm));
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 33478752
also, why are we declaring structs here?

struct tm tm1;

i'm confused as to syntax...usually i declare a struct like this..

struct tm
{
  //stuff inside
}

what does struct tm tm1;  do?
0
 
LVL 86

Expert Comment

by:jkr
ID: 33478788
'struct tm' is a C construct and in C it's perfectly legal to use them like that (hence, in C++ also). 'struct tm tm1;' does not declare a struct, it just instantiates 't1'.
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 33480384
ok, thanks for explaining...how about my other question before that (see earlier post)?

ok, can you explain to me what this line does? and why we need it?

memset(&tm1,0,sizeof(struct tm));
0
 
LVL 86

Expert Comment

by:jkr
ID: 33480423
Well, it is easier zo zero-out a struct in one call rather than setting each member to '0' manually, that's all.
0
 
LVL 1

Author Closing Comment

by:shaolinfunk
ID: 33487317
ok thanks!
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question