aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/car/calendar/common/CalendarFormatter.java
blob: 175bfc1d8880ee2f44baf217d7ae9e8909a34ea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.car.calendar.common;

import static android.text.format.DateUtils.FORMAT_ABBREV_ALL;
import static android.text.format.DateUtils.FORMAT_NO_YEAR;
import static android.text.format.DateUtils.FORMAT_SHOW_TIME;

import android.content.Context;
import android.icu.text.DisplayContext;
import android.icu.text.RelativeDateTimeFormatter;
import android.icu.util.ULocale;
import android.text.format.DateUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;
import java.util.TimeZone;

/** App specific text formatting utility. */
public class CalendarFormatter {
    private static final String TAG = "CarCalendarFormatter";
    private static final String SPACED_BULLET = " \u2022 ";
    private final Context mContext;
    private final Locale mLocale;
    private final Clock mClock;
    private final DateFormat mDateFormat;

    public CalendarFormatter(Context context, Locale locale, Clock clock) {
        mContext = context;
        mLocale = locale;
        mClock = clock;

        String pattern =
                android.text.format.DateFormat.getBestDateTimePattern(mLocale, "EEE, d MMM");
        mDateFormat = new SimpleDateFormat(pattern, mLocale);
        mDateFormat.setTimeZone(TimeZone.getTimeZone(mClock.getZone()));
    }

    /** Formats the given date to text. */
    public String getDateText(LocalDate localDate) {
        RelativeDateTimeFormatter formatter =
                RelativeDateTimeFormatter.getInstance(
                        ULocale.forLocale(mLocale),
                        null,
                        RelativeDateTimeFormatter.Style.LONG,
                        DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE);

        LocalDate today = LocalDate.now(mClock);
        LocalDate tomorrow = today.plusDays(1);
        LocalDate dayAfter = tomorrow.plusDays(1);

        String relativeDay = null;
        if (localDate.equals(today)) {
            relativeDay =
                    formatter.format(
                            RelativeDateTimeFormatter.Direction.THIS,
                            RelativeDateTimeFormatter.AbsoluteUnit.DAY);
        } else if (localDate.equals(tomorrow)) {
            relativeDay =
                    formatter.format(
                            RelativeDateTimeFormatter.Direction.NEXT,
                            RelativeDateTimeFormatter.AbsoluteUnit.DAY);
        } else if (localDate.equals(dayAfter)) {
            relativeDay =
                    formatter.format(
                            RelativeDateTimeFormatter.Direction.NEXT_2,
                            RelativeDateTimeFormatter.AbsoluteUnit.DAY);
        }

        StringBuilder result = new StringBuilder();
        if (relativeDay != null) {
            result.append(relativeDay);
            result.append(SPACED_BULLET);
        }

        ZonedDateTime zonedDateTime = localDate.atStartOfDay(mClock.getZone());
        Date date = new Date(zonedDateTime.toInstant().toEpochMilli());
        result.append(mDateFormat.format(date));
        return result.toString();
    }

    /** Formats the given time to text. */
    public String getTimeRangeText(Instant start, Instant end) {
        Formatter formatter = new Formatter(new StringBuilder(50), mLocale);
        return DateUtils.formatDateRange(
                        mContext,
                        formatter,
                        start.toEpochMilli(),
                        end.toEpochMilli(),
                        FORMAT_SHOW_TIME | FORMAT_NO_YEAR | FORMAT_ABBREV_ALL,
                        mClock.getZone().getId())
                .toString();
    }
}