Skip to content

Commit 905840c

Browse files
UPDATE
1 parent 4a3e0eb commit 905840c

File tree

4 files changed

+44
-50
lines changed

4 files changed

+44
-50
lines changed

Capsule/TimeHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public static function setPassedDate($date = null)
129129

130130
// if instance of Carbon
131131
// then convert to date time
132-
if($date instanceof carbonInstance){
132+
if($date instanceof carbonInstance || (is_object($date) && method_exists($date, 'toDateTimeString'))){
133133
$date = $date->toDateTimeString();
134134
}
135135

Tests/time.php

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,38 @@
1717
time: 'now',
1818
);
1919

20-
// $test = Time::setDate('2025-01-14 11:11:12');
21-
// [
22-
// $test->time(),
23-
// $test->sec(),
24-
// $test->min(),
25-
// $test->hour(),
26-
// $test->day(),
27-
// $test->week(),
28-
// ]
29-
3020
// Time::allTimezone(),
3121

3222
$time2 = (new Time)->setTimezone('America/St_Barthelemy');
3323
$time3 = (new Time)->setTimezone('Indian/Antananarivo');
3424
$time4 = TameTime('first day of December 2008', 'Pacific/Pago_Pago');
3525

26+
// [
27+
// $time->toJsTimer('24 Jan 2025 14:00:00'),
28+
// $time4->time(),
29+
// $time4->sec(),
30+
// $time4->min(),
31+
// $time4->hour(),
32+
// $time4->day(),
33+
// $time4->week(),
34+
// $time4->month(),
35+
// $time4->year(),
36+
// ];
37+
3638
dd(
3739
[
38-
$time->toJsTimer('24 Jan 2025 14:00:00'),
39-
$time4->time(),
40-
$time4->sec(),
41-
$time4->min(),
42-
$time4->hour(),
43-
$time4->day(),
44-
$time4->week(),
45-
$time4->month(),
46-
$time4->year(),
40+
'Time-1: '. $time->getTimeZone(),
41+
'Time-2: '. $time2->getTimeZone(),
42+
'Time-3: '. $time3->getTimeZone(),
43+
'Time-4: '. $time4->getTimeZone(),
4744
],
4845

49-
[
50-
$time->getTimeZone(),
51-
$time2->getTimeZone(),
52-
$time3->getTimeZone(),
53-
$time4->getTimeZone(),
54-
],
55-
56-
[
57-
$time3->date('first day of December 2008')->format(),
58-
$time3->yesterday()->format(),
59-
$time3->today()->format(),
60-
$time3->now()->format(),
61-
],
46+
// [
47+
// $time3->date('first day of December 2008')->format(),
48+
// $time3->yesterday()->format(),
49+
// $time3->today()->format(),
50+
// $time3->now()->format(),
51+
// ],
6252

6353
$time4->date('first day of this month')->subDays(4),
6454

@@ -67,24 +57,26 @@
6757
$time2->greetings('now'),
6858
$time3->greetings('24 Jan 2025 14:00:00'),
6959
TameTime()->toJs('today 9:23pm'),
70-
$time->date('last year december')->format(),
71-
$time4->addMonth(10)->addWeek(2)->format(),
72-
$time4->addDay(2000)->ago('date'),
73-
$time4->subDay(10)->ago('date'),
74-
$time4->addYear(10)->ago('date'),
75-
$time4->date('last week monday')->ago('date_time'),
76-
$time4->date('last year december')->diff('weeks'),
60+
// $time->date('last year december')->format(),
61+
// $time->addMonth(10)->addWeek(2)->format(),
62+
// $time4->addDay(20000)->ago('date'),
63+
// $time4->subDay(10)->ago('date'),
64+
// $time4->addYear(10)->ago('date'),
65+
// $time4->date('last week monday')->ago('date_time'),
66+
// $time4->date('last year december')->diff('weeks'),
7767
],
7868

69+
$time4->setTimezone('Indian/Antananarivo'),
70+
7971
[
8072
$time4->yesterday()->toDateTimeString(),
8173
$time4->format(null, 'first day of December 2008'),
8274
$time4->date('last year december')->diff('weeks'),
83-
$time4->setTimezone('Indian/Antananarivo')->diffBetween('last year', 'today', 'weeks'),
8475
$time4->date('today')->ago(),
76+
// $time4->setTimezone('Indian/Antananarivo')->diffBetween('last year', 'today', 'weeks'),
8577
],
8678

87-
79+
8880
TameTime()->range('1-14'),
8981
TameTime()->range('0-40')->format(true, true),
9082
//

Time.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,8 @@ public function __debugInfo(): array
730730
return [
731731
'timestamp' => $time,
732732
'formatted' => date('Y-m-d H:i:s', $time),
733-
'timezone' => (string) $this->timezone,
734-
'utc_offset' => date('(P)', $time),
733+
'timezone' => (string) ($this->timezoneName ?? $this->timezone),
734+
'utc_offset' => ($this->utcOffset ?? date('(P)', $time)),
735735
'greeting' => $this->__greeting($time),
736736
'time_ago_short' => $this->__timeAgo('short'),
737737
];

Traits/TimeTrait.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,13 @@ private function setTimeZoneAndTimeStamp($timezone = null)
174174
*/
175175
private function timestampPrint()
176176
{
177-
// get timezone
177+
// ensure timezone cached
178178
$this->timezone = $this->getTimeZone();
179+
$this->timezoneName = $this->timezone;
179180

180-
// set timezone
181+
// refresh system timezone and cache UTC offset
181182
$this->setTimeZoneAndTimeStamp($this->timezone);
183+
$this->utcOffset = date('(P)', (int) $this->date);
182184

183185
return $this->buildTimePrint();
184186
}
@@ -190,8 +192,8 @@ private function timestampPrint()
190192
*/
191193
private function buildTimePrint()
192194
{
193-
$date = date('Y-m-d H:i:s', $this->date);
194-
$utc = date('(P)', $this->date);
195+
$date = date('Y-m-d H:i:s', (int) $this->date);
196+
$utc = $this->utcOffset ?? date('(P)', (int) $this->date);
195197

196198
return "{$date}.{$this->microseconds()} {$this->timezone} {$utc}";
197199
}
@@ -206,8 +208,8 @@ public function debugTimestamp(): string
206208
{
207209
$date = $this->format('Y-m-d H:i:s');
208210
$micro = $this->microseconds();
209-
$tz = $this->timezone->getName();
210-
$offset = $this->format('(P)');
211+
$tz = (string) $this->__getTimezone();
212+
$offset = date('(P)', (int) $this->date);
211213
return sprintf('%s.%s %s %s', $date, $micro, $tz, $offset);
212214
}
213215

0 commit comments

Comments
 (0)