Thread: current time

  1. #1

    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'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.

    #include <stdio.h>
    #include <time.h>
    void GetTimeDate (void);
    int main()
       printf("Before the GetTimeDate() function is called.\n");
       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");
       str = asctime(localtime(&now));
       printf("The current date and time is ");
       for (i=0; str[i]; i++)
           printf("%c", str[i]);   

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    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.
    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
    Your function prototype is spelt incorrectly:

    >void GetTimeDate (void);

    Also, you function should be like this:

    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, 03:41 AM
  2. Military Time Functions
    By BB18 in forum C Programming
    Replies: 6
    Last Post: 10-10-2004, 01:57 PM
  3. Killing someones grandparents
    By nickname_changed in forum A Brief History of
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM