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  * ```
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * ```
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.healthconnect.controller.utils
17 
18 import android.content.Context
19 import android.text.format.DateFormat.is24HourFormat
20 import dagger.Module
21 import dagger.Provides
22 import dagger.hilt.InstallIn
23 import dagger.hilt.components.SingletonComponent
24 import java.time.Instant
25 import java.time.LocalDateTime
26 import java.time.ZoneId
27 import javax.inject.Singleton
28 
29 /** Time source that uses the system time. */
30 object SystemTimeSource : TimeSource {
currentTimeMillisnull31     override fun currentTimeMillis(): Long {
32         return System.currentTimeMillis()
33     }
34 
deviceZoneOffsetnull35     override fun deviceZoneOffset(): ZoneId {
36         return ZoneId.systemDefault()
37     }
38 
currentLocalDateTimenull39     override fun currentLocalDateTime(): LocalDateTime {
40         return Instant.ofEpochMilli(currentTimeMillis())
41             .atZone(deviceZoneOffset())
42             .toLocalDateTime()
43     }
44 
is24Hournull45     override fun is24Hour(context: Context): Boolean {
46         return is24HourFormat(context)
47     }
48 }
49 
50 @Module
51 @InstallIn(SingletonComponent::class)
52 class SystemTimeSourceModule {
53     @Provides
54     @Singleton
providesSystemTimeSourceModulenull55     fun providesSystemTimeSourceModule(): TimeSource {
56         return SystemTimeSource
57     }
58 }
59