#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    So I was making this clock...


    I was making a clock in C when I saw that my computer doesnt really count the loop incrementations in real time(suprise suprise) So my brother told me to use the <time.h> file.Unfortunately I cant make heads or tails of it :'( Here is my code :
    #include <stdio.h>
    #include <time.h>
    #include <conio.h>

    int main()
    {
    int hours,minutes,seconds;

    printf("\nPlease type in the time in a Hour/Minute/Second format\n");
    scanf("%d/%d/%d",&hours,&minutes,&seconds);
    while(1)
    {
    system("CLS");
    if(seconds<61);
    {
    seconds++;
    }
    if(seconds==60)
    {
    seconds=0;
    minutes++;
    }
    if(minutes==60)
    {
    minutes=0;
    hours++;
    }
    if(hours==24)
    {
    hours=0;
    }

    printf("\nThe time is %d:%d:%d\n",hours,minutes,seconds);


    }
    getch();
    return 0;
    }
    As you might have noticed,I did put in the <time.h> but didnt use a function that belongs to it.. I have seen lots of C documentations but still dont understand how to use <time.h> Thanks for the help :)
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    To start, please use code tags when posting code. HTML strips out leading blanks, thus destroying your code's formatting. Code tags preserve that formatting. Code tags are [code] <place your formatting code here between the tags> [/code]; eg:

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <conio.h>
    
    int main()
    {
        int hours,minutes,seconds;
    
        printf("\nPlease type in the time in a Hour/Minute/Second format\n");
        scanf("%d/%d/%d",&hours,&minutes,&seconds);
        while(1)
        {
            system("CLS");
            if(seconds<61);
            {
                seconds++;
            }
            if(seconds==60)
            {
                seconds=0;
                minutes++;
            }
            if(minutes==60)
            {
                minutes=0;
                hours++;
            }
            if(hours==24)
            {
                hours=0;
            }
    
                printf("\nThe time is %d:%d:%d\n",hours,minutes,seconds);
    
    
        }
        getch();
        return 0;
    }
    It would help a lot if you told us what part of time.h you don't understand. That way we wouldn't waste a lot of our time and yours explaining the basic stuff that you do already understand.

    time.h is based on UNIX time, which is a count of the number of seconds since midnight (ie, start-of-day midnight) 1970 Jan 01. The data type which holds UNIX time is time_t, which is usually typedef'd as a 32-bit integer (my gcc shows it as long), which will incidentally stop working in 2038. You can read more on UNIX time at http://en.wikipedia.org/wiki/UNIX_time and about the Y2038 Problem at http://en.wikipedia.org/wiki/Year_2038_problem.

    You get the system's current UNIX time with time(). Since the value returned is in seconds, you can calculate elapsed time in seconds by subtracting the stop time from the start time, or by using the difftime() function, which would be preferable since what time_t actually is depends on the implementation (possibility that it's not in seconds).

    You convert time_t to usable time and calendar units (eg, year, month, day, weekday, hours, minutes, seconds) through the struct tm. gmtime() converts Greenwich Mean Time (AKA "GMT", AKA "UTC") from time_t to struct tm, while localtime() does the same in local time (the system knows what time zone it's been set as being in). Or you could declare a struct tm, assign time and calendar values to its fields, and convert it to a time_t value with mktime(), which also calculates the correct weekday and day-of-year fields for you.

    Most of the rest of the functions are just utilities. asctime() and ctime() will return a text string representation of the date and time; asctime() takes a struct tm while ctime() takes a time_t and prints out the local date and time. strftime() also converts a struct tm to a text string, but it does so according to a format string that you give it.

    That's most of what anybody uses time.h for. Pretty straight forward. If there's something you still don't understand, please ask specific questions.
    Last edited by dwise1_aol; January 25th, 2013 at 12:06 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by dwise1_aol
    You get the system's current UNIX time with time(). Since the value returned is in seconds, you can calculate elapsed time in seconds by subtracting the stop time from the start time, or by using the difftime() function, which would be preferable since what time_t actually is depends on the implementation (possibility that it's not in seconds).

    You convert time_t to usable time and calendar units (eg, year, month, day, weekday, hours, minutes, seconds) through the struct tm. gmtime() converts Greenwich Mean Time (AKA "GMT", AKA "UTC") from time_t to struct tm, while localtime() does the same in local time (the system knows what time zone it's been set as being in). Or you could declare a struct tm, assign time and calendar values to its fields, and convert it to a time_t value with mktime(), which also calculates the correct weekday and day-of-year fields for you.
    Thanks about the rest
    but here is the part i dont actually undrstand(Most of it :P )
    anyway i would be very grateful if you could suggest a way i could use it in my currrent program with example code...
    Thanks a lot :D
    Also Is it possible to use <b>HTML Code?</b>
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    As far as I know, this forum's software doesn't let you use HTML, though another forum I'm on does allow it along with bbcodes (or dBCodes or whatever they're called here). This forum apparently only allows a form of bbcodes. For the most part, bbcodes are just like HTML except they use square brackets instead of angle (ie, [ ] instead of < >). Bold, italicize, and underline translate directly. I also use url, quote, code, and color tags. There might be a reference of these codes on this forum. But when you see somebody do something and you want to know how he did it (ie, what tags he had used), then click on the Reply button and it should display the tags. That's how I recovered your code's original indentation, with the Reply button. Works almost as well as View Source does for HTML.

    Perhaps you should start another thread asking where to find a list of the tags you can use here.


    As for how to use time.h with your program. Most uses for time.h deal directly with getting the system time and displaying it or finding elapsed time. What you're trying to do is a bit different. For example, in our embedded products we receive both the time and a 1PPS ("one pulse per second") from a GPS receiver and we use that to maintain a clock in our software. Basically, we tie the 1PPS signal to an interrupt and we service the interrupt by incrementing seconds and then rippling that new seconds count through the clock (ie, when seconds reaches 60, it wraps back around to zero and it increments minutes, etc). I would think that what your program needs to do is to somehow determine that a new second has ticked, whereupon you increment the seconds count and ripple that change through the rest of the clock as you have done.

    One thought for simulating a 1PPS would be to call time() and save the time_t value. Then you call it again and again and again. Each time you call time(), you compare it with the previous value. If the current value of time() is the same as the previous, then you are still in the same second. But if the current value is not equal, then you're in a new second and you should increment your seconds variable.

    time.h also provides a clock() function which returns a running count of the system's clock pulses and a macro, CLOCKS_PER_SECOND, which tells you how many clock pulses there are in one second. With that information, you should be able to simulate a 1PPS. Keep in mind, though, that the system clock pulse count is a running count that will overflow periodically and return to zero, so you would need to be able to take that boundary condition into account.


    And, of course, you should always RTFM ("Read The Manual"). For each of these functions, structs, and macros that I've told you about, you need to read the documentation on them. Your compiler should have come with a help file or with man pages, so make use of them. Microsoft's help file seems to have gotten rather spotty and too .NET-centric, so you could Google on MSDN function-name to look it up in the Microsoft Developers' Network (MSDN) library. Or you could Google on man page function-name for the function's UNIX-style manual page (AKA "man page"); many C functions are named the same as their corresponding shell command, or vice versa, so do not choose a man page from section 1 (eg, clock(1) would be a shell command, not a C function, which would be clock(3)).

    All I'm doing is to try to point you in the right direction and to give you some keywords to search on. It's still up to you to follow through.
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,130
    Rep Power
    9398
    Originally Posted by dwise1_aol
    There might be a reference of these codes on this forum.
    It's hard to find but it's linked to in the Site FAQ. The list is actually provided by vBulletin itself.

    Linky

    Highlighting is covered separately.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by dwise1_aol

    As for how to use time.h with your program. Most uses for time.h deal directly with getting the system time and displaying it or finding elapsed time. What you're trying to do is a bit different. For example, in our embedded products we receive both the time and a 1PPS ("one pulse per second") from a GPS receiver and we use that to maintain a clock in our software. Basically, we tie the 1PPS signal to an interrupt and we service the interrupt by incrementing seconds and then rippling that new seconds count through the clock (ie, when seconds reaches 60, it wraps back around to zero and it increments minutes, etc). I would think that what your program needs to do is to somehow determine that a new second has ticked, whereupon you increment the seconds count and ripple that change through the rest of the clock as you have done.

    One thought for simulating a 1PPS would be to call time() and save the time_t value. Then you call it again and again and again. Each time you call time(), you compare it with the previous value. If the current value of time() is the same as the previous, then you are still in the same second. But if the current value is not equal, then you're in a new second and you should increment your seconds variable.

    time.h also provides a clock() function which returns a running count of the system's clock pulses and a macro, CLOCKS_PER_SECOND, which tells you how many clock pulses there are in one second. With that information, you should be able to simulate a 1PPS. Keep in mind, though, that the system clock pulse count is a running count that will overflow periodically and return to zero, so you would need to be able to take that boundary condition into account.


    And, of course, you should always RTFM ("Read The Manual"). For each of these functions, structs, and macros that I've told you about, you need to read the documentation on them. Your compiler should have come with a help file or with man pages, so make use of them. Microsoft's help file seems to have gotten rather spotty and too .NET-centric, so you could Google on MSDN function-name to look it up in the Microsoft Developers' Network (MSDN) library. Or you could Google on man page function-name for the function's UNIX-style manual page (AKA "man page"); many C functions are named the same as their corresponding shell command, or vice versa, so do not choose a man page from section 1 (eg, clock(1) would be a shell command, not a C function, which would be clock(3)).

    All I'm doing is to try to point you in the right direction and to give you some keywords to search on. It's still up to you to follow through.
    I understand what you are trying to say,but is it possible for me to make a clock all by myself,without having to depend on a GPS?
    Thanks :tntworth:
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    Originally Posted by CptFuzzyboots
    I understand what you are trying to say,but is it possible for me to make a clock all by myself,without having to depend on a GPS?
    Thanks :tntworth:
    Uh ... yes, it is. That is what I was just telling you. Since you don't have an actual 1PPS interrupt, simulate one. That is to say, write some code that will increment seconds once per second. For example, write a function that doesn't return until it has detected the transition to the next second. Better, it should return one value (eg, 1) when it's a new second and another for when there's an error (eg, -1). Or something like that.

IMN logo majestic logo threadwatch logo seochat tools logo