From: Nik Okuntseff Date: Fri, 19 Apr 2019 20:02:40 +0000 (+0000) Subject: Initial work done to support negative durations, some issues remain. X-Git-Tag: timetracker_1.19-1~67 X-Git-Url: http://wagnertech.de/git?p=timetracker.git;a=commitdiff_plain;h=87a40bf7dc910c008aa6aadf8882b070ad120c39 Initial work done to support negative durations, some issues remain. --- diff --git a/WEB-INF/lib/common.lib.php b/WEB-INF/lib/common.lib.php index e2950753..747f3e01 100644 --- a/WEB-INF/lib/common.lib.php +++ b/WEB-INF/lib/common.lib.php @@ -143,11 +143,6 @@ function time_to_decimal($val) { return $decimalTime; } -function sec_to_time_fmt_hm($sec) -{ - return sprintf("%d:%02d", $sec / 3600, $sec % 3600 / 60); -} - function magic_quotes_off() { $_POST = array_map('stripslashes_deep', $_POST); diff --git a/WEB-INF/lib/ttChartHelper.class.php b/WEB-INF/lib/ttChartHelper.class.php index 528fcdb0..efb30c60 100644 --- a/WEB-INF/lib/ttChartHelper.class.php +++ b/WEB-INF/lib/ttChartHelper.class.php @@ -27,6 +27,7 @@ // +----------------------------------------------------------------------+ import('Period'); +import('ttTimeHelper'); // Definitions for chart types. define('CHART_PROJECTS', 1); @@ -97,7 +98,7 @@ class ttChartHelper { // Add a string representation of time + percentage to names. Example: "Time Tracker (1:15 - 6%)". foreach ($result as &$one_val) { $percent = round(100*$one_val['time']/$total).'%'; - $one_val['name'] .= ' ('.sec_to_time_fmt_hm($one_val['time']).' - '.$percent.')'; + $one_val['name'] .= ' ('.ttTimeHelper::minutesToDuration($one_val['time'] / 60).' - '.$percent.')'; } // Note: the remaining code here is needed to display labels on the side of the diagram. diff --git a/WEB-INF/lib/ttReportHelper.class.php b/WEB-INF/lib/ttReportHelper.class.php index 9c6dfb0f..5fd22531 100644 --- a/WEB-INF/lib/ttReportHelper.class.php +++ b/WEB-INF/lib/ttReportHelper.class.php @@ -513,7 +513,7 @@ class ttReportHelper { $res = $mdb2->query($sql); if (is_a($res, 'PEAR_Error')) die($res->getMessage()); while ($val = $res->fetchRow()) { - $time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null; + $time = $val['time'] ? ttTimeHelper::minutesToDuration($val['time'] / 60) : null; $rowLabel = ttReportHelper::makeGroupByLabel($val['group_field'], $options); if ($options['show_cost']) { $decimalMark = $user->getDecimalMark(); @@ -595,7 +595,7 @@ class ttReportHelper { if (is_a($res, 'PEAR_Error')) die($res->getMessage()); $val = $res->fetchRow(); - $total_time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null; + $total_time = $val['time'] ? ttTimeHelper::minutesToDuration($val['time'] / 60) : null; if ($options['show_cost']) { $total_cost = $val['cost']; if (!$total_cost) $total_cost = '0.00'; diff --git a/WEB-INF/lib/ttTimeHelper.class.php b/WEB-INF/lib/ttTimeHelper.class.php index 5745128f..3cef8b44 100644 --- a/WEB-INF/lib/ttTimeHelper.class.php +++ b/WEB-INF/lib/ttTimeHelper.class.php @@ -132,17 +132,21 @@ class ttTimeHelper { if (!isset($duration) || strlen($duration) == 0) return null; // Value is not set. Caller decides whether it is valid or not. + // We allow negative durations, similar to negative expenses (installments). + $signMultiplier = ttStartsWith($duration, '-') ? -1 : 1; + if ($signMultiplier == -1) $duration = ltrim($duration, '-'); + // Handle whole hours. if (preg_match('/^\d{1,3}h?$/', $duration )) { // 0 - 999, 0h - 999h $minutes = 60 * trim($duration, 'h'); - return $minutes > $max ? false : $minutes; + return $minutes > $max ? false : $signMultiplier * $minutes; } // Handle a normalized duration value. if (preg_match('/^\d{1,3}:[0-5][0-9]$/', $duration )) { // 0:00 - 999:59 $time_array = explode(':', $duration); $minutes = (int)@$time_array[1] + ((int)@$time_array[0]) * 60; - return $minutes > $max ? false : $minutes; + return $minutes > $max ? false : $signMultiplier * $minutes; } // Handle localized fractional hours. @@ -153,13 +157,13 @@ class ttTimeHelper { $duration = str_replace (',', '.', $duration); $minutes = (int)round(60 * floatval($duration)); - return $minutes > $max ? false : $minutes; + return $minutes > $max ? false : $signMultiplier * $minutes; } // Handle minutes. Some users enter durations like 10m (meaning 10 minutes). if (preg_match('/^\d{1,5}m$/', $duration )) { // 0m - 99999m $minutes = (int) trim($duration, 'm'); - return $minutes > $max ? false : $minutes; + return $minutes > $max ? false : $signMultiplier * $minutes; } // Everything else is not a valid duration. @@ -169,16 +173,17 @@ class ttTimeHelper { // minutesToDuration converts an integer number of minutes into duration string. // Formats returned HH:MM, HHH:MM, HH, or HHH. static function minutesToDuration($minutes, $abbreviate = false) { - if ($minutes < 0) return false; + $sign = $minutes > 0 ? '' : '-'; + $minutes = abs($minutes); $hours = (string) (int)($minutes / 60); $mins = (string) round(fmod($minutes, 60)); if (strlen($mins) == 1) $mins = '0' . $mins; if ($abbreviate && $mins == '00') - return $hours; + return $sign.$hours; - return $hours.':'.$mins; + return $sign.$hours.':'.$mins; } // toMinutes - converts a time string in format 00:00 to a number of minutes. @@ -552,7 +557,7 @@ class ttTimeHelper { $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { $val = $res->fetchRow(); - return sec_to_time_fmt_hm($val['sm']); + return ttTimeHelper::minutesToDuration($val['sm'] / 60); } return false; } @@ -574,7 +579,7 @@ class ttTimeHelper { $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { $val = $res->fetchRow(); - return sec_to_time_fmt_hm($val['sm']); + return ttTimeHelper::minutesToDuration($val['sm'] / 60); } return false; } @@ -596,7 +601,7 @@ class ttTimeHelper { $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { $val = $res->fetchRow(); - return sec_to_time_fmt_hm($val['sm']); + return ttTimeHelper::minutesToDuration($val['sm'] / 60); } return false; } diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index 8a059ce3..b41511a0 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.19.1.4962 | Copyright © Anuko | +  Anuko Time Tracker 1.19.1.4963 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve}