#22 Implementation of hh:mm:ss (/sipp/trunk/scenario.cpp)

open
nobody
sipp (50)
5
2012-12-15
2007-08-02
Simon Flannery
No

There is currently a function in the file "/sipp/trunk/scenario.cpp" called get_time that does not support the hh:mm:ss time format. I have added code to implement this simple task.

Note that I removed the use of the standard "strtod" function in preference to using the standard "strtol" function - so I hope that will not break anything else. I have tested it using MS VS C++ 2005 (professional, not express) and GNU g++ using a simple test harness - see the attached file.

/* This function returns a time in milliseconds from a string.
* The multiplier is used to convert from the default input type into
* milliseconds. For example, for seconds you should use 1000 and for
* milliseconds use 1. */
long get_time(const char *ptr, const char *what, int multiplier) {
char *endptr;
long ret;
long time[2];

ret = 0; /* Clear it. */
if (isdigit(*ptr) == 0) {
ERROR_P2("%s, \"%s\" is not a valid time!\n", what, ptr);
}

ret = strtol(ptr, &endptr, 10);
if (*endptr) {
if (strcmp(endptr, "s") == 0) { /* Seconds */
ret *= 1000;
} else if (strcmp(endptr, "ms") == 0) { /* Milliseconds. */
/* Nothing. */
} else if (strcmp(endptr, "m") == 0) { /* Minutes. */
ret *= 60;
ret *= 1000;
} else if (strcmp(endptr, "h") == 0) { /* Hours. */
ret *= 60;
ret *= 60;
ret *= 1000;
} else if (strstr(endptr, ":") != NULL) {
time[0] = ret;
ret = strtol(++endptr, &endptr, 10);
if (strstr(endptr, ":") != NULL) {
time[1] = ret;
ret = strtol(++endptr, &endptr, 10);
if (!*endptr) {
ret += time[0] * 3600 + time[1] * 60;
} else {
ret = 0;
ERROR_P2("%s, \"%s\" is not a valid time!\n", what, ptr);
}
} else if (!*endptr) {
ret += time[0] * 600;
}
else {
ret = 0;
ERROR_P2("%s, \"%s\" is not a valid time!\n", what, ptr);
}
} else {
ret = 0;
ERROR_P2("%s, \"%s\" is not a valid time!\n", what, ptr);
}
} else {
ret *= multiplier;
}

return ret;
}

Discussion

  • Simon Flannery
    Simon Flannery
    2007-08-02

    get_time test harness

     
    Attachments
  • Logged In: YES
    user_id=1475960
    Originator: NO

    To be reviewed