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 || (map != null 
56          && map.get("time_type") == null
57          && map.get("time_type") == null
58          && map.get("period") == null)) {
59        end = now.getTimeInMillis();
60        start = end - 60 * 60 * 1000;
61      } else if (map.get("period") != null
62          && !map.get("period").equals("")) {
63        String period = map.get("period");
64        this.start = now.getTimeInMillis();
65        this.end = now.getTimeInMillis();
66        if (period.equals("last1hr")) {
67          start = end - (60 * 60 * 1000);
68        } else if (period.equals("last2hr")) {
69          start = end - (2 * 60 * 60 * 1000);
70        } else if (period.equals("last3hr")) {
71          start = end - (3 * 60 * 60 * 1000);
72        } else if (period.equals("last6hr")) {
73          start = end - (6 * 60 * 60 * 1000);
74        } else if (period.equals("last12hr")) {
75          start = end - (12 * 60 * 60 * 1000);
76        } else if (period.equals("last24hr")) {
77          start = end - (24 * 60 * 60 * 1000);
78        } else if (period.equals("last7d")) {
79          start = end - (7 * 24 * 60 * 60 * 1000);
80        } else if (period.equals("last30d")) {
81          start = end - (30 * 24 * 60 * 60 * 1000);
82        }
83      } else if (map.get("start") != null
84          && map.get("end") != null) {
85        start = Long.parseLong(map.get("start"));
86        end = Long.parseLong(map.get("end"));
87      } else if (map.get("time_type").equals("range")) {
88        start = Long.parseLong(map.get("start"));
89        end = Long.parseLong(map.get("end"));
90      } else if (map.get("time_type").equals("last")
91          && map.get("period") != null) {
92        String period = map.get("period");
93        this.start = now.getTimeInMillis();
94        this.end = now.getTimeInMillis();
95        if (period.equals("last1hr")) {
96          start = end - (60 * 60 * 1000);
97        } else if (period.equals("last2hr")) {
98          start = end - (2 * 60 * 60 * 1000);
99        } else if (period.equals("last3hr")) {
100         start = end - (3 * 60 * 60 * 1000);
101       } else if (period.equals("last6hr")) {
102         start = end - (6 * 60 * 60 * 1000);
103       } else if (period.equals("last12hr")) {
104         start = end - (12 * 60 * 60 * 1000);
105       } else if (period.equals("last24hr")) {
106         start = end - (24 * 60 * 60 * 1000);
107       } else if (period.equals("last7d")) {
108         start = end - (7 * 24 * 60 * 60 * 1000);
109       } else if (period.equals("last30d")) {
110         start = end - (30L * 24 * 60 * 60 * 1000);
111       }
112     }
113     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
114     SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
115     SimpleDateFormat formatHour = new SimpleDateFormat("HH");
116     SimpleDateFormat formatMin = new SimpleDateFormat("mm");
117 
118     formatter.setTimeZone(this.tz);
119     formatDate.setTimeZone(this.tz);
120     formatHour.setTimeZone(this.tz);
121     formatMin.setTimeZone(this.tz);
122 
123     startS = formatter.format(start);
124     this.startDate = formatDate.format(start);
125     this.startHour = formatHour.format(start);
126     this.startMin = formatMin.format(start);
127     endS = formatter.format(end);
128     this.endDate = formatDate.format(end);
129     this.endHour = formatHour.format(end);
130     this.endMin = formatMin.format(end);
131   }
132 
133   public String getStartDate(String format) {
134     SimpleDateFormat formatter = new SimpleDateFormat(format);
135     formatter.setTimeZone(this.tz);
136     return formatter.format(this.start);
137   }
138 
139   public String getStartDate() {
140     return this.startDate;
141   }
142 
143   public String getStartHour() {
144     return this.startHour;
145   }
146 
147   public String getStartMinute() {
148     return this.startMin;
149   }
150 
151   public String getStartTimeText() {
152     return this.startS;
153   }
154 
155   public long getStartTime() {
156     return start;
157   }
158 
159   public String getEndDate(String format) {
160     SimpleDateFormat formatter = new SimpleDateFormat(format);
161     formatter.setTimeZone(this.tz);
162     return formatter.format(this.end);
163   }
164 
165   public String getEndDate() {
166     return this.endDate;
167   }
168 
169   public String getEndHour() {
170     return this.endHour;
171   }
172 
173   public String getEndMinute() {
174     return this.endMin;
175   }
176 
177   public String getEndTimeText() {
178     return this.endS;
179   }
180 
181   public long getEndTime() {
182     return end;
183   }
184 
185 }