Autopsy 4.22.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
TimeZoneUtils.java
Go to the documentation of this file.
1/*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2011-2021 Basis Technology Corp.
5 * Contact: carrier <at> sleuthkit <dot> org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package org.sleuthkit.autopsy.coreutils;
20
21import java.text.DateFormat;
22import java.text.SimpleDateFormat;
23import java.util.ArrayList;
24import java.util.Collections;
25import java.util.Comparator;
26import java.util.GregorianCalendar;
27import java.util.List;
28import java.util.SimpleTimeZone;
29import java.util.TimeZone;
30import org.sleuthkit.autopsy.core.UserPreferences;
31import org.sleuthkit.datamodel.TimeUtilities;
32
36public class TimeZoneUtils {
37
47 public static String convertToAlphaNumericFormat(String timeZoneId) {
48
49 java.util.TimeZone zone = java.util.TimeZone.getTimeZone(timeZoneId);
50 int offset = zone.getRawOffset() / 1000;
51 int hour = offset / 3600;
52 int min = Math.abs((offset % 3600) / 60);
53
54 DateFormat dfm = new SimpleDateFormat("z");
55 dfm.setTimeZone(zone);
56 boolean hasDaylight = zone.useDaylightTime();
57 String first = dfm.format(new GregorianCalendar(2010, 1, 1).getTime()).substring(0, 3);
58 String second = dfm.format(new GregorianCalendar(2011, 6, 6).getTime()).substring(0, 3);
59 int mid = hour * -1;
60 String result = first + Integer.toString(mid);
61 if (min != 0) {
62 result = result + ":" + Integer.toString(min);
63 }
64 if (hasDaylight) {
65 result += second;
66 }
67
68 return result;
69 }
70
78 public static String createTimeZoneString(TimeZone timeZone) {
79 int offset = timeZone.getRawOffset() / 1000;
80 int hour = offset / 3600;
81 int minutes = Math.abs((offset % 3600) / 60);
82
83 return String.format("(GMT%+d:%02d) %s", hour, minutes, timeZone.getID()); //NON-NLS
84 }
85
89 public static List<String> createTimeZoneList() {
90 /*
91 * Create a list of time zones.
92 */
93 List<TimeZone> timeZoneList = new ArrayList<>();
94
95 String[] ids = SimpleTimeZone.getAvailableIDs();
96 for (String id : ids) {
97 /*
98 * DateFormat dfm = new SimpleDateFormat("z");
99 * dfm.setTimeZone(zone); boolean hasDaylight =
100 * zone.useDaylightTime(); String first = dfm.format(new Date(2010,
101 * 1, 1)); String second = dfm.format(new Date(2011, 6, 6)); int mid
102 * = hour * -1; String result = first + Integer.toString(mid);
103 * if(hasDaylight){ result = result + second; }
104 * timeZoneComboBox.addItem(item + " (" + result + ")");
105 */
106 timeZoneList.add(TimeZone.getTimeZone(id));
107 }
108
109 /*
110 * Sort the list of time zones first by offset, then by ID.
111 */
112 Collections.sort(timeZoneList, new Comparator<TimeZone>() {
113 @Override
114 public int compare(TimeZone o1, TimeZone o2) {
115 int offsetDelta = Integer.compare(o1.getRawOffset(), o2.getRawOffset());
116
117 if (offsetDelta == 0) {
118 return o1.getID().compareToIgnoreCase(o2.getID());
119 }
120
121 return offsetDelta;
122 }
123 });
124
125 /*
126 * Create a list of Strings encompassing both the GMT offset and the
127 * time zone ID.
128 */
129 List<String> outputList = new ArrayList<>();
130
131 for (TimeZone timeZone : timeZoneList) {
132 outputList.add(createTimeZoneString(timeZone));
133 }
134
135 return outputList;
136 }
137
145 public static String getFormattedTime(long epochTime) {
146 return TimeUtilities.epochToTime(epochTime, getTimeZone());
147 }
148
157 public static String getFormattedTimeISO8601(long epochTime) {
158 return TimeUtilities.epochToTimeISO8601(epochTime, getTimeZone());
159 }
160
166 public static TimeZone getTimeZone() {
168 return TimeZone.getDefault();
169 }
170
171 return TimeZone.getTimeZone(UserPreferences.getTimeZoneForDisplays());
172 }
173
177 private TimeZoneUtils() {
178
179 }
180}
static String getFormattedTimeISO8601(long epochTime)
static String convertToAlphaNumericFormat(String timeZoneId)
static String createTimeZoneString(TimeZone timeZone)
static String getFormattedTime(long epochTime)

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.