Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git

Simplifies code

AuthorDave Jarvis <email>
Date2026-02-14 14:29:23 GMT-0800
Commitbbf6a606038486c91141216bac2bacc8e0ecd1e3
Parentbaa3605
render/FileRenderer.php
public function renderTime( int $timestamp ): void {
- if( !$timestamp ) {
- echo 'never';
-
- return;
- }
-
- $diff = time() - $timestamp;
-
- if( $diff < 5 ) {
- echo 'just now';
-
- return;
- }
-
$tokens = [
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
];
-
- foreach($tokens as $unit => $text) {
- if( $diff < $unit ) continue;
+ $diff = $timestamp ? time() - $timestamp : null;
+ $result = 'never';
- $num = floor( $diff / $unit );
- echo $num . ' ' . $text . (($num > 1)? 's': '') . ' ago';
+ if( $diff && $diff >= 5 ) {
+ foreach( $tokens as $unit => $text ) {
+ if( $diff < $unit ) {
+ continue;
+ }
- return;
+ $num = floor( $diff / $unit );
+ $result = $num . ' ' . $text . ($num > 1 ? 's' : '') . ' ago';
+ break;
+ }
+ } elseif( $diff ) {
+ $result = 'just now';
}
- echo 'just now';
+ echo $result;
}
public function renderSize( int $bytes ): void {
echo $this->formatSize($bytes);
}
-
- private function formatSize( int $bytes ): string {
- if( $bytes === 0 ) {
- return '0 B';
- }
+ private function formatSize(int $bytes): string {
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$i = 0;
- while( $bytes >= 1024 && $i < count($units) - 1 ) {
+ while ($bytes >= 1024 && $i < count($units) - 1) {
$bytes /= 1024;
$i++;
}
- return round($bytes, 2) . ' ' . $units[$i];
+ return ($bytes === 0 ? 0 : round($bytes)) . ' ' . $units[$i];
}
}
Delta17 lines added, 28 lines removed, 11-line decrease