current time

This is a discussion on current time within the C Programming forums, part of the General Programming Boards category; I tried printing out the current time on my computer through a character string, by using a pointer. The program ...

  1. #1
    Unregistered
    Guest

    current time

    I tried printing out the current time on my computer through a character string, by using a pointer. The program works fine, except for one small glitch....it's not printing out the correct time. Instead it's printing out some day in January 2116. I figure it has to be somewhere in the code for the address of the current time.
    Here's the code, maybe someone can show me where it went wrong.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    void GetTimeDate (void);
    
    int main()
    {
       printf("Before the GetTimeDate() function is called.\n");
       GetDateTime();
       printf("After the GetDateTime() function is called.\n");
       return 0;
    }
    
    /**********************************The GetDateTime function**************************************/
    
    GetDateTime (void)
    { 
       time_t now;
       int i;
       char *str;
       
       printf("Within the GetDateTime() function.\n");
       time(&now);
       str = asctime(localtime(&now));
       printf("The current date and time is ");
       for (i=0; str[i]; i++)
           printf("%c", str[i]);   
    }
    Nymph

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I think this is because the asctime functione expects a struct tm argument and not a time_t (long).
    You can use the localtime function (corrects for local time zone) to convert the time_t to a tm structure.
    Code:
    void GetDateTime (void)
    { 
       time_t now;
       struct tm *newtime;
       
       printf("Within the GetDateTime() function.\n");
       time(&now); /* get time as long */
       newtime = localtime(&now); /* convert to local time */
       
       printf("The current date and time is: %s\n", asctime(newtime));
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your function prototype is spelt incorrectly:

    >void GetTimeDate (void);

    Also, you function should be like this:

    Code:
    void GetDateTime(void)
    { .........
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 04:41 AM
  2. Military Time Functions
    By BB18 in forum C Programming
    Replies: 6
    Last Post: 10-10-2004, 02:57 PM
  3. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 08:56 AM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 07:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

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