473,569 Members | 2,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Duration Conversion

[C application running on TRU64 Unix]

Hello All

I have a simple task that is driving me crazy. A string representing a
duration in the following format is passed to my application, a
function is dedicated to convert this duration to seconds;

[H]H:MM:SS

e.g. 0:00:00 or 00:12:45

The original function used atoi as part of the conversion process and
it produced some scary conversions 0:00:10 converted to 956 seconds
etc.

The function was rewritten to make use of strtol because of its better
error handling functionality but it now core dumps! Do someone have a
bullet proof idea to do this conversion assuming that anything might
be passed into this function;

e.g. A:00:00 or :0:45 etc

Reproducted below is the offending function the lines where it
currently dumps is marked with a @.

Any help would be much appreciated.

Thank you,

B

void secondDurations ()
{
long hr, min, sec, sec_duration = 0;
char tm[10];
int ch = ':';
char *end_ptr = NULL;
char duration[41];

char *pfirst;
char *plast;

// 00:00:00
// 0:00:00
// 01234567
strcpy(duration , "00:00:34") ;

//String should at leat be 7 characters long
if (strlen(duratio n) >= 7)
{
pfirst = strchr(duration , ch);
plast = strrchr(duratio n, ch);

//Test duration format 00:00:00
if((pfirst != NULL || plast != NULL) && (pfirst != plast))
{
errno = 0;

//Get hour portion
strcpy(tm, strtok(duration ,":"));

//Convert
hr = strtol(tm, &end_ptr, 10);

//Test
if (hr INT_MAX || hr < INT_MIN)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
errno = 0;

@ strcpy(tm, strtok(NULL,":" ));

min = strtol(tm, &end_ptr, 10);

if (min INT_MAX || min < INT_MIN || min 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
errno = 0;

@ strcpy(tm, strtok(NULL,":" ));

sec = strtol(tm, &end_ptr, 10);

if (sec INT_MAX || sec < INT_MIN || sec 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
sec_duration = hr * 3600 + min * 60 + sec;
printf("duratio n in seconds=%d\n",( int)sec_duratio n);
}
}
}
}
}

Sep 13 '07 #1
15 2037
bcpkh wrote:
[C application running on TRU64 Unix]

Hello All

I have a simple task that is driving me crazy. A string representing a
duration in the following format is passed to my application, a
function is dedicated to convert this duration to seconds;

[H]H:MM:SS

e.g. 0:00:00 or 00:12:45

The original function used atoi as part of the conversion process and
it produced some scary conversions 0:00:10 converted to 956 seconds
etc.

The function was rewritten to make use of strtol because of its better
error handling functionality but it now core dumps! Do someone have a
bullet proof idea to do this conversion assuming that anything might
be passed into this function;

e.g. A:00:00 or :0:45 etc

Reproducted below is the offending function the lines where it
currently dumps is marked with a @.
From reading your code, these invalid time strings should be rejected.
As the format is rather strict in what you can accept, I would use
sscanf() for this task.

void secondDurations ()
{
int hr, min, sec, num_matches, num_chars;
long sec_duration = 0;
char duration[41];

// 00:00:00
// 0:00:00
// 01234567

strcpy(duration , "00:00:34") ;

//String should at leat be 7 characters long
if (strlen(duratio n) < 7)
{
// Reject
return;
}

/* The trailing %n causes the number of characters consumed to be
recorded. This should be the entire length of the string */
num_matches = sscanf(duration , "%d:%d:%d%n ", &hr, &min, &sec,
&num_chars);
if ((num_matches != 3) || (num_chars != strlen(duration )))
{
// Malformed date. Reject
return;
}
else
{
sec_duration = hr * 3600 + min * 60 + sec;
printf("duratio n in seconds=%ld\n", sec_duration);
}
}
>
Any help would be much appreciated.

Thank you,

B
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
Sep 13 '07 #2

"bcpkh" <va************ **@gmail.comwro te in message
news:11******** **************@ 19g2000hsx.goog legroups.com...
[C application running on TRU64 Unix]

Hello All

I have a simple task that is driving me crazy. A string representing a
duration in the following format is passed to my application, a
function is dedicated to convert this duration to seconds;

[H]H:MM:SS

e.g. 0:00:00 or 00:12:45

