From b70e0fdf09506fcf53af951d64a3ae47309fa245 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Thu, 31 Aug 2017 10:35:31 -0700 Subject: Add php support for Timestamp. (#3575) * Add php support for Timestamp. * Fix comments --- php/src/Google/Protobuf/Timestamp.php | 184 ++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 php/src/Google/Protobuf/Timestamp.php (limited to 'php/src/Google/Protobuf') diff --git a/php/src/Google/Protobuf/Timestamp.php b/php/src/Google/Protobuf/Timestamp.php new file mode 100644 index 00000000..d139da99 --- /dev/null +++ b/php/src/Google/Protobuf/Timestamp.php @@ -0,0 +1,184 @@ +google.protobuf.Timestamp + */ +class Timestamp extends \Google\Protobuf\Internal\Message +{ + /** + * Represents seconds of UTC time since Unix epoch + * 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to + * 9999-12-31T23:59:59Z inclusive. + * + * Generated from protobuf field int64 seconds = 1; + */ + private $seconds = 0; + /** + * Non-negative fractions of a second at nanosecond resolution. Negative + * second values with fractions must still have non-negative nanos values + * that count forward in time. Must be from 0 to 999,999,999 + * inclusive. + * + * Generated from protobuf field int32 nanos = 2; + */ + private $nanos = 0; + + public function __construct() { + \GPBMetadata\Google\Protobuf\Timestamp::initOnce(); + parent::__construct(); + } + + /** + * Represents seconds of UTC time since Unix epoch + * 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to + * 9999-12-31T23:59:59Z inclusive. + * + * Generated from protobuf field int64 seconds = 1; + * @return int|string + */ + public function getSeconds() + { + return $this->seconds; + } + + /** + * Represents seconds of UTC time since Unix epoch + * 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to + * 9999-12-31T23:59:59Z inclusive. + * + * Generated from protobuf field int64 seconds = 1; + * @param int|string $var + * @return $this + */ + public function setSeconds($var) + { + GPBUtil::checkInt64($var); + $this->seconds = $var; + + return $this; + } + + /** + * Non-negative fractions of a second at nanosecond resolution. Negative + * second values with fractions must still have non-negative nanos values + * that count forward in time. Must be from 0 to 999,999,999 + * inclusive. + * + * Generated from protobuf field int32 nanos = 2; + * @return int + */ + public function getNanos() + { + return $this->nanos; + } + + /** + * Non-negative fractions of a second at nanosecond resolution. Negative + * second values with fractions must still have non-negative nanos values + * that count forward in time. Must be from 0 to 999,999,999 + * inclusive. + * + * Generated from protobuf field int32 nanos = 2; + * @param int $var + * @return $this + */ + public function setNanos($var) + { + GPBUtil::checkInt32($var); + $this->nanos = $var; + + return $this; + } + + /** + * Converts PHP DateTime to Timestamp. + * + * @param DateTime $datetime + */ + public function fromDateTime($datetime) + { + if (get_class($datetime) !== \DateTime::class) { + trigger_error("Given parameter is not a DateTime.", + E_USER_ERROR); + } + $this->seconds = $datetime->format('U'); + $this->nanos = 0; + } + + /** + * Converts Timestamp to PHP DateTime. Nano second is ignored. + * + * @return DateTime $datetime + */ + public function toDateTime() + { + return \DateTime::createFromFormat('U', $this->seconds); + } +} + -- cgit v1.2.3