/** * Snapshot constructor. * @param AggregateIdInterface $aggregateId * @param AggregateRootInterface $aggregate * @param string $version * @param \DateTimeImmutable $createdAt */ public function __construct(AggregateIdInterface $aggregateId, AggregateRootInterface $aggregate, $version, \DateTimeImmutable $createdAt) { $this->aggregateId = $aggregateId; $this->aggregate = $aggregate; $this->version = $version; $this->createdAt = $createdAt->setTimezone(new \DateTimeZone('UTC')); }
/** * @param AggregateIdInterface|string $id * @param $version * @param mixed $payload * @param \DateTimeImmutable $recordedOn */ public function __construct($id, $version, $payload, \DateTimeImmutable $recordedOn) { $this->id = (string) $id; $this->payload = $payload; $this->recordedOn = $recordedOn->setTimezone(new \DateTimeZone('UTC')); $this->version = $version; }
/** * Set the date-time of the Date in this Header. * * If a DateTime instance is provided, it is converted to DateTimeImmutable. * * @param DateTimeInterface $dateTime */ public function setDateTime(DateTimeInterface $dateTime) { $this->clearCachedValueIf($this->getCachedValue() != $dateTime->format(DateTime::RFC2822)); if ($dateTime instanceof DateTime) { $immutable = new DateTimeImmutable('@' . $dateTime->getTimestamp()); $dateTime = $immutable->setTimezone($dateTime->getTimezone()); } $this->dateTime = $dateTime; }
public static function craftDateTime($seconds, $microseconds = 0) { // Override \Doctrine\ODM\MongoDB\Types\DateType::craftDateTime to return a DateTimeImmutable object // in the 'UTC' timezone instead the current PHP one (date_default_timezone_get()) $datetime = new \DateTimeImmutable(); $datetime->setTimezone(new \DateTimeZone('UTC')); $datetime->setTimestamp($seconds); if ($microseconds > 0) { $datetime = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', $datetime->format('Y-m-d H:i:s') . '.' . $microseconds, new \DateTimeZone('UTC')); } return $datetime; }
/** * Returns a DateTime of the numbered day of range of DateRange. * If $day is a negative number, it will count backwards from the end of the DateRange. * Example (2015-01-01 to 2015-01-31): * * 1 = first day of range: 2015-01-01 * * 10 = tenth day of range: 2015-01-10 * * -1 = last day of range: 2015-01-31 * * -5 = fifth to last day: 2015-01-27 * * @param int $day * @return \DateTime * @throws \Exception if $day falls outside of range */ public function getDayOfRange($day) { $return = $this->start->setTimezone($this->returnTimezone); if ($day > 1) { // subtract 1 from the day because 1 should be the first day of the range, not 1 day after start of range $daysToAdd = new \DateInterval('P' . ($day - 1) . 'D'); $return = $return->add($daysToAdd); } if ($day < 0) { $return = $this->end->setTimezone($this->returnTimezone); // subtract 1 from the day because 1 should be the last day of the range, not 1 day before end of range $return = $return->sub(new \DateInterval('P' . (abs($day) - 1) . 'D')); } if ($return < $this->start->setTimezone($this->returnTimezone) || $return > $this->end->setTimezone($this->returnTimezone)) { throw new \Exception('Requested day falls outside of DateRange'); } return new \DateTime($return->format('Y-m-d')); }
public function isValid(&$value) { if ($value === NULL && $this->isNullable) { return TRUE; } if ($this->enum) { return in_array($value, $this->enum, TRUE); } foreach ($this->types as $type => $foo) { if ($type === 'datetime') { if ($value instanceof \DateTime) { return TRUE; } elseif ($value instanceof \DateTimeImmutable) { $value = new \DateTime($value->format('c')); return TRUE; } elseif (is_string($value) && $value !== '') { $value = new \DateTime($value); $value->setTimezone(new DateTimeZone(date_default_timezone_get())); return TRUE; } elseif (ctype_digit($value)) { $value = new \DateTime("@{$value}"); return TRUE; } } elseif ($type === 'datetimeimmutable') { if ($value instanceof \DateTimeImmutable) { return TRUE; } elseif ($value instanceof \DateTime) { $value = new \DateTimeImmutable($value->format('c')); return TRUE; } elseif (is_string($value) && $value !== '') { $tmp = new \DateTimeImmutable($value); $value = $tmp->setTimezone(new DateTimeZone(date_default_timezone_get())); return TRUE; } elseif (ctype_digit($value)) { $value = new \DateTimeImmutable("@{$value}"); return TRUE; } } elseif ($type === 'string') { if (is_string($value)) { return TRUE; } if (is_int($value) || is_object($value) && method_exists($value, '__toString')) { $value = (string) $value; return TRUE; } } elseif ($type === 'float') { if (is_float($value)) { return TRUE; } if (is_numeric($value)) { settype($value, 'float'); return TRUE; } elseif (is_string($value)) { $value = (double) str_replace(array(' ', ','), array('', '.'), $value); return TRUE; } } elseif ($type === 'int') { if (is_int($value)) { return TRUE; } if (is_numeric($value)) { settype($value, 'int'); return TRUE; } elseif (is_string($value)) { $value = (int) str_replace(array(' ', ','), array('', '.'), $value); return TRUE; } } elseif ($type === 'bool') { if (is_bool($value)) { return TRUE; } if (in_array($value, array(0, 0.0, '0', 1, 1.0, '1'), TRUE)) { $value = (bool) $value; return TRUE; } } elseif ($type === 'array') { if (is_array($value)) { return TRUE; } } elseif ($type === 'object') { if (is_object($value)) { return TRUE; } } elseif ($type === 'scalar') { if (is_scalar($value)) { return TRUE; } } elseif ($type === 'mixed') { return TRUE; } else { if ($value instanceof $type) { return TRUE; } } } return FALSE; }
public function testSetTimezone() { $time = '2000-01-02T03:14:25'; $immutable = new DateTimeImmutable($time); $control = new DateTimeImmutable($time); $mutable = new DateTime($time); $timezone = new DateTimeZone('Pacific/Nauru'); $new = $immutable->setTimezone($timezone); $mutable->setTimezone($timezone); $this->assertNotSame($immutable, $new); $this->assertSame($control->format(DateTime::RFC3339), $immutable->format(DateTime::RFC3339)); $this->assertSame($mutable->format(DateTime::RFC3339), $new->format(DateTime::RFC3339)); }
/** * Format the date based on the ISO 8601 date formats * @see https://docs.threads.io/docs/threads-timestamp-format * * @param \DateTimeImmutable $datetime * * @return string */ private function formatDate(\DateTimeImmutable $datetime) { return str_replace('+00:00', '.000Z', $datetime->setTimezone(new \DateTimeZone('UTC'))->format('c')); }
/** * DateTimeHelper::getDatetime() from SonataIntlBundle * * @param \Datetime|\DateTimeImmutable|string|integer $data * @param \DateTimeZone timezone * @return \Datetime */ protected function getDatetime($data, \DateTimeZone $timezone) { if ($data instanceof \DateTime || $data instanceof \DateTimeImmutable) { return $data->setTimezone($timezone); } // the format method accept array or integer if (is_numeric($data)) { $data = (int) $data; } if (is_string($data)) { $data = strtotime($data); } // MongoDB Date and Timestamp if ($data instanceof \MongoDate || $data instanceof \MongoTimestamp) { $data = $data->sec; } // Mongodb bug ? timestamp value is on the key 'i' instead of the key 't' if (is_array($data) && array_keys($data) == array('t', 'i')) { $data = $data['i']; } $date = new \DateTime(); $date->setTimestamp($data); $date->setTimezone($timezone); return $date; }
function get_extend_until_info($user, $group, $periods = 1) { //use these functions to get current expiry dates for existing members - they are calculated in some cases //so just grabbing the "expire" field from the users_usergroups table doesn't always work $userInfo = $this->get_user_info($user); $usergroupdates = $this->get_user_groups_date($userInfo['userId']); $info = $this->get_group_info($group); //set the start date as now for new memberships and as expiry of current membership for existing members if (array_key_exists($group, $usergroupdates)) { if (!empty($usergroupdates[$group]['expire'])) { $date = $usergroupdates[$group]['expire']; } elseif ($info['expireAfter'] > 0) { $date = $usergroupdates[$group]['created']; } } if (!isset($date) || !$date) { $date = $this->now; //this is a new membership $new = true; } //convert start date to object $rawstartutc = new DateTimeImmutable('@' . $date); global $prefs; $tz = TikiDate::TimezoneIsValidId($prefs['server_timezone']) ? $prefs['server_timezone'] : 'UTC'; $timezone = new DateTimeZone($tz); $startlocal = $rawstartutc->setTimezone($timezone); //anniversary memberships if (!empty($info['anniversary'])) { //set time to 1 second after midnight so that all times are set to same times for interval calculations $startlocal = $startlocal->setTime(0, 0, 1); // annual anniversaries if (strlen($info['anniversary']) == 4) { $ann_month = substr($info['anniversary'], 0, 2); $ann_day = substr($info['anniversary'], 2, 2); $startyear = $startlocal->format('Y'); //increment the year if past the annual anniversary if ($startlocal->format('m') > $ann_month || $startlocal->format('m') == $ann_month && $startlocal->format('d') >= $ann_day) { $startyear++; } //first extension is always to next anniversary $next_ann = $startlocal->setDate($startyear, $ann_month, $ann_day); //extend past next anniversary if more than one period $extendto = $next_ann->modify('+' . $periods - 1 . ' years'); //previous anniversary for proration $prev_ann = $next_ann->modify('-1 years'); // monthly anniversaries //using modify('+1 month') can result in "skipping" months so fix the day of the previous/next month } elseif (strlen($info['anniversary']) == 2) { $ann_day = $info['anniversary']; $lastday = date('d', strtotime('last day of ' . $startlocal->format('Y') . '-' . $startlocal->format('m'))); $mod_ann_day = $ann_day > $lastday ? $lastday : $ann_day; if ($startlocal->format('d') < $mod_ann_day) { $mod = $mod_ann_day - $startlocal->format('d'); $next_ann = $startlocal->modify('+' . $mod . ' days'); $prev_mo_lastday = $startlocal->modify('last day of last month'); if ($ann_day >= $prev_mo_lastday->format('d')) { $prev_ann = $prev_mo_lastday; } else { $prev_ann = $startlocal->setDate($prev_mo_lastday->format('Y'), $prev_mo_lastday->format('m'), $mod_ann_day); } } else { //check if last day of month $next_mo_lastday = $startlocal->modify('last day of next month'); if ($mod_ann_day >= $next_mo_lastday->format('d')) { $next_ann = $next_mo_lastday; } else { $next_ann = $startlocal->setDate($next_mo_lastday->format('Y'), $next_mo_lastday->format('m'), $mod_ann_day); } $mod = $startlocal->format('d') - $mod_ann_day; $prev_ann = $startlocal->modify('-' . $mod . ' days'); } if ($periods - 1 > 0) { $yrsplus = floor(($periods - 1) / 12); $yr = $next_ann->format('Y') + $yrsplus; $moplus = $periods - 1 - $yrsplus * 12; if ($moplus + $next_ann->format('m') < 12) { $mo = $moplus + $next_ann->format('m'); } else { $yr++; $mo = $moplus + $next_ann->format('m') - 12; } if ($ann_day >= date('d', strtotime('last day of ' . $yr . '-' . $mo))) { $d = date('d', strtotime('last day of ' . $yr . '-' . $mo)); } else { $d = $ann_day; } $extendto = $next_ann->setDate($yr, $mo, $d); } else { $extendto = $next_ann; } } //calculate interval of membership term $interval = $startlocal->diff($extendto); //set prorate interval $prorateInterval = in_array($info['prorateInterval'], ['year', 'month', 'day']) ? $info['prorateInterval'] : 'day'; //prorate if ($prorateInterval == 'year' && strlen($info['anniversary']) == 4) { $ratio = $interval->y; $ratio += $interval->m > 0 || $interval->d > 0 ? 1 : 0; } elseif ($prorateInterval == 'month' || $prorateInterval == 'year' && strlen($info['anniversary']) == 2) { $round = $interval->d > 0 ? 1 : 0; $ratio = $interval->y * 12 + $interval->m + $round; if (strlen($info['anniversary']) == 4) { $ratio = $ratio / 12; } } elseif ($prorateInterval == 'day') { $ann_interval = $prev_ann->diff($next_ann); $stub_interval = $startlocal->diff($next_ann); $ratio = $stub_interval->days / $ann_interval->days + ($periods - 1); } $remainder = $ratio > 1 ? $ratio - floor($ratio) : $ratio; //memberships based on number of days } else { $remainder = 1; $ratio = 1; $extendto = $startlocal->modify('+' . $info['expireAfter'] * $periods . ' days'); $interval = $startlocal->diff($extendto); } $timestamp = $extendto != null ? $extendto->format('U') : null; return array('timestamp' => $timestamp, 'ratio_prorated_first_period' => $remainder, 'ratio' => $ratio, 'interval' => $interval, 'new' => $new); }
/** * Adds a log record. * * @param int $level The logging level * @param string $message The log message * @param array $context The log context * @return Boolean Whether the record has been processed */ public function addRecord(int $level, string $message, array $context = []) : bool { $levelName = static::getLevelName($level); // check if any handler will handle this message so we can return early and save cycles $handlerKey = null; reset($this->handlers); while ($handler = current($this->handlers)) { if ($handler->isHandling(['level' => $level])) { $handlerKey = key($this->handlers); break; } next($this->handlers); } if (null === $handlerKey) { return false; } $dateTime = new DateTimeImmutable($this->microsecondTimestamps, $this->timezone); $dateTime->setTimezone($this->timezone); $record = ['message' => $message, 'context' => $context, 'level' => $level, 'level_name' => $levelName, 'channel' => $this->name, 'datetime' => $dateTime, 'extra' => []]; foreach ($this->processors as $processor) { $record = call_user_func($processor, $record); } while ($handler = current($this->handlers)) { if (true === $handler->handle($record)) { break; } next($this->handlers); } return true; }
public function convertFromDatabase($value) { $datetime = new \DateTimeImmutable($value, new \DateTimeZone('UTC')); return $datetime->setTimezone(new \DateTimeZone(date_default_timezone_get())); }