Results 1 to 3 of 3

Thread: I need C to generate the Date in a certain format

  1. #1
    dmartin17
    Guest

    I need C to generate the Date in a certain format

    I am trying to generate a Common Log File. TO do this i need the Date in this form:
    [day/month/year:hh:mm:ss zone]
    For example, [08/May/1997:16:27:54 +0100]
    How can i get C to do this simply?

    thanks

  2. #2
    jim mcnamara
    Guest
    Try:

    Code:
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main(void){
    	char tmp[120];
    	char tmzone[4];
    	time_t lt;
    	struct tm *tmptr;
    	memset(tmp,0x00,sizeof(tmp));
    	memset(tmzone,0x00,sizeof(tmzone));
    	lt = time(NULL);  // time now 
            tmptr=localtime(&lt);
    	// make this format: [day/month/year:hh:mm:ss zone] 
    	strftime(tmzone,sizeof(tmzone),"%Z",tmptr);
    	strftime(tmp,sizeof(tmp),"[%d/%b/%Y:%H:%M:%S ",tmptr);
            strcat(tmp,tmzone);
            strcat(tmp,"]");
            printf("%s\n",tmp);
      	return 0;
    }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    nice, except that tmzone should be at least 6 chars long...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width