Re: Having problem populating time structure

From: Al Bowers (xabowers_at_rapidsys.com)
Date: 03/23/04


Date: Mon, 22 Mar 2004 19:31:33 -0500


Udoo wrote:
> Hi folks,
> I am trying to read a time entry from a log file and trying to
> populate the time structure. The issue I am having is everytime I read
> the next string of the ctime format log entry my previous field of the
> time structure gets overwritten. Please advice what is that I am doing
> wrong.
> Also suggestions to improve code. Its been almost 8years that I wrote
> C code and having some issues writing it.
>
> *********
> void populateTime(char file[])
> {
> struct tm time_struct;
> char date[50];
> char init[50];
> int counter = 0;
> struct tm time_struct;
>
> input_file_pointer = fopen(file, "r");
>
> fscanf( input_file_pointer, " %s ", date);
> time_struct.tm_wday = date;
>
> fscanf( input_file_pointer, " %s ", date);
> time_struct.tm_mon = date;
>
> fscanf( input_file_pointer, " %s ", date);
> time_struct.tm_mday = date;
>
> printf("Day of the Month = %s\n", time_struct.tm_wday );
> printf("Month = %s\n", time_struct.tm_mon );
> printf("Date of the Month = %s\n", time_struct.tm_mday );
> }
> ********
> The Log file entry is as follows:
> Mon Mar 22 09:59:53 CST 2004 BLAH BLAH BLAH.....
>
> I need to populate the time structure.
>
> When I run the above code I see :
> Day of Month = 22
> Month = 22
> Date of the Month = 22

The struct tm members are type int. You will need to have
a algorithm to convert the string mon to int and back. Also
you will need to convert the wday struct member to the
appropriate string.

Here is one way you may try.

#include <time.h>
#include <stdio.h>
#include <string.h>

const char mon_name[12][4] = {
          "Jan", "Feb", "Mar", "Apr", "May", "Jun",
          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
static const char wday_name[7][4] = {
          "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

int parsedate(const char *date, struct tm *t)
{
    int i;
    char month[4] = "";

    if(6 != sscanf(date,"%*s %s %d %d:%d:%d %*s %d",
                    month,&t->tm_mday,&t->tm_hour,&t->tm_min,
                    &t->tm_sec,&t->tm_year)) return 0;
    for(i = 0;i < 12;i++)
       if(0 == strcmp(mon_name[i],month)) break;
    if(i == 12) return 0;
    t->tm_mon = i;
    t->tm_year -= 1900;
    return 1;
}

int main(void)
{
    struct tm t = {0};
    time_t date;

    if(parsedate("Mon Mar 22 09:59:53 CST 2004",&t))
    {
       if((date = mktime(&t)) != (time_t)-1)
       {
          t = *localtime(&date);
          printf("Day of the Month = %s\n", wday_name[t.tm_wday] );
          printf("Month = %s\n", mon_name[t.tm_mon] );
          printf("Date of the Month = %d\n", t.tm_mday );
       }
       else puts("Unable to determine the time");
    }
    else puts("Unable to parse the date");
    return 0;
}

-- 
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


Relevant Pages

  • Re: Having problem populating time structure
    ... > I am trying to read a time entry from a log file and trying to ... >the next string of the ctime format log entry my previous field of the ... >time structure gets overwritten. ... The line above says to read a string from the input file, ...
    (comp.lang.c)
  • Re: Having problem populating time structure
    ... > I am trying to read a time entry from a log file and trying to ... >the next string of the ctime format log entry my previous field of the ... >time structure gets overwritten. ... tm_wday is still an int. ...
    (comp.lang.c)
  • Having problem populating time structure
    ... I am trying to read a time entry from a log file and trying to ... time structure gets overwritten. ... I need to populate the time structure. ...
    (comp.lang.c)