1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you
5  * may not use this file except in compliance with the License. You may
6  * 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
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 package com.android.vts.util;
17 
18 import java.time.Instant;
19 import java.time.ZoneId;
20 import java.time.ZonedDateTime;
21 import java.time.format.DateTimeFormatter;
22 import java.util.concurrent.TimeUnit;
23 import java.util.logging.Logger;
24 
25 /** TimeUtil, a helper class for formatting times. */
26 public class TimeUtil {
27     protected static final Logger logger = Logger.getLogger(TimeUtil.class.getName());
28 
29     public static final String DATE_FORMAT = "yyyy-MM-dd";
30     public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
31     public static final String DATE_TIME_ZONE_FORMAT = "yyyy-MM-dd HH:mm:ss (z)";
32     public static final ZoneId PT_ZONE = ZoneId.of("America/Los_Angeles");
33 
34     /**
35      * Create a date time string with zone info from the provided timestamp.
36      *
37      * @param timeMicroseconds The time in microseconds
38      * @return A formatted date time string.
39      */
getDateTimeZoneString(long timeMicroseconds)40     public static String getDateTimeZoneString(long timeMicroseconds) {
41         long timeMillis = TimeUnit.MICROSECONDS.toMillis(timeMicroseconds);
42         ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), PT_ZONE);
43         return DateTimeFormatter.ofPattern(DATE_TIME_ZONE_FORMAT).format(zdt);
44     }
45 
46     /**
47      * Create a date string from the provided timestamp.
48      *
49      * @param timeMicroseconds The time in microseconds
50      * @return A formatted date string.
51      */
getDateString(long timeMicroseconds)52     public static String getDateString(long timeMicroseconds) {
53         long timeMillis = TimeUnit.MICROSECONDS.toMillis(timeMicroseconds);
54         ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), PT_ZONE);
55         return DateTimeFormatter.ofPattern(DATE_FORMAT).format(zdt);
56     }
57 
58     /**
59      * Create a date string from the provided timestamp.
60      *
61      * @param timeMicroseconds The time in microseconds
62      * @return A formatted date string.
63      */
getDateTimeString(long timeMicroseconds)64     public static String getDateTimeString(long timeMicroseconds) {
65         long timeMillis = TimeUnit.MICROSECONDS.toMillis(timeMicroseconds);
66         ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), PT_ZONE);
67         return DateTimeFormatter.ofPattern(DATE_TIME_FORMAT).format(zdt);
68     }
69 
70     /**
71      * Create a ZonedDateTime object from the provided timestamp.
72      *
73      * @param timeMicroseconds The time in microseconds
74      * @return A ZonedDateTime object.
75      */
getZonedDateTime(long timeMicroseconds)76     public static ZonedDateTime getZonedDateTime(long timeMicroseconds) {
77         long timeMillis = TimeUnit.MICROSECONDS.toMillis(timeMicroseconds);
78         return ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), PT_ZONE);
79     }
80 }
81