國外時間快了一個小時

BUG 描述:
        將時區設置爲智利標準時間,重啓機器並GPS定位後當前日期顯示錯誤
   
    BUG 產生原因:
        默認時間戳設置錯誤所致
   
    動作/解決方案:
        修改默認時間戳

 

最近項目遇到一個很蛋疼的問題,選擇智利(聖地亞哥)之後,時間不對,比正常時間快了一個小時,使用GPS定位後時間不準。

可能是因爲沒有設置默認時區,採用的還是中國的時區去進行時間的顯示。

 

+import java.util.TimeZone;
+import java.text.SimpleDateFormat;
+import java.util.Date;

   // give any timezone code room without going into negative time.
    private static final long EARLIEST_SUPPORTED_TIME = 86400 * 1000;
    private static final long EARLIEST_SUPPORTED_TIME2 = 1514764800 * 1000L;//2018.1.1 08:00

    // fix some time zone date errors, 
    private static final String EARLIEST_SUPPORTED_TIME_UTC = "2018-01-01 08:00:00";
    private static final String EARLIEST_SUPPORTED_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
 

   * them from the build system somehow.
@@ -719,11 +731,24 @@ public final class SystemServer {
             if (alarm == null) {
                 Slog.d(TAG,"AlarmManagerService is null, not set time.");
             } else {
-                Slog.w(TAG, "Current time=" + System.currentTimeMillis() + ", EARLIEST_SUPPORTED_TIME="+EARLIEST_SUPPORTED_TIME2);
-                if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME2) {
-                    SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME2);
+                long earliest_time = EARLIEST_SUPPORTED_TIME2;
+                try {
+                    SimpleDateFormat sdf = new SimpleDateFormat(EARLIEST_SUPPORTED_TIME_FORMAT);
+                    sdf.setTimeZone(TimeZone.getDefault());
+                    String timeStr = SystemProperties.get("ro.earliest-time", "");
+                    timeStr = (!"".equals(timeStr)) ? timeStr.trim() : EARLIEST_SUPPORTED_TIME_UTC;
+                    Date date = sdf.parse(timeStr.trim());
+                    earliest_time = date.getTime();
+                } catch (Exception e) {
+                    Slog.d(TAG,"Error: " +e.getMessage());
+                }
+
+                Slog.w(TAG, "Current time=" + System.currentTimeMillis() + ", earliest_time="+earliest_time);
+                if (System.currentTimeMillis() < earliest_time) {
+                    SystemClock.setCurrentTimeMillis(earliest_time);
                     try {
-                        boolean ret = alarm.setTime(EARLIEST_SUPPORTED_TIME2);
+                        boolean ret = alarm.setTime(earliest_time);
                         Slog.d(TAG, "System clock is before 2018, set to 2018-01-01 8:00, ret = " + ret);
                     } catch (RemoteException e) {
                         Slog.e(TAG, "Unable to set RTC", e);
@@ -731,7 +756,9 @@ public final class SystemServer {

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章