Skip to content

Commit 227103f

Browse files
authored
Allow specifying the start date with a DateTime object when listing activities (#69)
1 parent 4605c84 commit 227103f

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/Model/Activities/HasActivitiesInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Platformsh\Client\Model\Activities;
44

5+
use DateTime;
56
use Platformsh\Client\Model\Activity;
67

78
/**
@@ -28,8 +29,8 @@ public function getActivity($id);
2829
* Limit the number of activities to return. Zero for no limit.
2930
* @param string|null $type
3031
* Filter activities by type.
31-
* @param int|null $startsAt
32-
* A UNIX timestamp for the maximum created date of activities to return.
32+
* @param int|DateTime|null $startsAt
33+
* A UNIX timestamp or DateTime for the maximum created date of activities to return.
3334
* @param string|string[]|null $state
3435
* Filter activities by state ("pending", "in_progress", "complete" or "cancelled").
3536
* @param string|string[]|null $result

src/Model/Activity.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Platformsh\Client\Model;
44

5+
use DateTime;
6+
use DateTimeZone;
57
use GuzzleHttp\Exception\ConnectException;
68
use GuzzleHttp\Message\ResponseInterface;
79
use GuzzleHttp\Stream\StreamInterface;
@@ -216,21 +218,27 @@ public function getDescription($html = false)
216218
}
217219

218220
/**
219-
* @param int $timestamp
221+
* @param int|DateTime $timestamp UNIX UTC timestamp (seconds) or DateTime
220222
*
221-
* @return false|string
223+
* @return false|string 2022-02-22T02:00:00.000000+00:00
222224
*/
223225
public static function formatStartsAt($timestamp)
224226
{
225-
$tz = date_default_timezone_get();
226-
date_default_timezone_set('UTC');
227-
$date = date('c', $timestamp);
228-
date_default_timezone_set($tz);
227+
if ($timestamp instanceof DateTime) {
228+
// Override the timezone to produce a UTC ISO date
229+
$date = clone $timestamp;
230+
$date->setTimezone(new DateTimeZone("UTC"));
231+
} else {
232+
// Parse the UNIX UTC timestamp (seconds) into a DateTime
233+
$date = DateTime::createFromFormat('U', $timestamp, new DateTimeZone("UTC"));
234+
}
235+
229236
if (!$date) {
230237
throw new \RuntimeException(sprintf('Failed to format timestamp: %d', $timestamp));
231238
}
232-
233-
return $date;
239+
240+
# Sample: 2022-02-22T02:00:00.000000+00:00
241+
return $date = $date->format('Y-m-d\TH:i:s.uP');
234242
}
235243

236244
/**

0 commit comments

Comments
 (0)