1 /*
2  * Copyright (C) 2023 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.android.settings.fuelgauge.batteryusage;
18 
19 import java.time.Duration;
20 import java.util.Calendar;
21 
22 /** A utility class for timestamp operations. */
23 final class TimestampUtils {
24 
getNextHourTimestamp(final long timestamp)25     static long getNextHourTimestamp(final long timestamp) {
26         final Calendar calendar = getSharpHourCalendar(timestamp);
27         calendar.add(Calendar.HOUR_OF_DAY, 1);
28         return calendar.getTimeInMillis();
29     }
30 
getNextEvenHourTimestamp(final long timestamp)31     static long getNextEvenHourTimestamp(final long timestamp) {
32         final Calendar calendar = getSharpHourCalendar(timestamp);
33         final int hour = calendar.get(Calendar.HOUR_OF_DAY);
34         calendar.add(Calendar.HOUR_OF_DAY, hour % 2 == 0 ? 2 : 1);
35         return calendar.getTimeInMillis();
36     }
37 
getLastEvenHourTimestamp(final long timestamp)38     static long getLastEvenHourTimestamp(final long timestamp) {
39         final Calendar calendar = getSharpHourCalendar(timestamp);
40         final int hour = calendar.get(Calendar.HOUR_OF_DAY);
41         calendar.add(Calendar.HOUR_OF_DAY, hour % 2 == 0 ? 0 : -1);
42         return calendar.getTimeInMillis();
43     }
44 
getNextDayTimestamp(final long timestamp)45     static long getNextDayTimestamp(final long timestamp) {
46         final Calendar calendar = getSharpHourCalendar(timestamp);
47         calendar.add(Calendar.DAY_OF_YEAR, 1);
48         calendar.set(Calendar.HOUR_OF_DAY, 0);
49         return calendar.getTimeInMillis();
50     }
51 
getSeconds(final long timeInMs)52     static long getSeconds(final long timeInMs) {
53         return Duration.ofMillis(timeInMs).getSeconds();
54     }
55 
isMidnight(final long timestamp)56     static boolean isMidnight(final long timestamp) {
57         final Calendar calendar = Calendar.getInstance();
58         calendar.setTimeInMillis(timestamp);
59         return calendar.get(Calendar.HOUR_OF_DAY) == 0
60                 && calendar.get(Calendar.MINUTE) == 0
61                 && calendar.get(Calendar.SECOND) == 0
62                 && calendar.get(Calendar.MILLISECOND) == 0;
63     }
64 
getSharpHourCalendar(final long timestamp)65     private static Calendar getSharpHourCalendar(final long timestamp) {
66         final Calendar calendar = Calendar.getInstance();
67         calendar.setTimeInMillis(timestamp);
68         calendar.set(Calendar.MINUTE, 0);
69         calendar.set(Calendar.SECOND, 0);
70         calendar.set(Calendar.MILLISECOND, 0);
71         return calendar;
72     }
73 }
74