a value change, why

C

collinm

hi

i would like to take a string and parse it to put her value in a tm

my struct:

typedef struct production_s
....
struct tm tmbStartRun;
struct tm tmbEndRun;
char time_rtu_debut[15];
char htime_rtu_debut[15];
....
} production_t;

printf("prod->htime_rtu_fin%s\n", prod->htime_rtu_fin); // 16:08:55

parseDate(&prod->tmbEndRun, date_rtu);
parseTime(&prod->tmbEndRun, prod->htime_rtu_fin);
printf("prod->htime_rtu_fin%s\n", prod->htime_rtu_fin); //16

void parseDate(struct tm *tm, char tmpdate[])
{
#define DELIM_DATE "/"
char *sep = strtok(tmpdate, DELIM_DATE);
int i=0;
while (sep != NULL)
{
i++;
if(sep!=NULL)
{
if(i==1)
tm->tm_year = 100+atoi(sep);
if(i==2)
tm->tm_mon= atoi(sep);
if(i==3)
tm->tm_mday= atoi(sep);
}
sep = strtok(NULL, DELIM_DATE);
}
}

void parseTime(struct tm *tm, char tmptime[])
{
#define DELIM_TIME ":"
char *sep = strtok(tmptime, DELIM_TIME);
int i=0;
while (sep != NULL)
{
i++;
if(sep!=NULL)
{
if(i==1)
tm->tm_hour = atoi(sep);
if(i==2)
tm->tm_min= atoi(sep);
if(i==3)
tm->tm_sec= atoi(sep);
}
sep = strtok(NULL, DELIM_TIME);
}
}


why prod->htime_rtu_fin display me 16? what happen with her value
16:08:55

thanks
 
E

Eric Sosman

collinm said:
hi

i would like to take a string and parse it to put her value in a tm
[... see upthread for code ...]
why prod->htime_rtu_fin display me 16? what happen with her value
16:08:55

strtok() destroyed it. The original string was

1 6 : 0 8 : 5 5 \0

but after three uses of strtok() it became

1 6 \0 0 8 \0 5 5 \0

Instead of strtok(), try using sscanf() -- the code
will be shorter and simpler, too:

void parseTime(struct tm *tm, char tmptime[]) {
if (sscanf(tmptime, "%d:%d:%d",
&tm->tm_hour, &tm->tm_min, &tm->tm_sec) != 3) {
/* something is wrong; handle the failure */
}
}

By the way, your parseDate() function has an error: the
tm_mon values are 0=January, 1=February, ..., 11=December,
so you need to subtract one from the numeric value.
 
W

William Ahern

collinm said:
hi

i would like to take a string and parse it to put her value in a tm
my struct:
typedef struct production_s
...
struct tm tmbStartRun;
struct tm tmbEndRun;
char time_rtu_debut[15];
char htime_rtu_debut[15];
...
} production_t;
printf("prod->htime_rtu_fin%s\n", prod->htime_rtu_fin); // 16:08:55
parseDate(&prod->tmbEndRun, date_rtu);
parseTime(&prod->tmbEndRun, prod->htime_rtu_fin);
printf("prod->htime_rtu_fin%s\n", prod->htime_rtu_fin); //16
void parseTime(struct tm *tm, char tmptime[])
{
#define DELIM_TIME ":"
char *sep = strtok(tmptime, DELIM_TIME);
why prod->htime_rtu_fin display me 16? what happen with her value
16:08:55

strtok() is replacing the colon with '\0'. Often times in this situation a
quick fix is to create a temporary copy. If your compiler supports C99-style
compound literals--and you're sure the input will always have a sensible
length--you can do:

char *tmp = strcpy((char [strlen(tmptime) + 1]){},tmptime);
char *sep = strtok(tmp,DELIM_TIME);

- Bill
 
C

collinm

Eric said:
strtok() destroyed it. The original string was

1 6 : 0 8 : 5 5 \0

but after three uses of strtok() it became

1 6 \0 0 8 \0 5 5 \0

Instead of strtok(), try using sscanf() -- the code
will be shorter and simpler, too:

void parseTime(struct tm *tm, char tmptime[]) {
if (sscanf(tmptime, "%d:%d:%d",
&tm->tm_hour, &tm->tm_min, &tm->tm_sec) != 3) {
/* something is wrong; handle the failure */
}
}

By the way, your parseDate() function has an error: the
tm_mon values are 0=January, 1=February, ..., 11=December,
so you need to subtract one from the numeric value.

ok thanks

is there a way to remove 30 seconds to the tm?
 
D

Dave Thompson

Eric Sosman wrote:
Instead of strtok(), try using sscanf() -- the code
will be shorter and simpler, too:

void parseTime(struct tm *tm, char tmptime[]) {
if (sscanf(tmptime, "%d:%d:%d",
&tm->tm_hour, &tm->tm_min, &tm->tm_sec) != 3) {
/* something is wrong; handle the failure */
}
}

By the way, your parseDate() function has an error: the
tm_mon values are 0=January, 1=February, ..., 11=December,
so you need to subtract one from the numeric value.

ok thanks

is there a way to remove 30 seconds to the tm?

Do you mean to compute the time 30 seconds earlier than the time value
you have parsed into the struct tm pointed to by tm? Yes.
time_t tt;
/* after filling in all standard tm-> fields */
tm->tm_sec -= 30;
tm->tm_isdst = -1; /* if not known and set for local time value */
tt = mktime (tm);
if( tt == (time_t)-1 ) /* invalid -- handle error somehow */
/* else -- tt is the encoded timevalue if desired -- conventionally
seconds since 1970-01-01 00:00:00 Z but that's not required */
/* even if you don't use tt, as a side effect the fields of tm
have been 'normalized' to in-range values, e.g.
104 (2004) 02 (Mar) 01 00 00 -10 changed to
104 (2004) 01 (Feb) 29 23 59 50 */

Similarly for minutes, hours, days, and months. Note when adding or
subtracting months they are not all the same length, and (thus)
certain day numbers don't exist in certain months. Similarly for years
some are leap and some not.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top