]> granicus.if.org Git - icu/commitdiff
ICU-20475 Japanese Calendar current era calculation should use local time instead...
authorJeff Genovy <29107334+jefgen@users.noreply.github.com>
Sat, 16 Mar 2019 21:25:33 +0000 (14:25 -0700)
committerJeff Genovy <29107334+jefgen@users.noreply.github.com>
Wed, 3 Apr 2019 21:42:57 +0000 (14:42 -0700)
icu4c/source/i18n/erarules.cpp
icu4c/source/i18n/erarules.h

index 7249601d64b3ac8e8d8e21312c8a79c5d6d5c35a..e375740bd6b10a02437056c0e11888ecef823647 100644 (file)
@@ -11,6 +11,7 @@
 #include "unicode/ucal.h"
 #include "unicode/ures.h"
 #include "unicode/ustring.h"
+#include "unicode/timezone.h"
 #include "cmemory.h"
 #include "cstring.h"
 #include "erarules.h"
@@ -290,9 +291,22 @@ int32_t EraRules::getEraIndex(int32_t year, int32_t month, int32_t day, UErrorCo
 }
 
 void EraRules::initCurrentEra() {
-    UDate now = ucal_getNow();
+    // Compute local wall time in millis using ICU's default time zone.
+    UErrorCode ec = U_ZERO_ERROR;
+    UDate localMillis = ucal_getNow();
+
+    int32_t rawOffset, dstOffset;
+    TimeZone* zone = TimeZone::createDefault();
+    // If we failed to create the default time zone, we are in a bad state and don't
+    // really have many options. Carry on using UTC millis as a fallback.
+    if (zone != nullptr) {
+        zone->getOffset(localMillis, FALSE, rawOffset, dstOffset, ec);
+        delete zone;
+        localMillis += (rawOffset + dstOffset);
+    }
+
     int year, month0, dom, dow, doy, mid;
-    Grego::timeToFields(now, year, month0, dom, dow, doy, mid);
+    Grego::timeToFields(localMillis, year, month0, dom, dow, doy, mid);
     int currentEncodedDate = encodeDate(year, month0 + 1 /* changes to 1-base */, dom);
     int eraIdx = numEras - 1;
     while (eraIdx > 0) {
@@ -303,7 +317,8 @@ void EraRules::initCurrentEra() {
     }
     // Note: current era could be before the first era.
     // In this case, this implementation returns the first era index (0).
-    currentEra = eraIdx;}
+    currentEra = eraIdx;
+}
 
 U_NAMESPACE_END
 #endif /* #if !UCONFIG_NO_FORMATTING */
index 620f27cb862d333b843d74c696ce48a241059024..74b7862da4c18c3327632d021e6368090d40f23b 100644 (file)
@@ -75,7 +75,8 @@ public:
 
     /**
      * Gets the current era index. This is calculated only once for an instance of
-     * EraRules.
+     * EraRules. The current era calculation is based on the default time zone at
+     * the time of instantiation.
      *
      * @return era index of current era (or 0, when current date is before the first era)
      */