• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 692
  • Last Modified:

Print current date and time in a file

Hi,

My C program printing data in a file. Now, I need to add current date and time in a specific format. How would I do it? Is there any library function for that?

Here is what I print in a file:

fprintf(outfile,"id=1234\n");
fprintf(outfile,"frequnecy=34000\n");
fprintf(outfile,"name=michelle\n");

Now, I need to add current date and time in yymmdd hh:mm format.

forexample:

fprintf(outfile,"namedatetime=michelle date time\n");

So, it should print in a file like this (current date & time)

namedatetime=michelle 040416 10:47

Name (michelle) is constant, so don't need to worry about it. I will put michelle in print statement .

I will be really appreciated if you can help me in this.

Thanks,
John
0
John22
Asked:
John22
  • 3
  • 3
1 Solution
 
stefan73Commented:
Hi John22,
Use strftime:

 #include <time.h>

     size_t strftime(const char *s, size_t  maxsize,  const  char
     *format, const struct tm *timeptr);


Cheers,
Stefan
0
 
stefan73Commented:
Here is an example:

#include <time.h>

time_t now;
char timebuf[256];

now=time(0);
strftime(timebuf,"%y%m%d %H%M",localtime(now));

fprintf(outfile,"namedatetime=michelle %s\n",timebuf);


0
 
NovaDenizenCommented:
#include <time.h>

....

time_t now_secs;
struct tm now_breakout;
char buf[1024];
time(&now);
now_breakout = *(localtime(&now)); // or use localtime_r
strftime(buf, sizeof(buf), "%g%m%d %H:%M", &now_breakout);
fprintf(outfile, "namedatetime = michelle %s\n", buf);

Check the strftime man page if you want to change the format string.  For instance, if you want to avoid y2k issues and use a 4-digit year instead, replace the "%g" with "%G"


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
stefan73Commented:
> strftime(timebuf,"%y%m%d %H%M",localtime(now));

should read

> strftime(timebuf,"%y%m%d %H%M",localtime(&now));

...as localtime expects a pointer.
0
 
NovaDenizenCommented:
There's a mistake in my format fields.  The %g and %G give you the ISO 8601 bizarro year instead of the calender year, which you get through %y and %Y.  
0
 
John22Author Commented:
Thanks stefan73 and NovaDenizen.

I tried NovaDenizen code, but I got error. After I chaged time_t now_secs to time_t now and it worked fine.

I also tried stefan73. I also got error, but can't figure out. Here is the error:

cc -i test.c
"test.c", line 210: warning: improper pointer/integer combination: arg #1
"test.c", line 210: warning: improper pointer/integer combination: arg #2
"test.c", line 210: warning: argument #3 is incompatible with prototype:
        prototype: pointer to const char : "/usr/include/time.h", line 86
        argument : pointer to struct tm {int tm_sec, int tm_min, int tm_hour, int tm_mday, int tm_mon, int tm_year, int tm_wday, int tm_yday, i...
"test.c", line 210: prototype mismatch: 3 args passed, 4 expected
cc: acomp failed for test.c

Here is line 210:

strftime(timebuf,"%y%m%d %H%M",localtime(&now));

Thanks again.
John

0
 
NovaDenizenCommented:
Don't forget to change my %g to %y.  It may give you the right answer now, but as soon as the financial year changes over it will give you the wrong answer.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now