Skip to content

Commit 939f90e

Browse files
authored
Pass "Locale-US" to SimpleDateFormat while formatting first-open time… (firebase#3877)
* Pass "Locale-US" to SimpleDateFormat while formatting first-open time. This ensures that the english numerals in the timestamp are not converted to the local language numerals. This is a fix for firebase#3757 * adding more detail to a comment
1 parent 1b5e956 commit 939f90e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

firebase-config/src/main/java/com/google/firebase/remoteconfig/internal/ConfigFetchHttpClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ private JSONObject createFetchRequestBody(
353353
}
354354

355355
private String convertToISOString(long millisFromEpoch) {
356-
SimpleDateFormat isoDateFormat = new SimpleDateFormat(ISO_DATE_PATTERN);
356+
// ISO-8601 Timestamp expects Western Arabic numerals. Locale.US ensures that the english
357+
// numerals are not converted to the local language numerals.
358+
SimpleDateFormat isoDateFormat = new SimpleDateFormat(ISO_DATE_PATTERN, Locale.US);
357359
isoDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
358360
return isoDateFormat.format(millisFromEpoch);
359361
}

firebase-config/src/test/java/com/google/firebase/remoteconfig/internal/ConfigFetchHttpClientTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,25 @@ public void fetch_nullFirstOpenTime_fieldNotPresentInRequestBody() throws Except
240240
assertFalse(requestBody.has(FIRST_OPEN_TIME));
241241
}
242242

243+
@Test
244+
public void fetch_firstOpenTimeFromNonEnglishLanguageLocale_digitsInEnglishInRequestBody()
245+
throws Exception {
246+
String languageTag = "ar-AE"; // Language Tag for UAE Arabic
247+
Locale.setDefault(Locale.forLanguageTag(languageTag));
248+
249+
setServerResponseTo(noChangeResponseBody, SECOND_ETAG);
250+
251+
Map<String, String> customUserProperties = ImmutableMap.of("up1", "hello", "up2", "world");
252+
long firstOpenTimeEpochFromMillis = 1636146000000L;
253+
// ISO-8601 value corresponding to 1636146000000 ms-from-epoch in UTC
254+
String firstOpenTimeIsoString = "2021-11-05T21:00:00.000Z";
255+
256+
fetch(FIRST_ETAG, customUserProperties, firstOpenTimeEpochFromMillis);
257+
258+
JSONObject requestBody = new JSONObject(fakeHttpURLConnection.getOutputStream().toString());
259+
assertThat(requestBody.get(FIRST_OPEN_TIME)).isEqualTo(firstOpenTimeIsoString);
260+
}
261+
243262
@Test
244263
public void fetch_requestEncodesLanguageSubtags() throws Exception {
245264
String languageTag = "zh-Hant-TW"; // Taiwan Chinese in traditional script

0 commit comments

Comments
 (0)