-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathTimestamp.php
More file actions
164 lines (150 loc) · 4.02 KB
/
Timestamp.php
File metadata and controls
164 lines (150 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Core;
use DateTimeInterface;
/**
* Represents a Timestamp value.
*
* Nanosecond precision is preserved by passing nanoseconds as a separate
* argument to the constructor. If nanoseconds are given, any subsecond
* precision in the timestamp will be overridden when encoding the timestamp
* as a string.
*
* Example:
* ```
* use Google\Cloud\Core\Timestamp;
*
* $timestamp = new Timestamp(new \DateTime('2003-02-05 11:15:02.421827Z'));
* ```
*
* ```
* // Timestamps can be cast to strings.
* echo (string) $timestamp;
* ```
*/
class Timestamp implements \JsonSerializable
{
use TimeTrait;
const FORMAT = 'Y-m-d\TH:i:s.u\Z';
const FORMAT_NO_MS = 'Y-m-d\TH:i:s\Z';
const FORMAT_INTERPOLATE = 'Y-m-d\TH:i:s.%\s\Z';
/**
* @var \DateTimeInterface
*/
private $value;
/**
* @var int
*/
private $nanoSeconds;
/**
* @param \DateTimeInterface $value The timestamp value. Use of
* `DateTimeImmutable` is highly recommended over `DateTime` in order
* to avoid side effects.
* @param int $nanoSeconds [optional] The number of nanoseconds in the
* timestamp. If omitted, subsecond precision will be obtained from
* the instance of `\DateTimeInterface` provided in the first
* argument. If provided, any precision in `$value` below seconds
* will be disregarded.
*/
public function __construct(\DateTimeInterface $value, $nanoSeconds = null)
{
$this->value = $value;
$this->nanoSeconds = $nanoSeconds !== null
? (int) $nanoSeconds
: null;
}
/**
* Get the underlying `\DateTimeInterface` implementation.
*
* Please note that if you provided nanoseconds when creating the timestamp,
* they will not be included in this value.
*
* Example:
* ```
* $dateTime = $timestamp->get();
* ```
*
* @return DateTimeInterface
*/
public function get(): DateTimeInterface
{
return $this->value;
}
/**
* Return the number of nanoseconds.
*
* Example:
* ```
* $nanos = $timestamp->nanoSeconds();
* ```
*
* @return int
*/
public function nanoSeconds(): int
{
return $this->nanoSeconds === null
? (int) $this->value->format('u') * 1000
: $this->nanoSeconds;
}
/**
* Format the value as a string.
*
* Example:
* ```
* $value = $timestamp->formatAsString();
* ```
*
* @return string
*/
public function formatAsString(): string
{
return $this->formatTimeAsString(
$this->value,
$this->nanoSeconds
);
}
/**
* Format the value as a string.
*
* @return string
* @access private
*/
public function __toString()
{
return $this->formatAsString();
}
/**
* Format a timestamp for the API with nanosecond precision.
*
* @return array
*/
public function formatForApi(): array
{
return $this->formatTimeAsArray($this->value, $this->nanoSeconds());
}
/**
* Implement JsonSerializable by returning a ISO 8601 formatted string
*
* @return string
* @access private
*/
#[\ReturnTypeWillChange]
public function jsonSerialize(): string
{
return $this->formatAsString();
}
}