42 #include <google/protobuf/port_def.inc>
53 static const int kMicrosPerSecond = 1000000;
54 static const int kMillisPerSecond = 1000;
55 static const int kNanosPerMillisecond = 1000000;
56 static const int kNanosPerMicrosecond = 1000;
57 static const int kSecondsPerMinute = 60;
58 static const int kSecondsPerHour = 3600;
91 if (seconds < 0 && nanos > 0) {
94 }
else if (seconds > 0 && nanos < 0) {
109 if (nanos % kNanosPerMillisecond == 0) {
110 return StringPrintf(
"%03d", nanos / kNanosPerMillisecond);
111 }
else if (nanos % kNanosPerMicrosecond == 0) {
112 return StringPrintf(
"%06d", nanos / kNanosPerMicrosecond);
126 void CurrentTime(
int64* seconds,
int32* nanos) {
139 if (result < 0 && remainder > 0) {
166 *timestamp = CreateNormalized<Timestamp>(seconds, nanos);
173 CurrentTime(&seconds, &nanos);
174 return CreateNormalized<Timestamp>(seconds, nanos);
182 int32 nanos = duration.nanos();
183 if (seconds < 0 || nanos < 0) {
188 result +=
StrCat(seconds);
190 result +=
"." + FormatNanos(nanos);
198 for (
int i = 0;
i <
y; ++
i) {
208 bool negative = (
value[0] ==
'-');
209 int sign_length = (negative ? 1 : 0);
213 size_t pos =
value.find_last_of(
".");
214 if (pos == std::string::npos) {
215 seconds_part =
value.substr(sign_length,
value.length() - 1 - sign_length);
218 seconds_part =
value.substr(sign_length, pos - sign_length);
219 nanos_part =
value.substr(pos + 1,
value.length() - pos - 2);
223 if (
end != seconds_part.c_str() + seconds_part.length()) {
227 if (
end != nanos_part.c_str() + nanos_part.length()) {
230 nanos = nanos *
Pow(10, 9 - nanos_part.length());
247 return CreateNormalized<Duration>(
248 micros / kMicrosPerSecond,
249 (micros % kMicrosPerSecond) * kNanosPerMicrosecond);
253 return CreateNormalized<Duration>(
254 millis / kMillisPerSecond,
255 (millis % kMillisPerSecond) * kNanosPerMillisecond);
259 return CreateNormalized<Duration>(seconds, 0);
263 return CreateNormalized<Duration>(minutes * kSecondsPerMinute, 0);
267 return CreateNormalized<Duration>(hours * kSecondsPerHour, 0);
275 return duration.
seconds() * kMicrosPerSecond +
276 RoundTowardZero(duration.
nanos(), kNanosPerMicrosecond);
280 return duration.
seconds() * kMillisPerSecond +
281 RoundTowardZero(duration.
nanos(), kNanosPerMillisecond);
289 return RoundTowardZero(duration.
seconds(), kSecondsPerMinute);
293 return RoundTowardZero(duration.
seconds(), kSecondsPerHour);
302 return CreateNormalized<Timestamp>(
303 micros / kMicrosPerSecond,
304 micros % kMicrosPerSecond * kNanosPerMicrosecond);
308 return CreateNormalized<Timestamp>(
309 millis / kMillisPerSecond,
310 millis % kMillisPerSecond * kNanosPerMillisecond);
314 return CreateNormalized<Timestamp>(seconds, 0);
322 return timestamp.
seconds() * kMicrosPerSecond +
323 RoundTowardZero(timestamp.
nanos(), kNanosPerMicrosecond);
327 return timestamp.
seconds() * kMillisPerSecond +
328 RoundTowardZero(timestamp.
nanos(), kNanosPerMillisecond);
336 return CreateNormalized<Timestamp>(
static_cast<int64>(
value), 0);
340 return static_cast<time_t
>(
value.seconds());
344 return CreateNormalized<Timestamp>(
value.tv_sec,
345 value.tv_usec * kNanosPerMicrosecond);
350 result.tv_sec =
value.seconds();
351 result.tv_usec = RoundTowardZero(
value.nanos(), kNanosPerMicrosecond);
356 return CreateNormalized<Duration>(
value.tv_sec,
357 value.tv_usec * kNanosPerMicrosecond);
362 result.tv_sec =
value.seconds();
363 result.tv_usec = RoundTowardZero(
value.nanos(), kNanosPerMicrosecond);
365 if (result.tv_usec < 0) {
367 result.tv_usec += kMicrosPerSecond;
379 using ::PROTOBUF_NAMESPACE_ID::util::CreateNormalized;
383 void ToUint128(
const Duration&
value, uint128* result,
bool* negative) {
384 if (
value.seconds() < 0 ||
value.nanos() < 0) {
395 void ToDuration(
const uint128&
value,
bool negative,
Duration* duration) {
423 ToUint128(
d, &
value, &negative);
427 negative = !negative;
430 ToDuration(
value, negative, &
d);
442 d = CreateNormalized<Duration>(seconds, nanos);
449 ToUint128(
d, &
value, &negative);
453 negative = !negative;
456 ToDuration(
value, negative, &
d);
465 bool negative1, negative2;
467 ToUint128(d1, &value1, &negative1);
468 ToUint128(d2, &value2, &negative2);
469 uint128 result = value1 % value2;
476 ToDuration(result, negative1, &d1);
481 bool negative1, negative2;
483 ToUint128(d1, &value1, &negative1);
484 ToUint128(d2, &value2, &negative2);
486 if (negative1 != negative2) {
493 t = CreateNormalized<Timestamp>(t.
seconds() +
d.seconds(),
499 t = CreateNormalized<Timestamp>(t.
seconds() -
d.seconds(),