1 /** 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * ``` 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * ``` 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package com.android.healthconnect.controller.utils 15 16 import android.content.Context 17 import android.text.format.DateFormat.* 18 import com.android.healthconnect.controller.R 19 import dagger.hilt.android.qualifiers.ApplicationContext 20 import java.time.Instant 21 import javax.inject.Inject 22 23 /** Formatter for printing time and time ranges. */ 24 class LocalDateTimeFormatter @Inject constructor(@ApplicationContext private val context: Context) { 25 <lambda>null26 private val timeFormat by lazy { getTimeFormat(context) } <lambda>null27 private val longDateFormat by lazy { getLongDateFormat(context) } 28 29 /** Returns localized time. */ formatTimenull30 fun formatTime(instant: Instant): String { 31 return timeFormat.format(instant.toEpochMilli()) 32 } 33 34 /** Returns localized long versions of date. */ formatLongDatenull35 fun formatLongDate(instant: Instant): String { 36 return longDateFormat.format(instant.toEpochMilli()) 37 } 38 39 /** Returns localized time range. */ formatTimeRangenull40 fun formatTimeRange(start: Instant, end: Instant): String { 41 return context.getString(R.string.time_range, formatTime(start), formatTime(end)) 42 } 43 44 /** Returns accessible and localized time range. */ formatTimeRangeA11ynull45 fun formatTimeRangeA11y(start: Instant, end: Instant): String { 46 return context.getString(R.string.time_range_long, formatTime(start), formatTime(end)) 47 } 48 } 49