The original function used atoi as part of the conversion process and
it produced some scary conversions 0:00:10 converted to 956 seconds
etc.

The function was rewritten to make use of strtol because of its better
error handling functionality but it now core dumps! Do someone have a
bullet proof idea to do this conversion assuming that anything might
be passed into this function;

e.g. A:00:00 or :0:45 etc

Reproducted below is the offending function the lines where it
currently dumps is marked with a @.

Any help would be much appreciated.

Thank you,

B

void secondDurations ()
{
long hr, min, sec, sec_duration = 0;
char tm[10];
int ch = ':';
char *end_ptr = NULL;
char duration[41];

char *pfirst;
char *plast;

// 00:00:00
// 0:00:00
// 01234567
strcpy(duration , "00:00:34") ;

//String should at leat be 7 characters long
if (strlen(duratio n) >= 7)
{
pfirst = strchr(duration , ch);
plast = strrchr(duratio n, ch);

//Test duration format 00:00:00
if((pfirst != NULL || plast != NULL) && (pfirst != plast))
{
errno = 0;

//Get hour portion
strcpy(tm, strtok(duration ,":"));

//Convert
hr = strtol(tm, &end_ptr, 10);

//Test
if (hr INT_MAX || hr < INT_MIN)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
errno = 0;

@ strcpy(tm, strtok(NULL,":" ));

min = strtol(tm, &end_ptr, 10);

if (min INT_MAX || min < INT_MIN || min 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
errno = 0;

@ strcpy(tm, strtok(NULL,":" ));

sec = strtol(tm, &end_ptr, 10);

if (sec INT_MAX || sec < INT_MIN || sec 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
sec_duration = hr * 3600 + min * 60 + sec;
printf("duratio n in seconds=%d\n",( int)sec_duratio n);
}
}
}
}
}
Try something like this:
long hrs, mins, sec;
char *ptr, *next;
hrs = strtol( duration, *ptr, 10 );
if ( (*ptr == ':' ) && (ptr != duration) ) {
/* hours read, try minutes */
next = ptr+1;
mins = strtol( next, &ptr, 10 );
if ( (*ptr == ':') && (ptr != next) } {
next = ptr+1;
sec = strtol( next, &ptr, 10 );
if ( *ptr == '\0' ) {
/* good - compute total seconds */

}
}
}

Other safety chacks should be made, such as that
ptr-duration is 1 or 2, and ptr-next is 2, etc.,
and that the hrs/mins/sec values are in the correct range.
--
Fred
Sep 13 '07 #3
Hello Bart and Fred

Thank you for you suggestions and comments, you have been very
helpful! I will definitely make use of your inputs.

B

Sep 13 '07 #4
Bart van Ingen Schenau <ba**@ingen.ddn s.infowrites:
bcpkh wrote:
>[C application running on TRU64 Unix]
I have a simple task that is driving me crazy. A string representing a
duration in the following format is passed to my application, a
function is dedicated to convert this duration to seconds;

[H]H:MM:SS

e.g. 0:00:00 or 00:12:45

The original function used atoi as part of the conversion process and
it produced some scary conversions 0:00:10 converted to 956 seconds
etc.
[...]
From reading your code, these invalid time strings should be rejected.
As the format is rather strict in what you can accept, I would use
sscanf() for this task.

void secondDurations ()
{
int hr, min, sec, num_matches, num_chars;
[...]
/* The trailing %n causes the number of characters consumed to be
recorded. This should be the entire length of the string */
num_matches = sscanf(duration , "%d:%d:%d%n ", &hr, &min, &sec,
&num_chars);
[...]

If the string you're scanning contains a syntactically valid integer
value that can't be stored in an int, then sscanf with the "%d" format
invokes undefined behavior. This is true for all the numeric
conversion formats for the *scanf() functions.

If you want safety, you'll need to check for the number of digits
before invoking sscanf, or you'll need to use some other method (like
strtol).

I haven't studied the original code, so I don't know why it's blowing
up.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 13 '07 #5
On Sep 14, 3:25 am, bcpkh <vanheerden.br. ..@gmail.comwro te:
[C application running on TRU64 Unix]
The original function used atoi as part of the conversion process and
it produced some scary conversions 0:00:10 converted to 956 seconds
etc.

