Skip to content

Commit 90a4252

Browse files
Patch 1.0.2
Fixed small aspects of work with timezone of DateTime object. Now "tz" is not read-only anymore, and it can accept string representation of Time Zone. `setTimezone()`` as well is chainable. ```php $t = ts('previous monday 23:00', true) ->setTimezone('America/Los_Angeles'); ```
1 parent 1f2b1b4 commit 90a4252

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/PHP.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function frameworkDir() {
106106
*/
107107
public static function simpUtilsVersion(): Version|string {
108108
$class = static::redef(Version::class);
109-
return new $class('1.0.1', 'SimpUtils');
109+
return new $class('1.0.2', 'SimpUtils');
110110
}
111111

112112
/**

src/models/DateTime.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<?php
2-
3-
42
namespace spaf\simputils\models;
53

64
use DateTimeInterface;
@@ -14,6 +12,7 @@
1412
use spaf\simputils\traits\ForOutputsTrait;
1513
use function date_interval_create_from_date_string;
1614
use function is_null;
15+
use function is_string;
1716
use function json_encode;
1817
use function trim;
1918

@@ -32,7 +31,7 @@
3231
* @property-read \spaf\simputils\models\Date|string $date Date part (stringifiable object)
3332
*
3433
* @property-read \spaf\simputils\models\Time|string $time Time part
35-
* @property-read \spaf\simputils\models\DateTimeZone $tz
34+
* @property \spaf\simputils\models\DateTimeZone|string $tz Timezone change
3635
*
3736
* @property int $week ISO 8601 week number of year, weeks starting on Monday
3837
* @property-read int $doy The day of the year (starting from 0)
@@ -148,13 +147,22 @@ public function getTimezone(): DateTimeZone|false {
148147
return new DateTimeZone(parent::getTimezone()->getName());
149148
}
150149

150+
/**
151+
* @param \spaf\simputils\models\DateTimeZone|string $timezone Time zone
152+
*
153+
* @return static
154+
*/
151155
#[Property('tz')]
152156
#[\ReturnTypeWillChange]
153-
public function setTimezone($timezone) {
157+
public function setTimezone($timezone): static {
154158
// IMP This method is original native PHP method, and it expects to return something,
155159
// And when it's used as a method it works exactly the same, but in case of
156160
// property - it will be used ONLY for setting, without returning anything.
157161
// This is why return-type signature has no definition!
162+
$class_tz = PHP::redef(DateTimeZone::class);
163+
if (is_string($timezone)) {
164+
$timezone = new $class_tz($timezone);
165+
}
158166
return parent::setTimezone($timezone);
159167
}
160168

@@ -350,6 +358,18 @@ public function period(
350358
return $left->walk($right, $step);
351359
}
352360

361+
/**
362+
* Shortcut for "format"
363+
*
364+
* @param string $format Output format
365+
*
366+
* @return string
367+
*/
368+
#[Shortcut('static::format()')]
369+
function f($format) {
370+
return $this->format($format);
371+
}
372+
353373
public function toJson(?bool $pretty = null, bool $with_class = false): string {
354374
// TODO Implement optional choice of "for_*"
355375
return json_encode($this->for_system);

0 commit comments

Comments
 (0)