-
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
-
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));
}
-
Your function prototype is spelt incorrectly:
>void GetTimeDate (void);
Also, you function should be like this:
Code:
void GetDateTime(void)
{ .........