C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to Compute Time in C language using time.h? Posted by madmhan84 on 31 Mar 2011 at 8:50 PM
Good day Sir/Ma'am,

Just want to ask if you can help me with this one, and please can you provide me a sample code on how to do this in C language:


If an employee input a data like this:

--------------------------------------------------
Enter Time-in: 08:00
Enter Time-out: 17:01
Total No. of Working Hours: 8 hours
--------------------------------------------------

How will i do this in C language using time.h?

Thanks a lot....
Report
Re: How to Compute Time in C language using time.h? Posted by skaa7 on 1 Apr 2011 at 8:30 AM
For example, this way:

#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void main()
{
char stb[1025];
char ste[1025];
char sh[3],sm[3];
tm rtb,rte;
time_t ttb,tte,ttr;
long qh,qm;

printf("Enter Time-in: ");scanf("%s",stb);
printf("Enter Time-out: ");scanf("%s",ste);

rtb.tm_year=111;rtb.tm_mon=0;rtb.tm_mday=1;rtb.tm_sec=0;
strncpy(sh,stb,2);sh[2]='\0';
strncpy(sm,stb+3,2);sm[2]='\0';
rtb.tm_hour=atoi(sh);
rtb.tm_min=atoi(sm);

rte.tm_year=111;rte.tm_mon=0;rte.tm_mday=1;rte.tm_sec=0;
strncpy(sh,ste,2);sh[2]='\0';
strncpy(sm,ste+3,2);sm[2]='\0';
rte.tm_hour=atoi(sh);
rte.tm_min=atoi(sm);

ttb=mktime(&rtb);
tte=mktime(&rte);

ttr=difftime(tte,ttb);
qm=ttr/60;
qh=qm/60;
if(qh)
qm=qm%(qh*60);
printf("Total No. of Working Hours: %02d:%02d\n",qh,qm);
}
Report
Re: How to Compute Time in C language using time.h? Posted by madmhan84 on 4 Apr 2011 at 1:18 AM
Sir thanks for your kind reply and code sample

BUT i receive this error when i run your code, this is the Error:

Undefined symbol 'mktime' in module

hope you could help me with this one...

thanks a lot sir!
Report
Re: How to Compute Time in C language using time.h? Posted by madmhan84 on 6 Apr 2011 at 12:02 AM
Sir skaa7,

thanks a lot for your post, its working now, i already found the problem, i use Turbo C++ 3.0 and now its working....

thanks a lot again sir!

By the way sir, i have a question:
First Question:
---------------------------------------
why did you add +3 on stb?

strncpy(sm,stb+3,2);
---------------------------------------

Second Question:
---------------------------------------
and what is this for?

sm[2]='\0';
---------------------------------------


Thank you very much Sir!
Report
Re: How to Compute Time in C language using time.h? Posted by skaa7 on 6 Apr 2011 at 7:21 AM
strncpy(sm,stb+3,2); is because of this time format:
HH:MM
, so if we count from 0, MM begins from 3-rd symbol.
sm[2]='\0'; is because functions like atoi(...) work with null-terminated strings.
Report
Re: How to Compute Time in C language using time.h? Posted by madmhan84 on 15 Apr 2011 at 9:23 PM
Sir Skaa7,

Thank you very much for your kind reply! I really appreciated it!


Sir i have another problem, the code is working except in the part of minutes like this:

Enter Time-in: 8:00
Enter Time-out: 17:00
Total No. of Working Hours: 9:00 hours

the above is correct but when I Enter a data like this:

Enter Time-in: 8:15
Enter Time-out: 17:00
Total No. of Working Hours: 7:00 hours

which is I think Wrong... it has to be 8:45 hours (9hrs - 15minutes = 8:45 hours)

Hope you can help me on this one too sir...

Thank you very much again Sir!
Report
This post has been deleted. Posted by madmhan84 on 6 Apr 2011 at 12:04 AM
This post has been deleted.
Report
This post has been deleted. Posted by madmhan84 on 6 Apr 2011 at 12:10 AM
This post has been deleted.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.