Skip to content

Commit b76e66e

Browse files
committed
Duration: update other format methods to support negative dates
This will let us remove some special-case logic from Timewarrior.
1 parent 5df98cb commit b76e66e

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/Duration.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,18 @@ std::string Duration::format () const
403403
if (_period)
404404
{
405405
time_t t = _period;
406+
407+
std::stringstream s;
408+
if (t < 0) {
409+
s << '-';
410+
t *= -1;
411+
}
412+
406413
int seconds = t % 60; t /= 60;
407414
int minutes = t % 60; t /= 60;
408415
int hours = t % 24; t /= 24;
409416
int days = t;
410417

411-
std::stringstream s;
412418
if (days)
413419
s << days << "d ";
414420

@@ -432,11 +438,17 @@ std::string Duration::formatHours () const
432438
if (_period)
433439
{
434440
time_t t = _period;
441+
442+
std::stringstream s;
443+
if (t < 0) {
444+
s << '-';
445+
t *= -1;
446+
}
447+
435448
int seconds = t % 60; t /= 60;
436449
int minutes = t % 60; t /= 60;
437450
int hours = t;
438451

439-
std::stringstream s;
440452
s << hours
441453
<< ':'
442454
<< std::setw (2) << std::setfill ('0') << minutes
@@ -501,16 +513,23 @@ std::string Duration::formatISO () const
501513
//
502514
std::string Duration::formatVague (bool padding) const
503515
{
516+
time_t t = _period;
504517
float days = (float) _period / 86400.0;
505518

506519
std::stringstream formatted;
507-
if (_period >= 86400 * 365) formatted << std::fixed << std::setprecision (1) << (days / 365) << (padding ? "y " : "y");
508-
else if (_period >= 86400 * 90) formatted << static_cast <int> (days / 30) << (padding ? "mo " : "mo");
509-
else if (_period >= 86400 * 14) formatted << static_cast <int> (days / 7) << (padding ? "w " : "w");
510-
else if (_period >= 86400) formatted << static_cast <int> (days) << (padding ? "d " : "d");
511-
else if (_period >= 3600) formatted << static_cast <int> (_period / 3600) << (padding ? "h " : "h");
512-
else if (_period >= 60) formatted << static_cast <int> (_period / 60) << "min"; // Longest suffix - no padding
513-
else if (_period >= 1) formatted << static_cast <int> (_period) << (padding ? "s " : "s");
520+
if (t < 0) {
521+
formatted << '-';
522+
t *= -1;
523+
days *= -1.0;
524+
}
525+
526+
if (t >= 86400 * 365) formatted << std::fixed << std::setprecision (1) << (days / 365) << (padding ? "y " : "y");
527+
else if (t >= 86400 * 90) formatted << static_cast <int> (days / 30) << (padding ? "mo " : "mo");
528+
else if (t >= 86400 * 14) formatted << static_cast <int> (days / 7) << (padding ? "w " : "w");
529+
else if (t >= 86400) formatted << static_cast <int> (days) << (padding ? "d " : "d");
530+
else if (t >= 3600) formatted << static_cast <int> (t / 3600) << (padding ? "h " : "h");
531+
else if (t >= 60) formatted << static_cast <int> (t / 60) << "min"; // Longest suffix - no padding
532+
else if (t >= 1) formatted << static_cast <int> (t) << (padding ? "s " : "s");
514533

515534
return formatted.str ();
516535
}

0 commit comments

Comments
 (0)