Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit 591a16e

Browse files
committed
Fix #18
1 parent 14dffb8 commit 591a16e

File tree

5 files changed

+94
-21
lines changed

5 files changed

+94
-21
lines changed

core/CLI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private function overview(){
132132
),
133133
array(
134134
'' => 'Worked until now',
135-
'Value' => Stats::secToTime($current->getValue(['lastopend']) - $current->getValue(['begin']))
135+
'Value' => Time::secToTime($current->getValue(['lastopend']) - $current->getValue(['begin']))
136136
)
137137
));
138138
}

core/Settings.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private function edit(array $commands) : void {
159159
array(
160160
'Name: ' . CLIOutput::colorString( $s->getValue([$id, 'name']), CLIOutput::YELLOW),
161161
'Category: ' . CLIOutput::colorString( $s->getValue([$id, 'category']), CLIOutput::YELLOW),
162-
'Duration: ' . CLIOutput::colorString( Stats::secToTime( $s->getValue([$id, 'end']) - $s->getValue([$id, 'begin']) ) , CLIOutput::YELLOW),
162+
'Duration: ' . CLIOutput::colorString( Time::secToTime( $s->getValue([$id, 'end']) - $s->getValue([$id, 'begin']) ) , CLIOutput::YELLOW),
163163
)
164164
), null, 1);
165165
$this->editSingle($id, $s);
@@ -171,7 +171,7 @@ private function edit(array $commands) : void {
171171
'ID' => strval($id),
172172
'Name' => $d['name'],
173173
'Category' => $d['category'],
174-
'Duration' => Stats::secToTime( $d['end'] - $d['begin'] )
174+
'Duration' => Time::secToTime( $d['end'] - $d['begin'] )
175175
);
176176
}
177177
$this->output->table($list);

core/Stats.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private function printDataset(array $data) : void {
220220
);
221221

222222
foreach( $table as &$d ){
223-
$d['Time'] = self::secToTime($d['Time']);
223+
$d['Time'] = Time::secToTime($d['Time']);
224224
}
225225

226226
$this->output->table($table);
@@ -232,20 +232,7 @@ private function printDataset(array $data) : void {
232232
ExtensionEventHandler::statsViewed($data, $this->output);
233233
}
234234

235-
public static function secToTime(int $t) : string {
236-
return str_pad(
237-
($t >= 3600 ? intval($t/3600) . 'h ' : '' ) .
238-
str_pad(
239-
intval(($t % 3600) / 60) . 'm',
240-
3,
241-
" ",
242-
STR_PAD_LEFT
243-
),
244-
8,
245-
" ",
246-
STR_PAD_LEFT
247-
);
248-
}
235+
249236

250237
private function printTodayView(array $data) : void {
251238
array_multisort(
@@ -275,7 +262,7 @@ private function printTodayView(array $data) : void {
275262
}
276263

277264
foreach( $table as &$d ){
278-
$d['Time'] = self::secToTime($d['Time']);
265+
$d['Time'] = Time::secToTime($d['Time']);
279266
}
280267

281268
$this->output->table($table);

core/Time.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
class Time {
3+
4+
private const DEFAULT_FORMAT = 'dhm|s';
5+
private const ALLOWED_PARTS = array(
6+
'd' => 24*60*60,
7+
'h' => 60*60,
8+
'm' => 60,
9+
's' => 1
10+
);
11+
private const DELIMITER = '|';
12+
private const PAD_VALUE = 4;
13+
14+
private static ?Time $time = null;
15+
private string $timeformat = "";
16+
17+
public function __construct() {
18+
// write to config
19+
$r = Config::getStorageReader('config');
20+
if( !$r->isValue(['timeformat']) ){
21+
$r->setValue(['timeformat'], self::DEFAULT_FORMAT );
22+
}
23+
$this->timeformat = $r->getValue(['timeformat']);
24+
25+
// check syntax
26+
$ok = true;
27+
foreach(str_split($this->timeformat) as $char){
28+
if( !in_array($char, array_keys(self::ALLOWED_PARTS), true) && $char !== self::DELIMITER ){
29+
$ok = false;
30+
}
31+
}
32+
$ok = $ok &&
33+
$this->timeformat[0] !== self::DELIMITER &&
34+
substr($this->timeformat, -1) !== self::DELIMITER &&
35+
str_replace('||', '', $this->timeformat) === $this->timeformat;
36+
37+
if(!$ok){
38+
$this->timeformat = self::DEFAULT_FORMAT;
39+
echo "ERROR: Invalid timeformat in config.json" . PHP_EOL;
40+
}
41+
42+
// calculate pad
43+
$this->padResult = self::PAD_VALUE * max(array_map( 'strlen', explode(self::DELIMITER, $this->timeformat)));
44+
}
45+
46+
private function formatTime(int $time, string $format) : array {
47+
$d = array();
48+
$notEmpty = false;
49+
foreach( str_split($format) as $k => $f ){
50+
$c = intval($time / self::ALLOWED_PARTS[$f]);
51+
if( $c > 0 || $notEmpty){
52+
$d[$f] = $c;
53+
$time = intval($time % self::ALLOWED_PARTS[$f]);
54+
$notEmpty = true;
55+
}
56+
}
57+
return $d;
58+
}
59+
60+
private function formatAsString(array $duration){
61+
$s = "";
62+
foreach( $duration as $name => $value){
63+
$s .= str_pad( " " . $value . $name, self::PAD_VALUE, " ", STR_PAD_LEFT);
64+
}
65+
return str_pad( $s, $this->padResult, " ", STR_PAD_LEFT);
66+
}
67+
68+
private function getDurationString(int $t) : string {
69+
foreach(explode(self::DELIMITER, $this->timeformat) as $format){
70+
$v = $this->formatTime($t, $format);
71+
//print_r($v);
72+
if( array_sum($v) > 0 ){
73+
return $this->formatAsString($v);
74+
}
75+
}
76+
return str_pad( "0", $this->padResult, " ", STR_PAD_LEFT);
77+
}
78+
79+
public static function secToTime(int $t) : string {
80+
if(self::$time === null){
81+
self::$time = new Time();
82+
}
83+
return self::$time->getDurationString($t);
84+
}
85+
}
86+
?>

extensions/statsSum/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ public static function statsViewed(array $data, \CLIOutput $output) : void {
2424
foreach( $sums as $k => $s ){
2525
$tab[] = array(
2626
'Category' => $k,
27-
'Sum' => \Stats::secToTime($s)
27+
'Sum' => \Time::secToTime($s)
2828
);
2929
}
3030

3131
$output->table($tab);
3232
$output->print(array(
33-
'Total sum: ' . trim(\Stats::secToTime(array_sum($sums)))
33+
'Total sum: ' . trim(\Time::secToTime(array_sum($sums)))
3434
), \CLIOutput::RED);
3535
}
3636
}

0 commit comments

Comments
 (0)