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 java.time.Instant
17 import java.time.Instant.ofEpochMilli
18 import java.time.LocalDate
19 import java.time.LocalTime
20 import java.time.ZoneId
21 
22 /**
23  * Returns an Instant with the specified year, month and day-of-month. The day must be valid for the
24  * year and month, otherwise an exception will be thrown.
25  *
26  * @param year the year to represent, from MIN_YEAR to MAX_YEAR
27  * @param month the month-of-year to represent, from 1 (January) to 12 (December)
28  * @param day the day-of-month to represent, from 1 to 31
29  */
getInstantnull30 fun getInstant(year: Int, month: Int, day: Int): Instant {
31     val date = LocalDate.of(year, month, day)
32     return date.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant()
33 }
34 
Longnull35 fun Long.toInstant(): Instant {
36     return ofEpochMilli(this)
37 }
38 
toLocalDatenull39 fun Instant.toLocalDate(): LocalDate {
40     return atZone(ZoneId.systemDefault()).toLocalDate()
41 }
42 
Instantnull43 fun Instant.toLocalTime(): LocalTime {
44     return atZone(ZoneId.systemDefault()).toLocalTime()
45 }
46