diff options
author | Neil Fuller <nfuller@google.com> | 2019-07-17 19:35:40 +0100 |
---|---|---|
committer | Neil Fuller <nfuller@google.com> | 2019-07-17 19:35:40 +0100 |
commit | cb7e206f0f41d053939cd0f17582ecbf08376e44 (patch) | |
tree | d6e48f930aba13507966b372acb2564b5200d1ef | |
parent | 8b607803d2a24010222ab0e99d868404b975df5c (diff) | |
download | ex-cb7e206f0f41d053939cd0f17582ecbf08376e44.tar.gz |
Convert use of Time.format() to an alternative
Convert use of Time.format() to an alternative.
This avoids future Y2038 issues associated with Time.
The code has been changed to use GregorianCalendar
which has lenient rounding behavior when values are
out of range (similar to android.text.format.Time).
This is unlike LocalDateTime, which throws exceptions.
There are no tests for this code.
Bug: 16550209
Test: build only
Change-Id: Ic7d027035b7f3dbb185e19b63c1402ffc2c60af5
-rw-r--r-- | common/java/com/android/common/LegacyHttpDateTime.java | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/common/java/com/android/common/LegacyHttpDateTime.java b/common/java/com/android/common/LegacyHttpDateTime.java index 9b902d67..83befa61 100644 --- a/common/java/com/android/common/LegacyHttpDateTime.java +++ b/common/java/com/android/common/LegacyHttpDateTime.java @@ -16,9 +16,9 @@ package com.android.common; -import android.text.format.Time; - import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -108,17 +108,16 @@ final class LegacyHttpDateTime { } } - // FIXME: Y2038 BUG! - if (year >= 2038) { - year = 2038; - month = Calendar.JANUARY; - date = 1; - } - - Time time = new Time(Time.TIMEZONE_UTC); - time.set(timeOfDay.second, timeOfDay.minute, timeOfDay.hour, date, - month, year); - return time.toMillis(false /* use isDst */); + TimeZone utc = TimeZone.getTimeZone("UTC"); + GregorianCalendar calendar = new GregorianCalendar(utc); + calendar.set(Calendar.YEAR, year); + calendar.set(Calendar.MONTH, month); + calendar.set(Calendar.DAY_OF_MONTH, date); + calendar.set(Calendar.HOUR_OF_DAY, timeOfDay.hour); + calendar.set(Calendar.MINUTE, timeOfDay.minute); + calendar.set(Calendar.SECOND, timeOfDay.second); + calendar.set(Calendar.MILLISECOND, 0); + return calendar.getTimeInMillis(); } private static int getDate(String dateString) { |