Skip to content

Commit 88298e7

Browse files
UPDATE
1 parent c015627 commit 88298e7

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

Capsule/TimeHelper.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct($startDate = null, $endDate = null, $format = null)
4747
}
4848

4949
/**
50-
* format
50+
* Format the range.
5151
*
5252
* @param bool $start Whether to return the start date (true) or the end date (false).
5353
* @param bool $year Whether to include the year in the result.
@@ -75,10 +75,10 @@ public function format($start = false, $year = false)
7575
}
7676

7777
/**
78-
* Set the timezone.
79-
* @param string|null $timezone
80-
*
81-
* @return string
78+
* Configure and return a valid timezone string. Falls back to system default or UTC.
79+
*
80+
* @param string|null $timezone IANA timezone name or null. If invalid, fallback is used.
81+
* @return string Valid IANA timezone identifier.
8282
*/
8383
public static function configureAndSetTimezone($timezone = null)
8484
{
@@ -87,13 +87,14 @@ public static function configureAndSetTimezone($timezone = null)
8787
if(!empty($timezone) && in_array($timezone, Country::timeZone())){
8888
$timezone = $timezone;
8989
} else{
90-
$timezone = date_default_timezone_get() ?? 'UTC';
90+
$timezone = date_default_timezone_get() ?: 'UTC';
9191
}
9292

9393
try {
9494
date_default_timezone_set($timezone);
9595
} catch (\Throwable $th) {
96-
date_default_timezone_set('UTC');
96+
$timezone = 'UTC';
97+
date_default_timezone_set($timezone);
9798
}
9899

99100
return $timezone;
@@ -143,4 +144,16 @@ public static function carbonInstance($date)
143144
return $date?->timestamp ?? $date;
144145
}
145146

147+
/**
148+
* Build a microsecond string (6 digits) for pretty prints.
149+
*
150+
* @return string e.g. "123456"
151+
*/
152+
public static function microseconds(): string
153+
{
154+
$micro = explode(' ', microtime());
155+
$micros = (int) round(((float) ($micro[0] ?? 0)) * 1_000_000);
156+
return str_pad((string) $micros, 6, '0', STR_PAD_LEFT);
157+
}
158+
146159
}

Traits/TimeTrait.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ public function greaterThanOrEqualTo($time): bool
6565
}
6666

6767
/**
68-
* build Time Modifier
68+
* Modify builder for add/sub operations.
6969
*
70-
* @param string $mode
71-
* @param int $value
72-
* @param bool $sub
73-
* @return $clone
70+
* @param string $mode second|minute|hour|day|week|month|year
71+
* @param int $value Amount to adjust
72+
* @param bool $sub If true, subtract instead of add
73+
* @return static
7474
*/
7575
private function buildTimeModifier($mode = 'day', $value = 0, $sub = false)
7676
{
7777
$clone = $this->clone();
78-
$date = $clone->format();
78+
$date = $clone->format("Y-m-d H:i:s");
7979
$mode = Str::lower($mode);
80-
$sign = !$sub ? '+' : '-';
80+
$sign = $sub ? '-' : '+';
8181

8282
$text = match ($mode) {
8383
'second', => $value <= 1 ? 'second' : 'seconds',
@@ -163,7 +163,7 @@ private function setTimeZoneAndTimeStamp($timezone = null)
163163
}
164164

165165
/**
166-
* create timestamp
166+
* timestampPrint(): rebuilds debug-like timestamp string.
167167
*
168168
* @return string
169169
*/
@@ -179,7 +179,7 @@ private function timestampPrint()
179179
}
180180

181181
/**
182-
* buildTimePrint
182+
* Pretty string including microseconds, tz and UTC offset (compat buildTimePrint())
183183
*
184184
* @return string
185185
*/
@@ -190,6 +190,21 @@ private function buildTimePrint()
190190

191191
return "{$date}.{$this->microseconds()} {$this->timezone} {$utc}";
192192
}
193+
194+
/**
195+
* Return a pretty timestamp with microseconds, timezone, and UTC offset.
196+
* Example: "2025-05-01 10:20:30.123456 America/New_York (-04:00)"
197+
*
198+
* @return string
199+
*/
200+
public function debugTimestamp(): string
201+
{
202+
$date = $this->format('Y-m-d H:i:s');
203+
$micro = $this->microseconds();
204+
$tz = $this->timezone->getName();
205+
$offset = $this->format('(P)');
206+
return sprintf('%s.%s %s %s', $date, $micro, $tz, $offset);
207+
}
193208

194209
/**
195210
* create microseconds
@@ -198,10 +213,9 @@ private function buildTimePrint()
198213
*/
199214
private function microseconds()
200215
{
201-
$microtime = explode(' ', microtime());
202-
$milliseconds = (int) round($microtime[0] * 1000000); // Get microseconds
203-
204-
return str_pad(Str::trim($milliseconds), 6, '0', STR_PAD_LEFT);
216+
$micro = explode(' ', microtime());
217+
$micros = (int) round(((float) ($micro[0] ?? 0)) * 1_000_000);
218+
return str_pad((string) $micros, 6, '0', STR_PAD_LEFT);
205219
}
206220

207221
/**
@@ -287,7 +301,7 @@ private static function nonExistMethod($method = null, $args = null, $clone = nu
287301
$clone = new static();
288302
}
289303

290-
return $clone->$method(...$args);
304+
return $clone->$method(...($args ?? []));
291305
}
292306

293307
}

0 commit comments

Comments
 (0)