Skip to content

Commit 4a3e0eb

Browse files
UPDATE
1 parent 88298e7 commit 4a3e0eb

File tree

3 files changed

+113
-22
lines changed

3 files changed

+113
-22
lines changed

Capsule/TimeHelper.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,30 @@
88
use Tamedevelopers\Support\Str;
99
use Tamedevelopers\Support\Country;
1010

11+
/**
12+
* Class TimeHelper
13+
*
14+
* Utilities for timezone configuration, date parsing, and formatting helpers
15+
* used by Time. This class also carries lightweight range formatting support
16+
* via startDate/endDate/format passed from Time::dateRange.
17+
*/
1118
class TimeHelper {
1219

1320
/**
14-
* startDate
21+
* Start of the range date (as Time instance)
1522
*
1623
* @var mixed
1724
*/
1825
protected $startDate;
1926
/**
20-
* endDate
27+
* End of the range date (as Time instance)
2128
*
2229
* @var mixed
2330
*/
2431
protected $endDate;
2532

2633
/**
27-
* format
34+
* Output format for range formatting.
2835
*
2936
* @var string|null
3037
*/

Time.php

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,57 @@
1010
use Tamedevelopers\Support\Traits\TimeTrait;
1111
use Tamedevelopers\Support\Capsule\TimeHelper;
1212

13+
/**
14+
* Class Time
15+
*
16+
* Time utility with dynamic static/instance method support.
17+
* - Supports setters like date(), now(), today(), yesterday()
18+
* - Add/subtract helpers: addSeconds/Minutes/Hours/Days/Weeks/Months/Years and sub*
19+
* - Formatting: format(), toDateTimeString(), toJsTimer(), timestamp()
20+
* - Range helper: dateRange()
21+
* - Text features/config: config(), greeting (via __greeting), timeAgo (via __timeAgo)
22+
*
23+
* All methods are documented at their definitions for clarity.
24+
*/
1325
class Time {
1426

1527
use TimeTrait;
1628

1729
/**
18-
* For storing the time value
19-
*
20-
* @var mixed
21-
* - int|string
30+
* For storing the time value.
31+
*
32+
* @var int|string
2233
*/
2334
protected $date;
2435

2536
/**
26-
* For storing the timestamp value
27-
*
37+
* For storing a human-friendly timestamp snapshot
38+
* (e.g., "2024-01-01 12:00:00.123456 UTC (+00:00)").
39+
*
2840
* @var string
2941
*/
3042
protected $timestamp;
3143

3244
/**
33-
* For storing the timezone value
34-
*
45+
* For storing the timezone value.
46+
*
3547
* @var string
3648
*/
3749
protected $timezone;
50+
51+
/**
52+
* Cached timezone name (alias of $timezone) for quick access.
53+
*
54+
* @var string|null
55+
*/
56+
protected $timezoneName;
57+
58+
/**
59+
* Cached UTC offset for the current $date (e.g., "+00:00").
60+
*
61+
* @var string|null
62+
*/
63+
protected $utcOffset;
3864

3965
/**
4066
* static
@@ -75,6 +101,13 @@ public function __construct($date = null, $timezone = null)
75101
*
76102
* @return mixed
77103
*/
104+
/**
105+
* Magic: instance dynamic calls map to supported methods.
106+
*
107+
* @param string $name Invoked method name
108+
* @param array $args Arguments
109+
* @return mixed
110+
*/
78111
public function __call($name, $args)
79112
{
80113
return self::nonExistMethod($name, $args, $this);
@@ -87,6 +120,13 @@ public function __call($name, $args)
87120
*
88121
* @return mixed
89122
*/
123+
/**
124+
* Magic: static dynamic calls map to supported methods using stored static instance.
125+
*
126+
* @param string $name Invoked static method name
127+
* @param array $args Arguments
128+
* @return mixed
129+
*/
90130
public static function __callStatic($name, $args)
91131
{
92132
return self::nonExistMethod($name, $args, self::$staticData);
@@ -97,6 +137,12 @@ public static function __callStatic($name, $args)
97137
* @param int $value
98138
* @return $this
99139
*/
140+
/**
141+
* Add seconds to the current date.
142+
*
143+
* @param int $value Number of seconds to add
144+
* @return $this New cloned instance with updated time
145+
*/
100146
public function addSeconds($value = 0)
101147
{
102148
return $this->buildTimeModifier('second', $value);
@@ -649,6 +695,12 @@ public function __timeAgo($mode = null)
649695
*
650696
* @return mixed
651697
*/
698+
/**
699+
* Retrieve text configuration entries.
700+
*
701+
* @param string|null $mode Specific key to fetch or null for all
702+
* @return mixed
703+
*/
652704
private static function getText($mode = null)
653705
{
654706
if(!defined('TAME_TIME_CONFIG')){
@@ -657,5 +709,32 @@ private static function getText($mode = null)
657709

658710
return TAME_TIME_CONFIG[$mode] ?? TAME_TIME_CONFIG;
659711
}
712+
713+
/**
714+
* Magic: customize what is displayed during var-dump/dd().
715+
* Provides a pretty, safe snapshot of the current time object.
716+
*
717+
* - timestamp: the unix timestamp (int)
718+
* - formatted: default formatted string (Y-m-d H:i:s)
719+
* - timezone: current timezone name
720+
* - utc_offset: offset at the timestamp time
721+
* - greeting: localized greeting based on hour
722+
* - time_ago_short: a compact time-ago string
723+
*
724+
* @return array
725+
*/
726+
public function __debugInfo(): array
727+
{
728+
$time = (int) $this->date;
729+
730+
return [
731+
'timestamp' => $time,
732+
'formatted' => date('Y-m-d H:i:s', $time),
733+
'timezone' => (string) $this->timezone,
734+
'utc_offset' => date('(P)', $time),
735+
'greeting' => $this->__greeting($time),
736+
'time_ago_short' => $this->__timeAgo('short'),
737+
];
738+
}
660739
}
661740

Traits/TimeTrait.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
use Tamedevelopers\Support\Country;
1212
use Tamedevelopers\Support\Capsule\TimeHelper;
1313

14+
1415
/**
16+
* Trait TimeTrait
17+
*
18+
* Internal helpers used by the Time class for cloning, timezone handling,
19+
* and common operations. Public API is provided by Time and dynamic dispatch.
1520
* @property mixed $staticData
1621
*/
1722
trait TimeTrait{
1823

1924
/**
20-
* isTimeInstance
25+
* Determine whether the static context already holds a Time instance.
2126
*
2227
* @return bool
2328
*/
@@ -27,9 +32,9 @@ static protected function isTimeInstance()
2732
}
2833

2934
/**
30-
* Clone a new Instance of self
35+
* Clone a new instance of the owning class.
3136
*
32-
* @return $this
37+
* @return $this A shallow clone of the current instance.
3338
*/
3439
private function clone()
3540
{
@@ -39,7 +44,7 @@ private function clone()
3944
/**
4045
* Alias for clone() method.
4146
*
42-
* @return $this
47+
* @return $this A shallow clone of the current instance.
4348
*/
4449
public function copy()
4550
{
@@ -80,13 +85,13 @@ private function buildTimeModifier($mode = 'day', $value = 0, $sub = false)
8085
$sign = $sub ? '-' : '+';
8186

8287
$text = match ($mode) {
83-
'second', => $value <= 1 ? 'second' : 'seconds',
84-
'minute', => $value <= 1 ? 'minute' : 'minutes',
85-
'hour', => $value <= 1 ? 'hour' : 'hours',
86-
'week', => $value <= 1 ? 'week' : 'weeks',
87-
'month', => $value <= 1 ? 'month' : 'months',
88-
'year', => $value <= 1 ? 'year' : 'years',
89-
default => $value <= 1 ? 'day' : 'days',
88+
'second' => $value <= 1 ? 'second' : 'seconds',
89+
'minute' => $value <= 1 ? 'minute' : 'minutes',
90+
'hour' => $value <= 1 ? 'hour' : 'hours',
91+
'week' => $value <= 1 ? 'week' : 'weeks',
92+
'month' => $value <= 1 ? 'month' : 'months',
93+
'year' => $value <= 1 ? 'year' : 'years',
94+
default => $value <= 1 ? 'day' : 'days',
9095
};
9196

9297
// format text

0 commit comments

Comments
 (0)