Results 1 to 2 of 2

Thread: Time code and timer

  1. #1
    Senior Member
    Join Date
    Feb 2005
    Posts
    149

    Time code and timer

    Hi, here is some useful code, the first one is the current date and time, and the second one is a cool little timer.
    /*Date and time*/
    #include <stdio.h>
    #include <time.h>

    int main ()
    {
    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    printf ( "Current date and time are: %s", asctime (timeinfo) );
    getch();

    return 0;
    }

    On this next code you can change the 10 in the for loop to whatever number you want the counter to start.:
    /* Timer decrements from 10 to 1!*/
    #include <stdio.h>
    #include <time.h>

    void wait ( int seconds )
    {
    clock_t endwait;
    endwait = clock () + seconds * CLK_TCK ;
    while (clock() < endwait) {}
    }

    int main ()
    {
    int n;
    printf ("Starting countdown...\n");
    for (n=10; n>0; n--)
    {
    printf ("%d\n",n);
    wait (1);
    }
    printf ("Blast off!!! NASA we have lift off!!!\n");
    return 0;
    }

  2. #2
    Senior Member
    Join Date
    Feb 2005
    Posts
    149
    Ooops, forgot to specify that the code is in the C language. But im sure you knew.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •