I'm trying to write a function to get the current system time, so I can
use it when I need to output it to a log, below is the code I'm
testing:
#include "include.h"
#include <stdlib.h>
main(argc, argv)
int argc;
char **argv;
{
int GetCurrentTime();
struct tm *time_struct;
GetCurrentTime(time_struct);
fprintf(stdout, "Starting get_error_checks run on: %d...\n\n",
time_struct->tm_year+1900);
exit(0);
}
int GetCurrentTime(ts_ptr)
struct tm *ts_ptr;
{
static char *weekday[7] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
static char *month[12] = {"January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October", "November", "December"};
time_t time_value=0;
/* Retrieve current time information from the Operating System . . .
*/
time(&time_value);
ts_ptr = localtime(&time_value);
fprintf(stdout, "**\tGetCurrentTime %s, %s %d, %d
%d:%02d\n**\n**\n*/\n\n\n",
weekday[ts_ptr->tm_wday],
month[ts_ptr->tm_mon],
ts_ptr->tm_mday,
ts_ptr->tm_year+1900,
ts_ptr->tm_hour,
ts_ptr->tm_min);
return;
}
The "include.h" just has stdio.h, time.h and sys/systypes.h as further
includes. This seems to work fine in the function call but I can't get
the caller to "know" about the changes. I've tried declaring
time_struct as just a pointer (as above) or having a struct declared,
passing in time_struct, &time_struct (which I think are the same in
this case ?) having a separate pointer to the struct get passed, with
no luck. Obviously I'm missing something fundamental here, can anyone
help ? I have been going through FAQs and some some similar issues... 2 2378
<jc******@rochester.rr.com> wrote in message
news:11********************@o13g2000cwo.googlegrou ps.com... I'm trying to write a function to get the current system time, so I can use it when I need to output it to a log, below is the code I'm testing:
#include "include.h" #include <stdlib.h>
main(argc, argv) int argc; char **argv; {
int GetCurrentTime();
struct tm *time_struct;
GetCurrentTime(time_struct);
fprintf(stdout, "Starting get_error_checks run on: %d...\n\n", time_struct->tm_year+1900);
exit(0); } int GetCurrentTime(ts_ptr) struct tm *ts_ptr; { static char *weekday[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
static char *month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
time_t time_value=0;
/* Retrieve current time information from the Operating System . . . */
time(&time_value); ts_ptr = localtime(&time_value); fprintf(stdout, "**\tGetCurrentTime %s, %s %d, %d %d:%02d\n**\n**\n*/\n\n\n", weekday[ts_ptr->tm_wday], month[ts_ptr->tm_mon], ts_ptr->tm_mday, ts_ptr->tm_year+1900, ts_ptr->tm_hour, ts_ptr->tm_min); return;
}
The "include.h" just has stdio.h, time.h and sys/systypes.h as further includes. This seems to work fine in the function call but I can't get the caller to "know" about the changes. I've tried declaring time_struct as just a pointer (as above) or having a struct declared, passing in time_struct, &time_struct (which I think are the same in this case ?) having a separate pointer to the struct get passed, with no luck. Obviously I'm missing something fundamental here, can anyone help ? I have been going through FAQs and some some similar issues...
There were a few things wrong. Like your original pointer didn't point to
anything. This seems to work though [below] - if there's any further
questions about it, fire away.
struct tm * GetCurrentTime(void);
int main(int argc, char ** argv)
{
struct tm * time_struct;
time_struct = GetCurrentTime();
fprintf(stdout, "Starting get_error_checks run on: %d %d...\n\n",
time_struct->tm_year, time_struct->tm_year+1900);
getchar();
exit(0);
}
struct tm * GetCurrentTime(void)
{
static char *weekday[7] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
static char *month[12] = {"January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October", "November", "December"};
struct tm * ts_ptr = NULL;
time_t time_value=0;
/* Retrieve current time information from the Operating System . . .
*/
time(&time_value);
/* localtime() returns a pointer to static storage.
*/
if((ts_ptr = localtime(&time_value)) != NULL)
{
fprintf(stdout, "**\tGetCurrentTime %s, %s %d, %d
%d:%02d\n**\n**\n*/\n\n\n",
weekday[ts_ptr->tm_wday],
month[ts_ptr->tm_mon],
ts_ptr->tm_mday,
ts_ptr->tm_year+1900,
ts_ptr->tm_hour,
ts_ptr->tm_min);
}
/* although ts_ptr is a 'local' variable, returning its 'value' is ok as
localtime()
** returns a pointer to static storage. If localtime fails, the value
is NULL.
*/
return ts_ptr;
} jc******@rochester.rr.com wrote: I'm trying to write a function to get the current system time, so I can use it when I need to output it to a log, below is the code I'm testing:
The OP's code is retained at EOM. Try the following, noting the
differences between it and your code.
#include <stdio.h>
#include <time.h>
#include <string.h>
void GetCurrentTime();
int main(void)
{
struct tm time_struct; /* NOTE: not a pointer */
GetCurrentTime(&time_struct);
fprintf(stdout, "Starting get_error_checks run on: %d...\n\n",
time_struct.tm_year + 1900);
return 0;
}
void GetCurrentTime(struct tm *ts_ptr)
{
time_t time_value = 0;
time(&time_value);
memcpy(ts_ptr, localtime(&time_value), sizeof *ts_ptr);
fprintf(stdout, "%s\n", ctime(&time_value));
}
[Output]
Mon Jan 9 11:48:57 2006
Starting get_error_checks run on: 2006...
[OP's code]
#include "include.h" #include <stdlib.h>
main(argc, argv) int argc; char **argv; {
int GetCurrentTime();
struct tm *time_struct;
GetCurrentTime(time_struct);
fprintf(stdout, "Starting get_error_checks run on: %d...\n\n", time_struct->tm_year+1900);
exit(0); } int GetCurrentTime(ts_ptr) struct tm *ts_ptr; { static char *weekday[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
static char *month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
time_t time_value=0;
/* Retrieve current time information from the Operating System . . . */
time(&time_value); ts_ptr = localtime(&time_value); fprintf(stdout, "**\tGetCurrentTime %s, %s %d, %d %d:%02d\n**\n**\n*/\n\n\n", weekday[ts_ptr->tm_wday], month[ts_ptr->tm_mon], ts_ptr->tm_mday, ts_ptr->tm_year+1900, ts_ptr->tm_hour, ts_ptr->tm_min); return;
}
The "include.h" just has stdio.h, time.h and sys/systypes.h as further includes. This seems to work fine in the function call but I can't get the caller to "know" about the changes. I've tried declaring time_struct as just a pointer (as above) or having a struct declared, passing in time_struct, &time_struct (which I think are the same in this case ?) having a separate pointer to the struct get passed, with no luck. Obviously I'm missing something fundamental here, can anyone help ? I have been going through FAQs and some some similar issues... This discussion thread is closed Replies have been disabled for this discussion. Similar topics
5 posts
views
Thread by kazack |
last post: by
|
4 posts
views
Thread by anonymous |
last post: by
|
6 posts
views
Thread by DeepaK K C |
last post: by
|
1 post
views
Thread by jconnort |
last post: by
|
5 posts
views
Thread by Bill Pursell |
last post: by
|
50 posts
views
Thread by Mikhail Teterin |
last post: by
|
5 posts
views
Thread by Mike Cain |
last post: by
|
6 posts
views
Thread by =?Utf-8?B?QWxleGFuZGVyZmU=?= |
last post: by
|
13 posts
views
Thread by frakie |
last post: by
|
160 posts
views
Thread by DiAvOl |
last post: by
| | | | | | | | | | |