Skip to content

Commit 8c10b4e

Browse files
---
yaml --- r: 9673 b: refs/heads/mrschmidt-core-timestamps c: 4cdaf68 h: refs/heads/master i: 9671: 4fa14aa
1 parent 9f88f02 commit 8c10b4e

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,4 @@ refs/tags/v0.46.0: 40dfc83a11b2cf2c21bf0f5a7b1e47087cbf0259
9797
refs/heads/alixhami-patch-1: 277eebc2d3abd73fa376a1d6f39fcb93a9f0335d
9898
refs/heads/michaelawyu-patch-1: 244423736b94700c9bc54fc63ec4f5dda65fb625
9999
refs/tags/0.47.0: 2e8688416bcae6150c0fa59936103554dbbe3821
100-
refs/heads/mrschmidt-core-timestamps: 86be7e5eb5cd45a2f8c89754d19f269527b004f0
100+
refs/heads/mrschmidt-core-timestamps: 4cdaf68bc8da8297b9089e2237df093e745c553c

branches/mrschmidt-core-timestamps/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ public static Timestamp of(Date date) {
9595
return ofTimeMicroseconds(TimeUnit.MILLISECONDS.toMicros(date.getTime()));
9696
}
9797

98+
/**
99+
* Returns a new {@code java.util.Date} corresponding to this {@code timestamp}. Any
100+
* sub-millisecond precision will be stripped.
101+
*
102+
* @return An approximate {@code java.util.Date} representation of this {@code timestamp}.
103+
*/
104+
public Date toDate() {
105+
long secondsInMilliseconds = TimeUnit.SECONDS.toMillis(this.seconds);
106+
long nanosInMilliseconds = TimeUnit.NANOSECONDS.toMillis(this.nanos);
107+
return new Date(secondsInMilliseconds + nanosInMilliseconds);
108+
}
109+
98110
/** Creates an instance with current time. */
99111
public static Timestamp now() {
100112
java.sql.Timestamp date = new java.sql.Timestamp(System.currentTimeMillis());

branches/mrschmidt-core-timestamps/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/TimestampTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
import com.google.common.testing.EqualsTester;
2323
import java.util.Calendar;
24+
import java.util.Date;
2425
import java.util.GregorianCalendar;
2526
import java.util.TimeZone;
27+
import java.util.concurrent.TimeUnit;
2628
import org.junit.Rule;
2729
import org.junit.Test;
2830
import org.junit.rules.ExpectedException;
@@ -35,6 +37,9 @@ public class TimestampTest {
3537
private static final String TEST_TIME_ISO = "2015-10-12T15:14:54Z";
3638
private static final long TEST_TIME_SECONDS = 1444662894L;
3739
private static final long TEST_TIME_MICROSECONDS = 10000100L;
40+
private static final long TEST_TIME_MILLISECONDS =
41+
TimeUnit.SECONDS.toMillis(1444662894L) + TimeUnit.MICROSECONDS.toMillis(1234);
42+
private static final Date TEST_DATE = new Date(TEST_TIME_MILLISECONDS);
3843

3944
@Rule public ExpectedException expectedException = ExpectedException.none();
4045

@@ -64,6 +69,24 @@ public void ofMicroseconds() {
6469
assertThat(timestamp.getNanos()).isEqualTo(TEST_TIME_MICROSECONDS % 1000000L * 1000);
6570
}
6671

72+
@Test
73+
public void ofDate() {
74+
Timestamp timestamp = Timestamp.of(TEST_DATE);
75+
Long expectedSeconds = TimeUnit.MILLISECONDS.toSeconds(TEST_TIME_MILLISECONDS);
76+
Long expectedNanos =
77+
TimeUnit.MILLISECONDS.toNanos(TEST_TIME_MILLISECONDS)
78+
- TimeUnit.SECONDS.toNanos(expectedSeconds);
79+
assertThat(timestamp.getSeconds()).isEqualTo(expectedSeconds);
80+
assertThat(timestamp.getNanos()).isEqualTo(expectedNanos);
81+
}
82+
83+
@Test
84+
public void toDate() {
85+
Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 1234 * 1000);
86+
Date date = timestamp.toDate();
87+
assertThat(TEST_TIME_MILLISECONDS).isEqualTo(date.getTime());
88+
}
89+
6790
@Test
6891
public void toFromSqlTimestamp() {
6992
long seconds = TEST_TIME_SECONDS;

0 commit comments

Comments
 (0)