The function was rewritten to make use of strtol because of its better
error handling functionality but it now core dumps! Do someone have a
bullet proof idea to do this conversion assuming that anything might
be passed into this function;
You forgot the following lines:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <limits.h>

Once those were included, and an output line and a main function
added, the code compiles and runs fine for me.

You should have gotten many warnings when you compiled
the code -- if not, then find out how to turn up the
warning level on your compiler.
strcpy(duration , "00:00:34") ;

//String should at leat be 7 characters long
if (strlen(duratio n) >= 7)
{
You have a weird code structure here. It goes:
if (A)
{
if (B)
return;
else if (C)
return;
else
{
if (D)
return;
else if (E)
return;
else
{
/* dozens of lines */
printf(......);
}
}
/* end of function */

It is hard to follow. IMHO, much clearer is:
if ( !A )
return;
if ( B )
return;
if ( C )
return;
if ( D )
return;
if ( E )
return;
/* dozens of lines */
/* print results */
//Get hour portion
strcpy(tm, strtok(duration ,":"));
strtok would return NULL if the stirng contains no colon.
As posted, this can't happen, but what if somebody changes
the 'ch' variable but does not update the strtok calls?
You should use the same variable for both.

Also, if the duration string contains a lot of characters
between the colons, you cause a buffer overflow when copying
into tm.

What is the purpose of this "tm" ? You could have
written:
hr = strtol( strtok(NULL, ":"), &end_ptr, 10 );

although in either case I would prefer that the result
of strtok be stored in an intermediate variable and
checked that it is not NULL.
Sep 13 '07 #6
On Thu, 13 Sep 2007 15:25:03 -0000, bcpkh <va************ **@gmail.com>
wrote:
snip explanation
>void secondDurations ()
{
long hr, min, sec, sec_duration = 0;
char tm[10];
int ch = ':';
char *end_ptr = NULL;
char duration[41];

char *pfirst;
char *plast;

// 00:00:00
// 0:00:00
// 01234567
strcpy(duration , "00:00:34") ;

//String should at leat be 7 characters long
if (strlen(duratio n) >= 7)
{
pfirst = strchr(duration , ch);
plast = strrchr(duratio n, ch);
Is it possible for pfirst to be different from plast? Can these two
statements ever produce different results?
>
//Test duration format 00:00:00
if((pfirst != NULL || plast != NULL) && (pfirst != plast))
{
errno = 0;

//Get hour portion
strcpy(tm, strtok(duration ,":"));

//Convert
hr = strtol(tm, &end_ptr, 10);

//Test
if (hr INT_MAX || hr < INT_MIN)
On some systems, sizeof(long) is the same as sizeof(int).
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
You set errno to 0. Did you intend to set it to some other value now?
return;
}
else
{
errno = 0;

@ strcpy(tm, strtok(NULL,":" ));
This will allow a string of the form 01::23:45 to be treated as
correct.
>
min = strtol(tm, &end_ptr, 10);

if (min INT_MAX || min < INT_MIN || min 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
errno = 0;
You only need to set it to 0 once. It can't become any more zero than
it is.
>
@ strcpy(tm, strtok(NULL,":" ));

sec = strtol(tm, &end_ptr, 10);

if (sec INT_MAX || sec < INT_MIN || sec 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
sec_duration = hr * 3600 + min * 60 + sec;
printf("duratio n in seconds=%d\n",( int)sec_duratio n);
}
}
}
}
}

Remove del for email
Sep 14 '07 #7
On Thu, 13 Sep 2007 17:23:32 -0700, Barry Schwarz <sc******@doezl .net>
wrote:
>On Thu, 13 Sep 2007 15:25:03 -0000, bcpkh <va************ **@gmail.com>
wrote:
snip explanation
>>void secondDurations ()
{
long hr, min, sec, sec_duration = 0;
char tm[10];
int ch = ':';
char *end_ptr = NULL;
char duration[41];

char *pfirst;
char *plast;

// 00:00:00
// 0:00:00
// 01234567
strcpy(duration , "00:00:34") ;

//String should at leat be 7 characters long
if (strlen(duratio n) >= 7)
{
pfirst = strchr(duration , ch);
plast = strrchr(duratio n, ch);

Is it possible for pfirst to be different from plast? Can these two
statements ever produce different results?
Obviously they can if I would only notice the fourth r in the second
statement.
Remove del for email
Sep 14 '07 #8
Old Wolf wrote:
>
.... snip ...
>
It is hard to follow. IMHO, much clearer is:
if ( !A )
return;
if ( B )
return;
if ( C )
return;
if ( D )
return;
if ( E )
return;
/* dozens of lines */
/* print results */
I would prefer:

if (!(!A || B || C || D || E)) {
/* dozens of lines */
/* print results */
}
return;

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 14 '07 #9
On Thu, 13 Sep 2007 23:24:33 -0400, CBFalconer <cb********@yah oo.com>
wrote:
>Old Wolf wrote:
>>
... snip ...
>>
It is hard to follow. IMHO, much clearer is:
if ( !A )
return;
if ( B )
return;
if ( C )
return;
if ( D )
return;
if ( E )
return;
/* dozens of lines */
/* print results */

I would prefer:

if (!(!A || B || C || D || E)) {
/* dozens of lines */
/* print results */
}
return;
Applying De Morgan's Theorem to your expression I get:

if ( A && !B && !C && !D && !E )
{
/* dozens of lines */
/* print results */
}

This is clearer to me, and it ostensibly takes advantage of C's
short-circuit evaluation of conditions to determine the value of the
expression. It's unclear to me whether your expression takes advantage
of C's short-circuit evaluation of conditions, given that everything
in the inner-most parenthesis is surrounded by a '!'.

Whether it is clearer to you or anyone else is a subjective matter.
Nevertheless, it's useful to apply De Morgan's Theorem to any
non-trivial expression as it gives you a pair of alternatives that
often makes it clear which one is the best, in terms of readability
and possibly even performance.

--
jay
Sep 14 '07 #10

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

Similar topics

2
2456
by: Louis | last post by:
Hi, Does anyone know of a script that will give "weighted job duration"? I want to use it, to identify which jobs are hogging the CPU. That is for a given server, list the sql agent jobs ordered by: (avg job duration in minutes) times (avg num of times job runs in a given day).
1
2234
by: Dave | last post by:
Greetings, I am trying to create a duration field in a query. I have a field with a start time and a field with an end time. They are both in military time, and are formatted as hours:minutes. I am trying to create a duration field by subtracting the start time from the end time. This is working. However, some end times do not happen...
4
5696
by: Stephen Young | last post by:
Hello Have a bit of a problem, im trying to get the total number of hours:minutes for all actions over the month in a query, have tried two methods to no luck... Duration This Month: Format(Sum(.-.),"hh:nn") displays the total hours but rolls over anything over 1 day and starts
2
2152
by: CJ | last post by:
If I set the outputcache like this.. <%@ OutputCache Duration="900" VaryByParam="none" %> on a server on the east coast. Will someone who hits the site on the west coast get three hours added to the cache expiration? it mentions at msdn that Response.Cache.SetExpires should follow UTC and any HTTP1.1 docs that you read on w3c will say...
0
1412
by: kbodily | last post by:
Hi, I have this code in a dragdrop routine for a listview to get the duration of an mp3, where fPath is the path to the file that gets dropped Dim fname As String = Chr(34) + Trim(fPath) + Chr(34) mciSendString("open " & fname & " alias song", 0, 0, 0) Dim totalTime As String = Space(128) mciSendString("status song length", totalTime,...
5
2052
by: Ivan Novick | last post by:
Is the string below automatic storage duration? #include <iostream> #include <string> int main(int argc, char** argv) { std::cout << std::string().size() << std::endl; return 0; }
3
2313
by: Keith Wilby | last post by:
Here's something I thought I'd know how to do ... I have an Appointments table and want to add a duration (integer representing hours) to a start time (Date/Time field, "Short Time" format) to give a finish time. I want to do this in a query but if I do + I get a date returned. I've tried using the Format function to no avail. What...
0
1250
by: Yew12 | last post by:
I'm trying to use PL/SQL to create a trigger that will stop bookings. Based on a duration field and a start time. I have had a go at trying to do this but I'm sure that im well off from where I need to be. Create or Replace Trigger multiple_appointments Before insert on appointment Begin IF (NEW.toNumber(ADATE) between...
1
3750
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: - "function prototype scope" for structures and unions? - "extern" for internal linkage ?
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.