+ Reply to Thread
Results 1 to 3 of 3

Thread: Displaying Current Time and Date

  1. #1
    dcs
    dcs is online now
    Guru dcs is just really nice dcs is just really nice dcs is just really nice dcs is just really nice
    Join Date
    Mar 2008
    Posts
    771

    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 08:42 PM.

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Displaying Current Time and Date

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

  3. #3
    Newbie TroUblE is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    8

    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. date and time present
    By Siten0308 in forum C# Programming
    Replies: 11
    Last Post: 10-10-2008, 12:58 PM
  2. Windows Shortcut List - Saves Time
    By 2stamlers in forum The Lounge
    Replies: 6
    Last Post: 04-10-2008, 06:58 AM
  3. Date Filter Being Odd, ms access 03, Any Advise?
    By Xantara in forum Visual Basic Programming
    Replies: 1
    Last Post: 02-08-2008, 07:49 AM
  4. Displaying e-mail date
    By kittenslayer in forum Visual Basic Programming
    Replies: 0
    Last Post: 04-01-2007, 04:45 PM
  5. PHP:Tutorial The Date
    By John in forum PHP Tutorials
    Replies: 0
    Last Post: 01-10-2007, 06:10 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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