time.h

This is a discussion on time.h within the C Programming forums, part of the General Programming Boards category; i want to make a program that carries out tasks at defined system time. example - Code: do{ if (time ...

  1. #1
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24

    time.h

    i want to make a program that carries out tasks at defined system time.

    example -
    Code:
          do{
                    if (time <= 2:29:50)
                    {
                            printf("%d seconds left",seconds);
                    } 
                    if (time == 2:30:00)
                    {
                           exit(1);
                     }
             }while (time !=02:30:00);
    // i know this isnt the syntax or even close and prolly dont make any sense just tryin to paint a picture

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    you do not seriously expect us to make the program for you, do you?

    why don't you view its documentation, and then come back and post any problems you might have encountered.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    try posix (real time) timers: timer_create/delete/settime/gettime()... with the absolute time option, it should suit your need (but not necessarily available on your system)
    All other solutions will be approximative (unix timers: setitimer, etc. or all the others functions using timeouts or signals: alarm(), select(), sleep(), nanosleep() [posix too]...)
    Read the doc for details.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by root4 View Post
    try posix (real time) timers: timer_create/delete/settime/gettime()... with the absolute time option, it should suit your need (but not necessarily available on your system)
    All other solutions will be approximative (unix timers: setitimer, etc. or all the others functions using timeouts or signals: alarm(), select(), sleep(), nanosleep() [posix too]...)
    Read the doc for details.
    why would he limit his program to be platform-dependant when he has a perfectly fine header available for accomplishing what he wants (time.h).?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    why would he limit his program to be platform-dependant when he has a perfectly fine header available for accomplishing what he wants (time.h).?
    You perfectly right! time() rules. But if the OP wants a scheduler a bit more useful, I provided some hints, nothing more.

  6. #6
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    all i want to know is how do i get a program to excecute instructions at different time intervals

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Doc. View Post
    all i want to know is how do i get a program to excecute instructions at different time intervals
    Well, you can of course use time() to read the current time, and then calculate the difference between that and the time you actually want to execute at (using for example mktime() to produce a time-value for the target time, and difftime() to get the difference between two time-values). Assuming you only want seconds precision this is fine.

    But if you do some sort of loop calling time() until you get the right answer, you will spend a lot of CPU-time doing absolutely nothing useful. That is OK if:
    1. There is nothing else the processor can do at the time.
    2. You do not care one bit if the processor is busy or not (that is, you don't pay for the electricity, and you do not pay for the new processor to replace this one because it wears out quicker [although it may not make much difference in "wear and tear"]. Or if it's a battery operated machine, that you do not care about battery life.
    3. There is no other person using this computer while you are running this code (so it's not being run on a machine shared between d9ifferent users).

    Unfortunately, the solution that is nicer will depend on the OS you are running.

    A simple solution is to take the current time, then use the OS's sleep/delay functionality to wait for that amount of time to pass. For example, Linux has a "sleep" function that sleeps for the number of seconds you give it. In Windows, there is a very similar function called Sleep() (not capital), that takes a number of milliseconds.

    Most OS's also have a way to say "Wait until the time is ...", but again it is dependant on the OS you are using.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    sleep() takes seconds as it's argument and delay() takes milliseconds

  9. #9
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    THANKS FOR THE REPLIES.

    i tried most of those..

    here is basically what i want - i want a countdown timer to be runnin and at the same time, the user has to enter a pass that will stop the timer.

    <1>if the pass is correct the timer stops

    <2>if the pass is incorrect the program ends

    <3>if the timer reaches zero before the correct pass is entered, the program stops.

    Thanks alot for the replies guys
    Last edited by Doc.; 01-08-2009 at 12:33 AM.

  10. #10
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    in the mean time - how do i print:

    current hour:

    current min:

    current sec:

    gonna try matsp's suggestion about difftime

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You can use multi-threading. One thread countdown and when it ends it notifies the main thread to die, by setting a flag for example. The main thread will be like while(flag) read password.
    Or you can read, sleep, read, sleep. But of course the read has to be done asyncrhoneously so it doesn't block the program to wait for input.
    The countdown can be done with whatever method you want. It is fairly simple.
    If you want to stop the timer and continue from there you need something more complicated than sleep in the second thread. You will probably have to count the time and save it in the second thread.
    Also, you can always countdown and but more "lightly". Like measure time, sleep(500ms), measure time etc etc.

  12. #12
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    But of course the read has to be done asyncrhoneously so it doesn't block the program to wait for input.
    how???

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Doc. View Post
    how???
    Basicaly, you've got to make it read the keyboard and return immediately if there's nothing there. I think the FAQ on "How do I read the keyboard without enter" will be a starting point, but you may have to modify it a bit. How you actually go about reading the keyboard in a way that doesn't block if the user isn't hitting a key will definitely depend on the OS of your system.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. time.h
    By zensatori in forum C Programming
    Replies: 28
    Last Post: 04-15-2007, 06:39 PM
  2. Help with time.h functions please.
    By Ifurita. in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2003, 03:58 AM
  3. time.h and miliseconds question
    By Diamonds in forum C++ Programming
    Replies: 10
    Last Post: 12-16-2002, 07:41 AM
  4. Replies: 2
    Last Post: 10-18-2002, 08:30 AM
  5. a time.h problem
    By Max in forum C Programming
    Replies: 14
    Last Post: 10-15-2002, 02:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21