Skip to content

Commit ff5993d

Browse files
pekingmedsn5ft
authored andcommitted
Fixed MaterialDatePicker todayInUtcMilliseconds method.
- Used UtcDates to return the UTC milliseconds of today in local timezone. - Previous method is replaced with thisMonthInUtcMilliseconds, which fits its function better. Resolves #778 PiperOrigin-RevId: 283981946 (cherry picked from commit 77ce281)
1 parent 6178e71 commit ff5993d

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

catalog/java/io/material/catalog/datepicker/DatePickerMainDemoFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private static Calendar getClearedUtc() {
5656
}
5757

5858
private void initSettings() {
59-
today = MaterialDatePicker.todayInUtcMilliseconds();
59+
today = MaterialDatePicker.thisMonthInUtcMilliseconds();
6060
Calendar calendar = getClearedUtc();
6161
calendar.setTimeInMillis(today);
6262
calendar.roll(Calendar.MONTH, 1);

lib/java/com/google/android/material/datepicker/CalendarConstraints.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public Builder setValidator(DateValidator validator) {
294294
@NonNull
295295
public CalendarConstraints build() {
296296
if (openAt == null) {
297-
long today = MaterialDatePicker.todayInUtcMilliseconds();
297+
long today = MaterialDatePicker.thisMonthInUtcMilliseconds();
298298
openAt = start <= today && today <= end ? today : start;
299299
}
300300
Bundle deepCopyBundle = new Bundle();

lib/java/com/google/android/material/datepicker/MaterialDatePicker.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,15 @@ public final class MaterialDatePicker<S> extends DialogFragment {
6565
static final Object CANCEL_BUTTON_TAG = "CANCEL_BUTTON_TAG";
6666
static final Object TOGGLE_BUTTON_TAG = "TOGGLE_BUTTON_TAG";
6767

68-
/** Returns a value of UTC milliseconds representing today for the device's current timezone. */
68+
/** Returns the UTC milliseconds representing the first moment of today in local timezone. */
6969
public static long todayInUtcMilliseconds() {
70+
return UtcDates.getTodayCalendar().getTimeInMillis();
71+
}
72+
73+
/**
74+
* Returns the UTC milliseconds representing the first moment in current month in local timezone.
75+
*/
76+
public static long thisMonthInUtcMilliseconds() {
7077
return Month.today().timeInMillis;
7178
}
7279

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2019 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.material.datepicker;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.time.Duration;
22+
import java.util.Calendar;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.robolectric.RobolectricTestRunner;
26+
27+
/** Test for {@link com.google.android.material.datepicker.MaterialDatePicker}. */
28+
@RunWith(RobolectricTestRunner.class)
29+
public class MaterialDatePickerTest {
30+
31+
private static final long ONE_DAY_MILLIS = Duration.ofDays(1).toMillis();
32+
33+
@Test
34+
public void testTodayInUtcMilliseconds() {
35+
long todayUtcMS = MaterialDatePicker.todayInUtcMilliseconds();
36+
Calendar outputUtcTodayInCalendar = Calendar.getInstance(UtcDates.getTimeZone());
37+
outputUtcTodayInCalendar.setTimeInMillis(todayUtcMS);
38+
Calendar expectedUtcTodayInCalendar = Calendar.getInstance(UtcDates.getTimeZone());
39+
40+
// Assert fields finer than a day are stripped.
41+
assertThat(todayUtcMS % ONE_DAY_MILLIS).isEqualTo(0);
42+
// Assert this is the same date as today in UTC.
43+
assertThat(outputUtcTodayInCalendar.get(Calendar.YEAR))
44+
.isEqualTo(expectedUtcTodayInCalendar.get(Calendar.YEAR));
45+
assertThat(outputUtcTodayInCalendar.get(Calendar.MONTH))
46+
.isEqualTo(expectedUtcTodayInCalendar.get(Calendar.MONTH));
47+
assertThat(outputUtcTodayInCalendar.get(Calendar.DATE))
48+
.isEqualTo(expectedUtcTodayInCalendar.get(Calendar.DATE));
49+
}
50+
51+
@Test
52+
public void testThisMonthInUtcMilliseconds() {
53+
long thisMonthUtcMS = MaterialDatePicker.thisMonthInUtcMilliseconds();
54+
Calendar outputUtcThisMonthInCalendar = Calendar.getInstance(UtcDates.getTimeZone());
55+
outputUtcThisMonthInCalendar.setTimeInMillis(thisMonthUtcMS);
56+
Calendar expectedUtcThisMonthInCalendar = Calendar.getInstance(UtcDates.getTimeZone());
57+
58+
// Assert fields finer than a day are stripped.
59+
assertThat(thisMonthUtcMS % ONE_DAY_MILLIS).isEqualTo(0);
60+
// Assert this is the first day of this month in UTC.
61+
assertThat(outputUtcThisMonthInCalendar.get(Calendar.YEAR))
62+
.isEqualTo(expectedUtcThisMonthInCalendar.get(Calendar.YEAR));
63+
assertThat(outputUtcThisMonthInCalendar.get(Calendar.MONTH))
64+
.isEqualTo(expectedUtcThisMonthInCalendar.get(Calendar.MONTH));
65+
assertThat(outputUtcThisMonthInCalendar.get(Calendar.DATE)).isEqualTo(1);
66+
}
67+
}

0 commit comments

Comments
 (0)