|
10 | 10 |
|
11 | 11 | #include <ctype.h>
|
12 | 12 | #include <limits.h>
|
| 13 | +#include <math.h> |
13 | 14 | #include <signal.h>
|
14 | 15 | #ifndef WIN32
|
15 | 16 | #include <unistd.h> /* for write() */
|
@@ -531,6 +532,57 @@ ClearOrSaveResult(PGresult *result)
|
531 | 532 | }
|
532 | 533 |
|
533 | 534 |
|
| 535 | +/* |
| 536 | + * Print microtiming output. Always print raw milliseconds; if the interval |
| 537 | + * is >= 1 second, also break it down into days/hours/minutes/seconds. |
| 538 | + */ |
| 539 | +static void |
| 540 | +PrintTiming(double elapsed_msec) |
| 541 | +{ |
| 542 | + double seconds; |
| 543 | + double minutes; |
| 544 | + double hours; |
| 545 | + double days; |
| 546 | + |
| 547 | + if (elapsed_msec < 1000.0) |
| 548 | + { |
| 549 | + /* This is the traditional (pre-v10) output format */ |
| 550 | + printf(_("Time: %.3f ms\n"), elapsed_msec); |
| 551 | + return; |
| 552 | + } |
| 553 | + |
| 554 | + /* |
| 555 | + * Note: we could print just seconds, in a format like %06.3f, when the |
| 556 | + * total is less than 1min. But that's hard to interpret unless we tack |
| 557 | + * on "s" or otherwise annotate it. Forcing the display to include |
| 558 | + * minutes seems like a better solution. |
| 559 | + */ |
| 560 | + seconds = elapsed_msec / 1000.0; |
| 561 | + minutes = floor(seconds / 60.0); |
| 562 | + seconds -= 60.0 * minutes; |
| 563 | + if (minutes < 60.0) |
| 564 | + { |
| 565 | + printf(_("Time: %.3f ms (%02d:%06.3f)\n"), |
| 566 | + elapsed_msec, (int) minutes, seconds); |
| 567 | + return; |
| 568 | + } |
| 569 | + |
| 570 | + hours = floor(minutes / 60.0); |
| 571 | + minutes -= 60.0 * hours; |
| 572 | + if (hours < 24.0) |
| 573 | + { |
| 574 | + printf(_("Time: %.3f ms (%02d:%02d:%06.3f)\n"), |
| 575 | + elapsed_msec, (int) hours, (int) minutes, seconds); |
| 576 | + return; |
| 577 | + } |
| 578 | + |
| 579 | + days = floor(hours / 24.0); |
| 580 | + hours -= 24.0 * days; |
| 581 | + printf(_("Time: %.3f ms (%.0f d %02d:%02d:%06.3f)\n"), |
| 582 | + elapsed_msec, days, (int) hours, (int) minutes, seconds); |
| 583 | +} |
| 584 | + |
| 585 | + |
534 | 586 | /*
|
535 | 587 | * PSQLexec
|
536 | 588 | *
|
@@ -679,7 +731,7 @@ PSQLexecWatch(const char *query, const printQueryOpt *opt)
|
679 | 731 |
|
680 | 732 | /* Possible microtiming output */
|
681 | 733 | if (pset.timing)
|
682 |
| - printf(_("Time: %.3f ms\n"), elapsed_msec); |
| 734 | + PrintTiming(elapsed_msec); |
683 | 735 |
|
684 | 736 | return 1;
|
685 | 737 | }
|
@@ -1332,7 +1384,7 @@ SendQuery(const char *query)
|
1332 | 1384 |
|
1333 | 1385 | /* Possible microtiming output */
|
1334 | 1386 | if (pset.timing)
|
1335 |
| - printf(_("Time: %.3f ms\n"), elapsed_msec); |
| 1387 | + PrintTiming(elapsed_msec); |
1336 | 1388 |
|
1337 | 1389 | /* check for events that may occur during query execution */
|
1338 | 1390 |
|
|
0 commit comments