static void showtime(FILE *out, struct timeval *before, struct timeval *after, struct rusage *ru) { after->tv_sec -= before->tv_sec; after->tv_usec -= before->tv_usec; if (after->tv_usec < 0) after->tv_sec--, after->tv_usec += 1000000; if (pflag) { /* POSIX wants output that must look like "real %f\nuser %f\nsys %f\n" and requires at least two digits after the radix. */ fprintf(out, "real %jd%c%02ld\n", (intmax_t)after->tv_sec, decimal_point, after->tv_usec/10000); fprintf(out, "user %jd%c%02ld\n", (intmax_t)ru->ru_utime.tv_sec, decimal_point, ru->ru_utime.tv_usec/10000); fprintf(out, "sys %jd%c%02ld\n", (intmax_t)ru->ru_stime.tv_sec, decimal_point, ru->ru_stime.tv_usec/10000); } else if (hflag) { humantime(out, after->tv_sec, after->tv_usec/10000); fprintf(out, " real\t"); humantime(out, ru->ru_utime.tv_sec, ru->ru_utime.tv_usec/10000); fprintf(out, " user\t"); humantime(out, ru->ru_stime.tv_sec, ru->ru_stime.tv_usec/10000); fprintf(out, " sys\n"); } else { fprintf(out, "%9jd%c%02ld real ", (intmax_t)after->tv_sec, decimal_point, after->tv_usec/10000); fprintf(out, "%9jd%c%02ld user ", (intmax_t)ru->ru_utime.tv_sec, decimal_point, ru->ru_utime.tv_usec/10000); fprintf(out, "%9jd%c%02ld sys\n", (intmax_t)ru->ru_stime.tv_sec, decimal_point, ru->ru_stime.tv_usec/10000); } }
int main(int argc, char *argv[]) { try { vector < string > args; for (int i = 0; i < argc; ++i) { args.push_back(argv[i]); } App app; app.Main(args); } catch(string s) { cout << humantime() << "Error: " << s << endl; } catch(std::runtime_error s) { cout << humantime() << "Runtime error: " << s.what() << endl; } catch(std::exception s) { cout << humantime() << "Exception: " << s.what() << endl; } catch( ...) { cout << humantime() << "Unknown error." << endl; } }
void test_RegularTime(void) { time_t sample = 1276601278; char expected[15]; struct tm* tm; tm = localtime(&sample); TEST_ASSERT_TRUE(time != NULL); snprintf(expected, 15, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); TEST_ASSERT_EQUAL_STRING(expected, humantime(sample)); }
TEST_F(humandateTest, RegularTime) { time_t sample = 1276601278; std::ostringstream expected; tm* time; time = localtime(&sample); ASSERT_TRUE(time != NULL); expected << std::setfill('0') << std::setw(2) << time->tm_hour << ":" << std::setw(2) << time->tm_min << ":" << std::setw(2) << time->tm_sec; EXPECT_STREQ(expected.str().c_str(), humantime(sample)); }
void test_CurrentTime(void) { time_t sample; char expected[15]; struct tm* tm; time(&sample); tm = localtime(&sample); TEST_ASSERT_TRUE(tm != NULL); snprintf(expected, 15, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); TEST_ASSERT_EQUAL_STRING(expected, humantime(sample)); return; }