Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take DST into account when converting a calendar into its items #359

Merged
merged 1 commit into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class DatetimeMetricType internal constructor(
second = cal.get(Calendar.SECOND),
nano = AndroidTimeUnit.MILLISECONDS.toNanos(cal.get(Calendar.MILLISECOND).toLong()),
offset_seconds = AndroidTimeUnit.MILLISECONDS.toSeconds(
cal.get(Calendar.ZONE_OFFSET).toLong()
cal.get(Calendar.ZONE_OFFSET).toLong() + cal.get(Calendar.DST_OFFSET)
).toInt()
)
}
Expand Down Expand Up @@ -127,7 +127,7 @@ class DatetimeMetricType internal constructor(
second = value.get(Calendar.SECOND),
nano = AndroidTimeUnit.MILLISECONDS.toNanos(value.get(Calendar.MILLISECOND).toLong()),
offset_seconds = AndroidTimeUnit.MILLISECONDS.toSeconds(
value.get(Calendar.ZONE_OFFSET).toLong()
value.get(Calendar.ZONE_OFFSET).toLong() + value.get(Calendar.DST_OFFSET)
).toInt()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.util.Calendar
import java.util.Date
import java.util.TimeZone

const val MILLIS_PER_SEC = 1000L
private fun Date.asSeconds() = time / MILLIS_PER_SEC

@RunWith(AndroidJUnit4::class)
class DatetimeMetricTypeTest {

Expand Down Expand Up @@ -90,4 +94,27 @@ class DatetimeMetricTypeTest {
datetimeMetric.set()
assertFalse(datetimeMetric.testHasValue())
}

@Test
fun `Regression test - setting date and reading results in the same`() {
// This test is adopted from `SyncTelemetryTest.kt` in android-components.
// Previously we failed to properly deal with DST when converting from `Calendar` into its pieces.

val datetimeMetric = DatetimeMetricType(
disabled = false,
category = "telemetry",
lifetime = Lifetime.Ping,
name = "datetimeMetric",
sendInPings = listOf("store1"),
timeUnit = TimeUnit.Millisecond
)

val nowDate = Date()
val now = nowDate.asSeconds()
val timestamp = Date(now * MILLIS_PER_SEC)

datetimeMetric.set(timestamp)

assertEquals(now, datetimeMetric.testGetValue().asSeconds())
}
}