Thread: Printing the time that clocks says.

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    Printing the time that clocks says.

    Is there anyway so i can printf the exctly clock (hh//mm//ss) that my Desktop "says"? I think it is very simple as program but i don' t have any idea how to do it...does anyone know?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Try this:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
        time_t t = time(NULL);
        const char* s = ctime(&t);
        printf("%s\n", s); 
        return 0;
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Working!!!!

    Thank you very much...but could you explain the green marks so i can understand completely what i am seeing...thanks again....

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
        time_t t = time(NULL);
        const char* s = ctime(&t);
        printf("%s\n", s); 
        return 0;
    }

  4. #4
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Cool various C time functions

    Code:
    /* Demonstrates the time functions. */
    
    #include <stdio.h>
    #include <time.h>
    
    int main( void )
    {
         time_t start, finish, now;
         struct tm *ptr;
         char *c, buf1[80];
         double duration;
    
         /* Record the time the program starts execution. */
    
         start = time(0);
    
         /* Record the current time, using the alternate method of */
         /* calling time(). */
    
         time(&now);
    
         /* Convert the time_t value into a type tm structure. */
    
         ptr = localtime(&now);
    
         /* Create and display a formatted string containing */
         /* the current time. */
    
         c = asctime(ptr);
         puts(c);
         getc(stdin);
    
         /* Now use the strftime() function to create several different */
         /* formatted versions of the time. */
    
         strftime(buf1, 80, "This is week %U of the year %Y", ptr);
         puts(buf1);
         getc(stdin);
    
         strftime(buf1, 80, "Today is %A, %x", ptr);
         puts(buf1);
         getc(stdin);
    
         strftime(buf1, 80, "It is %M minutes past hour %I.", ptr);
         puts(buf1);
         getc(stdin);
    
         /* Now get the current time and calculate program duration. */
    
         finish = time(0);
         duration = difftime(finish, start);
         printf("\nProgram execution time using time() = %f seconds.", duration);
    
         /* Also display program duration in hundredths of seconds */
         /* using clock(). */
    
         printf("\nProgram execution time using clock() = %ld hundredths of sec.",
             clock());
         return 0;
    }
    There are various time functions in the above program from my textbook. I wont
    be able to answer any questions about them as they are about 4 chapters ahead
    of where I am now.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert a local time to a timezone's bias
    By NightCode in forum Windows Programming
    Replies: 1
    Last Post: 03-09-2010, 11:45 AM
  2. Help fixing a compile time error
    By matt_570 in forum C Programming
    Replies: 2
    Last Post: 09-05-2009, 09:36 PM
  3. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  4. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  5. What is the best way to record a process execution time?
    By hanash in forum Linux Programming
    Replies: 7
    Last Post: 03-15-2006, 07:17 AM