From 0d4412540e535b955b2879911808c84808c2874d Mon Sep 17 00:00:00 2001 From: aozarov Date: Wed, 11 Feb 2015 09:04:40 -0800 Subject: [PATCH] Fixes #6 --- src/main/java/com/google/gcloud/datastore/DateTime.java | 2 +- src/test/java/com/google/gcloud/datastore/DateTimeTest.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/google/gcloud/datastore/DateTimeTest.java diff --git a/src/main/java/com/google/gcloud/datastore/DateTime.java b/src/main/java/com/google/gcloud/datastore/DateTime.java index 3b7e85a2e736..97df17ba840b 100644 --- a/src/main/java/com/google/gcloud/datastore/DateTime.java +++ b/src/main/java/com/google/gcloud/datastore/DateTime.java @@ -78,7 +78,7 @@ public Calendar toCalendar() { } public static DateTime now() { - return new DateTime(System.nanoTime() / 1000L); + return copyFrom(new Date()); } public static DateTime copyFrom(Date date) { diff --git a/src/test/java/com/google/gcloud/datastore/DateTimeTest.java b/src/test/java/com/google/gcloud/datastore/DateTimeTest.java new file mode 100644 index 000000000000..43d84970925a --- /dev/null +++ b/src/test/java/com/google/gcloud/datastore/DateTimeTest.java @@ -0,0 +1 @@ +package com.google.gcloud.datastore; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import org.junit.Test; import java.util.Calendar; public class DateTimeTest { @Test public void testTimestampMicroseconds() throws Exception { Calendar cal = Calendar.getInstance(); DateTime date = DateTime.copyFrom(cal); assertEquals(cal.getTimeInMillis() * 1000, date.timestampMicroseconds()); } @Test public void testTimestampMillis() throws Exception { Calendar cal = Calendar.getInstance(); DateTime date = DateTime.copyFrom(cal); assertEquals(cal.getTimeInMillis(), date.timestampMillis()); } @Test public void testToDate() throws Exception { Calendar cal = Calendar.getInstance(); DateTime date = DateTime.copyFrom(cal); assertEquals(cal.getTime(), date.toDate()); } @Test public void testToCalendar() throws Exception { Calendar cal = Calendar.getInstance(); DateTime date = DateTime.copyFrom(cal); assertEquals(cal, date.toCalendar()); } @Test public void testNow() throws Exception { Calendar cal1 = Calendar.getInstance(); DateTime now = DateTime.now(); Calendar cal2 = Calendar.getInstance(); assertTrue(now.timestampMillis() >= cal1.getTimeInMillis()); assertTrue(now.timestampMillis() <= cal2.getTimeInMillis()); } @Test public void testCopyFrom() throws Exception { Calendar cal = Calendar.getInstance(); DateTime date1 = DateTime.copyFrom(cal); DateTime date2 = DateTime.copyFrom(cal.getTime()); cal.add(Calendar.DATE, 1); DateTime date3 = DateTime.copyFrom(cal.getTime()); assertEquals(date1, date2); assertNotEquals(date1, date3); } } \ No newline at end of file