This project has retired. For details please refer to its Attic page.
OfflineTimeHandler xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.chukwa.hicc;
20  
21  import java.util.Calendar;
22  import java.util.TimeZone;
23  import java.text.SimpleDateFormat;
24  import java.util.HashMap;
25  
26  public class OfflineTimeHandler {
27    private TimeZone tz = null;
28    private long start = 0;
29    private long end = 0;
30    private String startDate = null;
31    private String startHour = null;
32    private String startMin = null;
33    private String endDate = null;
34    private String endHour = null;
35    private String endMin = null;
36    private String startS = null;
37    private String endS = null;
38    
39    public OfflineTimeHandler(HashMap<String, String> map) {
40      this.tz = TimeZone.getTimeZone("UTC");
41      init(map);
42    }
43  
44    public OfflineTimeHandler(HashMap<String, String> map, String tz) {
45      if (tz != null) {
46        this.tz = TimeZone.getTimeZone(tz);
47      } else {
48        this.tz = TimeZone.getTimeZone("UTC");
49      }
50      init(map);
51    }
52  
53    public void init(HashMap<String, String> map) {
54      Calendar now = Calendar.getInstance();
55      if (map == null || 
56          (map.get("time_type") == null && map.get("period") == null)) {
57        end = now.getTimeInMillis();
58        start = end - 60 * 60 * 1000;
59      } else if (map.get("period") != null
60          && !map.get("period").equals("")) {
61        String period = map.get("period");
62        this.start = now.getTimeInMillis();
63        this.end = now.getTimeInMillis();
64        if (period.equals("last1hr")) {
65          start = end - (60 * 60 * 1000);
66        } else if (period.equals("last2hr")) {
67          start = end - (2 * 60 * 60 * 1000);
68        } else if (period.equals("last3hr")) {
69          start = end - (3 * 60 * 60 * 1000);
70        } else if (period.equals("last6hr")) {
71          start = end - (6 * 60 * 60 * 1000);
72        } else if (period.equals("last12hr")) {
73          start = end - (12 * 60 * 60 * 1000);
74        } else if (period.equals("last24hr")) {
75          start = end - (24 * 60 * 60 * 1000);
76        } else if (period.equals("last7d")) {
77          start = end - (7 * 24 * 60 * 60 * 1000);
78        } else if (period.equals("last30d")) {
79          start = end - (30 * 24 * 60 * 60 * 1000);
80        }
81      } else if (map.get("start") != null
82          && map.get("end") != null) {
83        start = Long.parseLong(map.get("start"));
84        end = Long.parseLong(map.get("end"));
85      } else if (map.get("time_type").equals("range")) {
86        start = Long.parseLong(map.get("start"));
87        end = Long.parseLong(map.get("end"));
88      } else if (map.get("time_type").equals("last")
89          && map.get("period") != null) {
90        String period = map.get("period");
91        this.start = now.getTimeInMillis();
92        this.end = now.getTimeInMillis();
93        if (period.equals("last1hr")) {
94          start = end - (60 * 60 * 1000);
95        } else if (period.equals("last2hr")) {
96          start = end - (2 * 60 * 60 * 1000);
97        } else if (period.equals("last3hr")) {
98          start = end - (3 * 60 * 60 * 1000);
99        } else if (period.equals("last6hr")) {
100         start = end - (6 * 60 * 60 * 1000);
101       } else if (period.equals("last12hr")) {
102         start = end - (12 * 60 * 60 * 1000);
103       } else if (period.equals("last24hr")) {
104         start = end - (24 * 60 * 60 * 1000);
105       } else if (period.equals("last7d")) {
106         start = end - (7 * 24 * 60 * 60 * 1000);
107       } else if (period.equals("last30d")) {
108         start = end - (30L * 24 * 60 * 60 * 1000);
109       }
110     }
111     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
112     SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
113     SimpleDateFormat formatHour = new SimpleDateFormat("HH");
114     SimpleDateFormat formatMin = new SimpleDateFormat("mm");
115 
116     formatter.setTimeZone(this.tz);
117     formatDate.setTimeZone(this.tz);
118     formatHour.setTimeZone(this.tz);
119     formatMin.setTimeZone(this.tz);
120 
121     startS = formatter.format(start);
122     this.startDate = formatDate.format(start);
123     this.startHour = formatHour.format(start);
124     this.startMin = formatMin.format(start);
125     endS = formatter.format(end);
126     this.endDate = formatDate.format(end);
127     this.endHour = formatHour.format(end);
128     this.endMin = formatMin.format(end);
129   }
130 
131   public String getStartDate(String format) {
132     SimpleDateFormat formatter = new SimpleDateFormat(format);
133     formatter.setTimeZone(this.tz);
134     return formatter.format(this.start);
135   }
136 
137   public String getStartDate() {
138     return this.startDate;
139   }
140 
141   public String getStartHour() {
142     return this.startHour;
143   }
144 
145   public String getStartMinute() {
146     return this.startMin;
147   }
148 
149   public String getStartTimeText() {
150     return this.startS;
151   }
152 
153   public long getStartTime() {
154     return start;
155   }
156 
157   public String getEndDate(String format) {
158     SimpleDateFormat formatter = new SimpleDateFormat(format);
159     formatter.setTimeZone(this.tz);
160     return formatter.format(this.end);
161   }
162 
163   public String getEndDate() {
164     return this.endDate;
165   }
166 
167   public String getEndHour() {
168     return this.endHour;
169   }
170 
171   public String getEndMinute() {
172     return this.endMin;
173   }
174 
175   public String getEndTimeText() {
176     return this.endS;
177   }
178 
179   public long getEndTime() {
180     return end;
181   }
182 
183 }