473,321 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

a value change, why

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

Nov 14 '05 #1
4 1548


collinm wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #2
collinm <co*****@laboiteaprog.com> wrote:
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 <snip>
void parseTime(struct tm *tm, char tmptime[])
{
#define DELIM_TIME ":"
char *sep = strtok(tmptime, DELIM_TIME); <snip>
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
Nov 14 '05 #3
Eric Sosman wrote:
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?

Nov 14 '05 #4
On 10 Jun 2005 10:15:35 -0700, "collinm" <co*****@laboiteaprog.com>
wrote:
Eric Sosman wrote:

<snip>
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
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rick | last post by:
I have an XML document that is generated from Infopath, I need to change the value of a namespace that is defined in a node in the form: <xsf:xDocumentClass "xmlns:my=valuehere">. when i navigate...
3
by: Ann Huxtable | last post by:
I wrote a method that accepted a form. The signature was as ff: void foo(ref Form frm) ; I had to remove the ref keyword to get it to compile. Any idea why? I though forms would be passed by...
21
by: planetthoughtful | last post by:
Hi All, As always, my posts come with a 'Warning: Newbie lies ahead!' disclaimer... I'm wondering if it's possible, using raw_input(), to provide a 'default' value with the prompt? I would...
3
by: John Smith | last post by:
I'm looking into this peace of code: protected void DropDown_SelectedIndexChanged(object sender, EventArgs e) { DropDownList list = (DropDownList)sender; TableCell cell = list.Parent as...
2
by: John Smith | last post by:
Will this line of the code: item.Cells.Text = "Some text..."; change only DataGrid visual value or it will also change value in the DataSource? How can I change value in DataSource? ...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
8
by: grpramodkumar | last post by:
HI, function change(value,sub) { subcat = document.getElementById(sub); subcat.options.value = value; }
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
5
by: raha | last post by:
hi. Is there any body to help me? I am writing a web program. I have some forms. each form have some controls and users can edit the controls. finally they can save their forms. I would like...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.