+ Reply to Thread
Results 1 to 3 of 3

Thread: Displaying Current Time and Date

  1. #1
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Displaying Current Time and Date

    The short version looks like this.
    Code:
    #include <stdio.h>
    #include <time.h>
     
    int main(void)
    {
       time_t now;
       if ( time(&now) != (time_t)(-1) )
       {
          struct tm *mytime = localtime(&now);
          if ( mytime )
          {
             char buffer [ 32 ];
             if ( strftime(buffer, sizeof buffer, "%X", mytime) )
             {
                printf("buffer = \"%s\"\n", buffer);
             }
          }
       }
       return 0;
    }
     
    /* my output
    buffer = "09:09:26"
    */
    Adding a few comments...
    Code:
    #include <stdio.h>
    #include <time.h>
     
    int main(void)
    {
       time_t now;
       /*
        * The next line attempts to get the current time.
        * The result is stored in 'now'.
        * If the call to time() fails, it returns (time_t)(-1); verify success
        * before continuing on.
        */
       if ( time(&now) != (time_t)(-1) )
       {
          /*
           * Declare a variable 'mytime' and initialize it to a pointer to the
           * result of calling localtime().
           * The localtime() function converts the calendar time in 'now' into a
           * broken-down time, expressed as local time.
           * This is needed for the call to strftime().
           */
          struct tm *mytime = localtime(&now);
          /*
           * If localtime() fails, it returns a null pointer; verify success before
           * continuing on.
           */
          if ( mytime )
          {
             char buffer [ 32 ];
             /*
              * Use strftime() to create a string representation of the broken-down
              * time.
              * If strftime() fails, zero is returned and the contents of the array
              * are indeterminate; verify success before continuing on.
              */
             if ( strftime(buffer, sizeof buffer, "%X", mytime) )
             {
                /*
                 * We were able to get the current time, we were able to convert
                 * this value to local time and point to its structure, we were able
                 * to create a string in our chosen format, so let's print it.
                 * The double quotes help show the full contents of the string we
                 * have created.
                 */
                printf("buffer = \"%s\"\n", buffer);
             }
          }
       }
       return 0;
    }
     
    /* my output
    buffer = "09:09:26"
    */
    Look up the format specifiers for strftime to customize the output, and adjust the buffer size as necessary.
    Last edited by dcs; 10-19-2008 at 06:42 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Displaying Current Time and Date

    Not bad, thanks for the posting. I like your comment style (very descriptive).

  4. #3
    TroUblE is offline Newbie
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Re: Displaying Current Time and Date

    Wonderfull!
    I'll use it in my next programs

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. showing current date/time problem !!
    By Paulo_Jorge in forum PHP Development
    Replies: 4
    Last Post: 06-22-2011, 12:42 PM
  2. Backup current text in textbox after x amount of time
    By Grue in forum Visual Basic Programming
    Replies: 4
    Last Post: 06-11-2009, 09:29 AM
  3. PHP date? Time?
    By phpforfun in forum PHP Development
    Replies: 6
    Last Post: 03-19-2009, 04:13 PM
  4. Date and Time
    By zeroradius in forum Database & Database Programming
    Replies: 2
    Last Post: 02-01-2009, 12:21 PM
  5. Displaying e-mail date
    By kittenslayer in forum Visual Basic Programming
    Replies: 0
    Last Post: 04-01-2007, 02:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

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