TimeUtil.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf.util;
32 
34 import com.google.protobuf.Timestamp;
35 
36 import java.math.BigInteger;
37 import java.text.ParseException;
38 
44 @Deprecated
45 public final class TimeUtil {
46  // Timestamp for "0001-01-01T00:00:00Z"
47  public static final long TIMESTAMP_SECONDS_MIN = -62135596800L;
48 
49  // Timestamp for "9999-12-31T23:59:59Z"
50  public static final long TIMESTAMP_SECONDS_MAX = 253402300799L;
51  public static final long DURATION_SECONDS_MIN = -315576000000L;
52  public static final long DURATION_SECONDS_MAX = 315576000000L;
53 
54  private static final long NANOS_PER_SECOND = 1000000000;
55 
56  private TimeUtil() {}
57 
72  @Deprecated
73  public static String toString(Timestamp timestamp) {
74  return Timestamps.toString(timestamp);
75  }
76 
89  @Deprecated
90  public static Timestamp parseTimestamp(String value) throws ParseException {
91  return Timestamps.parse(value);
92  }
93 
106  @Deprecated
107  public static String toString(Duration duration) {
108  return Durations.toString(duration);
109  }
110 
118  @Deprecated
119  public static Duration parseDuration(String value) throws ParseException {
120  return Durations.parse(value);
121  }
122 
128  @Deprecated
129  public static Timestamp createTimestampFromMillis(long milliseconds) {
130  return Timestamps.fromMillis(milliseconds);
131  }
132 
138  @Deprecated
139  public static Duration createDurationFromMillis(long milliseconds) {
140  return Durations.fromMillis(milliseconds);
141  }
142 
152  @Deprecated
153  public static long toMillis(Timestamp timestamp) {
154  return Timestamps.toMillis(timestamp);
155  }
156 
164  @Deprecated
165  public static long toMillis(Duration duration) {
166  return Durations.toMillis(duration);
167  }
168 
174  @Deprecated
175  public static Timestamp createTimestampFromMicros(long microseconds) {
176  return Timestamps.fromMicros(microseconds);
177  }
178 
184  @Deprecated
185  public static Duration createDurationFromMicros(long microseconds) {
186  return Durations.fromMicros(microseconds);
187  }
188 
198  @Deprecated
199  public static long toMicros(Timestamp timestamp) {
200  return Timestamps.toMicros(timestamp);
201  }
202 
210  @Deprecated
211  public static long toMicros(Duration duration) {
212  return Durations.toMicros(duration);
213  }
214 
220  @Deprecated
221  public static Timestamp createTimestampFromNanos(long nanoseconds) {
222  return Timestamps.fromNanos(nanoseconds);
223  }
224 
230  @Deprecated
231  public static Duration createDurationFromNanos(long nanoseconds) {
232  return Durations.fromNanos(nanoseconds);
233  }
234 
240  @Deprecated
241  public static long toNanos(Timestamp timestamp) {
242  return Timestamps.toNanos(timestamp);
243  }
244 
250  @Deprecated
251  public static long toNanos(Duration duration) {
252  return Durations.toNanos(duration);
253  }
254 
260  @Deprecated
261  public static Timestamp getCurrentTime() {
262  return Timestamps.fromMillis(System.currentTimeMillis());
263  }
264 
270  @Deprecated
271  public static Timestamp getEpoch() {
272  return Timestamp.getDefaultInstance();
273  }
274 
280  @Deprecated
282  return Timestamps.between(from, to);
283  }
284 
290  @Deprecated
292  return Timestamps.add(start, length);
293  }
294 
300  @Deprecated
302  return Timestamps.subtract(start, length);
303  }
304 
310  @Deprecated
311  public static Duration add(Duration d1, Duration d2) {
312  return Durations.add(d1, d2);
313  }
314 
320  @Deprecated
321  public static Duration subtract(Duration d1, Duration d2) {
322  return Durations.subtract(d1, d2);
323  }
324 
325  // Multiplications and divisions.
326 
327  // TODO(kak): Delete this.
328  @SuppressWarnings("DurationSecondsToDouble")
329  public static Duration multiply(Duration duration, double times) {
330  double result = duration.getSeconds() * times + duration.getNanos() * times / 1000000000.0;
331  if (result < Long.MIN_VALUE || result > Long.MAX_VALUE) {
332  throw new IllegalArgumentException("Result is out of valid range.");
333  }
334  long seconds = (long) result;
335  int nanos = (int) ((result - seconds) * 1000000000);
336  return normalizedDuration(seconds, nanos);
337  }
338 
339  // TODO(kak): Delete this.
340  public static Duration divide(Duration duration, double value) {
341  return multiply(duration, 1.0 / value);
342  }
343 
344  // TODO(kak): Delete this.
345  public static Duration multiply(Duration duration, long times) {
347  }
348 
349  // TODO(kak): Delete this.
350  public static Duration divide(Duration duration, long times) {
352  }
353 
354  // TODO(kak): Delete this.
355  public static long divide(Duration d1, Duration d2) {
356  return toBigInteger(d1).divide(toBigInteger(d2)).longValue();
357  }
358 
359  // TODO(kak): Delete this.
360  public static Duration remainder(Duration d1, Duration d2) {
362  }
363 
364  private static final BigInteger NANOS_PER_SECOND_BIG_INTEGER =
365  new BigInteger(String.valueOf(NANOS_PER_SECOND));
366 
367  private static BigInteger toBigInteger(Duration duration) {
368  return toBigInteger(duration.getSeconds())
370  .add(toBigInteger(duration.getNanos()));
371  }
372 
373  private static BigInteger toBigInteger(long value) {
374  return new BigInteger(String.valueOf(value));
375  }
376 
377  private static Duration createDurationFromBigInteger(BigInteger value) {
378  long seconds = value.divide(new BigInteger(String.valueOf(NANOS_PER_SECOND))).longValue();
379  int nanos = value.remainder(new BigInteger(String.valueOf(NANOS_PER_SECOND))).intValue();
380  return normalizedDuration(seconds, nanos);
381  }
382 
383  private static Duration normalizedDuration(long seconds, int nanos) {
384  if (nanos <= -NANOS_PER_SECOND || nanos >= NANOS_PER_SECOND) {
385  seconds += nanos / NANOS_PER_SECOND;
386  nanos %= NANOS_PER_SECOND;
387  }
388  if (seconds > 0 && nanos < 0) {
389  nanos += NANOS_PER_SECOND;
390  seconds -= 1;
391  }
392  if (seconds < 0 && nanos > 0) {
393  nanos -= NANOS_PER_SECOND;
394  seconds += 1;
395  }
396  if (seconds < DURATION_SECONDS_MIN || seconds > DURATION_SECONDS_MAX) {
397  throw new IllegalArgumentException("Duration is out of valid range.");
398  }
399  return Duration.newBuilder().setSeconds(seconds).setNanos(nanos).build();
400  }
401 }
com.google.protobuf.util.TimeUtil.createDurationFromNanos
static Duration createDurationFromNanos(long nanoseconds)
Definition: TimeUtil.java:231
absl::time_internal::cctz::seconds
std::chrono::duration< std::int_fast64_t > seconds
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:40
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
com.google.protobuf.util.Durations.subtract
static Duration subtract(Duration d1, Duration d2)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:431
com.google.protobuf.util.Timestamps.toMicros
static long toMicros(Timestamp timestamp)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:338
com.google.protobuf.util.Timestamps.fromNanos
static Timestamp fromNanos(long nanoseconds)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:347
com.google.protobuf.util.TimeUtil.NANOS_PER_SECOND_BIG_INTEGER
static final BigInteger NANOS_PER_SECOND_BIG_INTEGER
Definition: TimeUtil.java:364
com.google.protobuf.util.Timestamps.toNanos
static long toNanos(Timestamp timestamp)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:354
com.google.protobuf.util.TimeUtil.DURATION_SECONDS_MIN
static final long DURATION_SECONDS_MIN
Definition: TimeUtil.java:51
com.google.protobuf.util.Timestamps.fromMicros
static Timestamp fromMicros(long microseconds)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:325
com.google.protobuf.util.TimeUtil.toBigInteger
static BigInteger toBigInteger(Duration duration)
Definition: TimeUtil.java:367
com.google.protobuf.util.Durations.parse
static Duration parse(String value)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:241
com.google.protobuf.util.TimeUtil.normalizedDuration
static Duration normalizedDuration(long seconds, int nanos)
Definition: TimeUtil.java:383
com.google.protobuf.util.TimeUtil.toMillis
static long toMillis(Timestamp timestamp)
Definition: TimeUtil.java:153
com.google.protobuf.util.Timestamps.fromMillis
static Timestamp fromMillis(long milliseconds)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:303
com.google.protobuf.util.TimeUtil.add
static Duration add(Duration d1, Duration d2)
Definition: TimeUtil.java:311
com.google.protobuf.util.TimeUtil.createTimestampFromMillis
static Timestamp createTimestampFromMillis(long milliseconds)
Definition: TimeUtil.java:129
to
size_t to
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1385
com.google.protobuf
Definition: bloaty/third_party/protobuf/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java:2
com.google.protobuf.util.Timestamps.add
static Timestamp add(Timestamp start, Duration length)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:370
com.google.protobuf.util.TimeUtil.TIMESTAMP_SECONDS_MAX
static final long TIMESTAMP_SECONDS_MAX
Definition: TimeUtil.java:50
com.google.protobuf.util.Durations.toString
static String toString(Duration duration)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:214
com.google.protobuf.util.Timestamps.between
static Duration between(Timestamp from, Timestamp to)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:361
com.google.protobuf.util.TimeUtil.subtract
static Timestamp subtract(Timestamp start, Duration length)
Definition: TimeUtil.java:301
from
size_t from
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1384
com.google.protobuf.util.Durations
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:54
com.google.protobuf.util.Durations.fromNanos
static Duration fromNanos(long nanoseconds)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:327
start
static uint64_t start
Definition: benchmark-pound.c:74
com.google.protobuf.util.Durations.toMillis
static long toMillis(Duration duration)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:393
com.google.protobuf.util.TimeUtil.multiply
static Duration multiply(Duration duration, long times)
Definition: TimeUtil.java:345
d2
static const fe d2
Definition: curve25519_tables.h:39
xds_interop_client.int
int
Definition: xds_interop_client.py:113
com.google.protobuf.util.TimeUtil.parseDuration
static Duration parseDuration(String value)
Definition: TimeUtil.java:119
com.google.protobuf.util.TimeUtil.toString
static String toString(Timestamp timestamp)
Definition: TimeUtil.java:73
com.google.protobuf.util.TimeUtil.createDurationFromMicros
static Duration createDurationFromMicros(long microseconds)
Definition: TimeUtil.java:185
Timestamp
struct Timestamp Timestamp
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:672
com.google.protobuf.util.TimeUtil.divide
static long divide(Duration d1, Duration d2)
Definition: TimeUtil.java:355
com.google.protobuf.util.TimeUtil.toString
static String toString(Duration duration)
Definition: TimeUtil.java:107
com.google.protobuf.util.TimeUtil.toMillis
static long toMillis(Duration duration)
Definition: TimeUtil.java:165
com.google.protobuf.util.TimeUtil.multiply
static Duration multiply(Duration duration, double times)
Definition: TimeUtil.java:329
com.google.protobuf.util.Timestamps.parse
static Timestamp parse(String value)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:232
com.google.protobuf.util.TimeUtil.toBigInteger
static BigInteger toBigInteger(long value)
Definition: TimeUtil.java:373
com.google.protobuf.util.TimeUtil.remainder
static Duration remainder(Duration d1, Duration d2)
Definition: TimeUtil.java:360
com.google.protobuf.util.TimeUtil.subtract
static Duration subtract(Duration d1, Duration d2)
Definition: TimeUtil.java:321
com.google.protobuf.util.TimeUtil.NANOS_PER_SECOND
static final long NANOS_PER_SECOND
Definition: TimeUtil.java:54
com.google.protobuf.util.Timestamps.toString
static String toString(Timestamp timestamp)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:203
com.google.protobuf.util.Durations.add
static Duration add(Duration d1, Duration d2)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:423
com.google.protobuf.util.TimeUtil.getEpoch
static Timestamp getEpoch()
Definition: TimeUtil.java:271
com.google.protobuf.util.Durations.toMicros
static long toMicros(Duration duration)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:405
com.google.protobuf.util.TimeUtil.add
static Timestamp add(Timestamp start, Duration length)
Definition: TimeUtil.java:291
com.google.protobuf.util.TimeUtil.getCurrentTime
static Timestamp getCurrentTime()
Definition: TimeUtil.java:261
value
const char * value
Definition: hpack_parser_table.cc:165
java
com.google.protobuf.util.TimeUtil.divide
static Duration divide(Duration duration, double value)
Definition: TimeUtil.java:340
com.google.protobuf.util.Timestamps.toMillis
static long toMillis(Timestamp timestamp)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:316
com.google.protobuf::Duration
com.google.protobuf.util.TimeUtil.createTimestampFromNanos
static Timestamp createTimestampFromNanos(long nanoseconds)
Definition: TimeUtil.java:221
com.google.protobuf.util.TimeUtil.createDurationFromBigInteger
static Duration createDurationFromBigInteger(BigInteger value)
Definition: TimeUtil.java:377
L
lua_State * L
Definition: upb/upb/bindings/lua/main.c:35
com.google.protobuf.util.Timestamps
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:54
com.google.protobuf.util.TimeUtil.createDurationFromMillis
static Duration createDurationFromMillis(long milliseconds)
Definition: TimeUtil.java:139
com.google
com.google.protobuf.util.TimeUtil.toNanos
static long toNanos(Timestamp timestamp)
Definition: TimeUtil.java:241
com.google.protobuf.util.TimeUtil.parseTimestamp
static Timestamp parseTimestamp(String value)
Definition: TimeUtil.java:90
com
com.google.protobuf.util.TimeUtil.divide
static Duration divide(Duration duration, long times)
Definition: TimeUtil.java:350
com.google.protobuf.util.TimeUtil.TimeUtil
TimeUtil()
Definition: TimeUtil.java:56
com.google.protobuf.util.Durations.fromMillis
static Duration fromMillis(long milliseconds)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:311
com.google.protobuf.util.TimeUtil.DURATION_SECONDS_MAX
static final long DURATION_SECONDS_MAX
Definition: TimeUtil.java:52
com.google.protobuf.util.TimeUtil.TIMESTAMP_SECONDS_MIN
static final long TIMESTAMP_SECONDS_MIN
Definition: TimeUtil.java:47
com.google.protobuf.util.Timestamps.subtract
static Timestamp subtract(Timestamp start, Duration length)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Timestamps.java:379
com.google.protobuf.util.TimeUtil
Definition: TimeUtil.java:45
Duration
struct Duration Duration
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:640
com.google.protobuf.util.TimeUtil.createTimestampFromMicros
static Timestamp createTimestampFromMicros(long microseconds)
Definition: TimeUtil.java:175
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
com.google.protobuf.util.TimeUtil.toNanos
static long toNanos(Duration duration)
Definition: TimeUtil.java:251
com.google.protobuf.util.TimeUtil.distance
static Duration distance(Timestamp from, Timestamp to)
Definition: TimeUtil.java:281
com.google.protobuf.util.Durations.toNanos
static long toNanos(Duration duration)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:414
com.google.protobuf.util.TimeUtil.toMicros
static long toMicros(Duration duration)
Definition: TimeUtil.java:211
com.google.protobuf.util.TimeUtil.toMicros
static long toMicros(Timestamp timestamp)
Definition: TimeUtil.java:199
com.google.protobuf.util.Durations.fromMicros
static Duration fromMicros(long microseconds)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:319
google::protobuf.internal.decoder.long
long
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/decoder.py:89


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:39