How to Compute Time in C language using time.h?

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 [b]in C language:[/b]

[b]
If an employee input a data like this:[/b]
--------------------------------------------------
Enter Time-in: [b]08:00[/b]
Enter Time-out: [b]17:01[/b]
Total No. of Working Hours: [b]8 hours[/b]
--------------------------------------------------

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

[b]Thanks a lot....[/b]

Comments

  • For example, this way:

    #include
    #include
    #include
    #include
    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]='';
    strncpy(sm,stb+3,2);sm[2]='';
    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]='';
    strncpy(sm,ste+3,2);sm[2]='';
    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
    ",qh,qm);
    }
  • 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!
  • 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:
    ---------------------------------------
    [b]why did you add +3 on stb?[/b]

    [b][color=Green]strncpy(sm,stb+3,2);[/color] [/b]
    ---------------------------------------

    Second Question:
    ---------------------------------------
    [b]and what is this for?[/b]

    [b][color=Green]sm[2]='';[/color] [/b]
    ---------------------------------------


    [b]Thank you very much Sir![/b]
  • This post has been deleted.
  • This post has been deleted.
  • [b]strncpy(sm,stb+3,2);[/b] is because of this time format:
    HH:MM
    , so if we count from 0, MM begins from 3-rd symbol.
    [b]sm[2]='';[/b] is because functions like atoi(...) work with null-terminated strings.
  • [b]Sir Skaa7,

    Thank you very much for your kind reply! I really appreciated it![/b]

    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:[color=Red]15[/color]
    Enter Time-out: 17:00
    Total No. of Working Hours: [b][color=Green]7:00 hours[/color][/b]

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

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

    [b]Thank you very much again Sir![/b]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories