Autopsy  4.17.0
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-2018 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  */
19 package org.sleuthkit.autopsy.coreutils;
20 
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.GregorianCalendar;
27 import java.util.List;
28 import java.util.SimpleTimeZone;
29 import java.util.TimeZone;
30 
34 public class TimeZoneUtils {
35 
45  public static String convertToAlphaNumericFormat(String timeZoneId) {
46 
47  java.util.TimeZone zone = java.util.TimeZone.getTimeZone(timeZoneId);
48  int offset = zone.getRawOffset() / 1000;
49  int hour = offset / 3600;
50  int min = Math.abs((offset % 3600) / 60);
51 
52  DateFormat dfm = new SimpleDateFormat("z");
53  dfm.setTimeZone(zone);
54  boolean hasDaylight = zone.useDaylightTime();
55  String first = dfm.format(new GregorianCalendar(2010, 1, 1).getTime()).substring(0, 3);
56  String second = dfm.format(new GregorianCalendar(2011, 6, 6).getTime()).substring(0, 3);
57  int mid = hour * -1;
58  String result = first + Integer.toString(mid);
59  if (min != 0) {
60  result = result + ":" + Integer.toString(min);
61  }
62  if (hasDaylight) {
63  result += second;
64  }
65 
66  return result;
67  }
68 
76  public static String createTimeZoneString(TimeZone timeZone) {
77  int offset = timeZone.getRawOffset() / 1000;
78  int hour = offset / 3600;
79  int minutes = Math.abs((offset % 3600) / 60);
80 
81  return String.format("(GMT%+d:%02d) %s", hour, minutes, timeZone.getID()); //NON-NLS
82  }
83 
87  public static List<String> createTimeZoneList() {
88  /*
89  * Create a list of time zones.
90  */
91  List<TimeZone> timeZoneList = new ArrayList<>();
92 
93  String[] ids = SimpleTimeZone.getAvailableIDs();
94  for (String id : ids) {
95  /*
96  * DateFormat dfm = new SimpleDateFormat("z");
97  * dfm.setTimeZone(zone); boolean hasDaylight =
98  * zone.useDaylightTime(); String first = dfm.format(new Date(2010,
99  * 1, 1)); String second = dfm.format(new Date(2011, 6, 6)); int mid
100  * = hour * -1; String result = first + Integer.toString(mid);
101  * if(hasDaylight){ result = result + second; }
102  * timeZoneComboBox.addItem(item + " (" + result + ")");
103  */
104  timeZoneList.add(TimeZone.getTimeZone(id));
105  }
106 
107  /*
108  * Sort the list of time zones first by offset, then by ID.
109  */
110  Collections.sort(timeZoneList, new Comparator<TimeZone>(){
111  @Override
112  public int compare(TimeZone o1, TimeZone o2){
113  int offsetDelta = Integer.compare(o1.getRawOffset(), o2.getRawOffset());
114 
115  if (offsetDelta == 0) {
116  return o1.getID().compareToIgnoreCase(o2.getID());
117  }
118 
119  return offsetDelta;
120  }
121  });
122 
123  /*
124  * Create a list of Strings encompassing both the GMT offset and the
125  * time zone ID.
126  */
127  List<String> outputList = new ArrayList<>();
128 
129  for (TimeZone timeZone : timeZoneList) {
130  outputList.add(createTimeZoneString(timeZone));
131  }
132 
133  return outputList;
134  }
135 
139  private TimeZoneUtils() {
140 
141  }
142 }
static String createTimeZoneString(TimeZone timeZone)
static String convertToAlphaNumericFormat(String timeZoneId)

Copyright © 2012-2021 Basis Technology. Generated on: Tue Jan 19 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.