|
| 1 | +// |
| 2 | +// Source code recreated from a .class file by IntelliJ IDEA |
| 3 | +// (powered by FernFlower decompiler) |
| 4 | +// |
| 5 | + |
| 6 | +package org.apache.hadoop.hive.common.type; |
| 7 | + |
| 8 | +import org.apache.hive.common.util.DateParser; |
| 9 | + |
| 10 | +import java.sql.Date; |
| 11 | +import java.text.SimpleDateFormat; |
| 12 | +import java.util.Calendar; |
| 13 | +import java.util.TimeZone; |
| 14 | + |
| 15 | +public class HiveDate extends Date { |
| 16 | + private static boolean dateShowTime = true; |
| 17 | + private java.util.Date privateDate = null; |
| 18 | + private static final long serialVersionUID = -261750473771622491L; |
| 19 | + private static final ThreadLocal<TimeZone> LOCAL_TIMEZONE = |
| 20 | + new ThreadLocal<TimeZone>() { |
| 21 | + protected TimeZone initialValue() { |
| 22 | + return Calendar.getInstance().getTimeZone(); |
| 23 | + } |
| 24 | + }; |
| 25 | + |
| 26 | + public HiveDate() { |
| 27 | + super(System.currentTimeMillis()); |
| 28 | + this.privateDate = new java.util.Date(this.getTime()); |
| 29 | + } |
| 30 | + |
| 31 | + // private static boolean isDateShowTime() { |
| 32 | + // String confValue = GlobalHiveConf.get(ConfVars.INCEPTOR_DATE_SHOW_TIME.varname); |
| 33 | + // return confValue.toLowerCase().equalsIgnoreCase("true"); |
| 34 | + // } |
| 35 | + |
| 36 | + /** @deprecated */ |
| 37 | + @Deprecated |
| 38 | + public HiveDate(int year, int month, int day) { |
| 39 | + super(year, month, day); |
| 40 | + this.privateDate = new java.util.Date(this.getTime()); |
| 41 | + } |
| 42 | + |
| 43 | + /** @deprecated */ |
| 44 | + @Deprecated |
| 45 | + public HiveDate(int year, int month, int day, int hrs, int min) { |
| 46 | + super(0L); |
| 47 | + this.privateDate = new java.util.Date(year, month, day, hrs, min); |
| 48 | + this.setTime(this.privateDate.getTime()); |
| 49 | + } |
| 50 | + |
| 51 | + /** @deprecated */ |
| 52 | + @Deprecated |
| 53 | + public HiveDate(int year, int month, int day, int hrs, int min, int sec) { |
| 54 | + super(0L); |
| 55 | + this.privateDate = new java.util.Date(year, month, day, hrs, min, sec); |
| 56 | + this.setTime(this.privateDate.getTime()); |
| 57 | + } |
| 58 | + |
| 59 | + public HiveDate(long date) { |
| 60 | + super(date); |
| 61 | + this.privateDate = new java.util.Date(date); |
| 62 | + } |
| 63 | + |
| 64 | + public java.util.Date getPrivateDate() { |
| 65 | + return this.privateDate; |
| 66 | + } |
| 67 | + |
| 68 | + public void setPrivateDate(java.util.Date privateDate) { |
| 69 | + this.privateDate = privateDate; |
| 70 | + } |
| 71 | + |
| 72 | + public void setTime(long date) { |
| 73 | + super.setTime(date); |
| 74 | + this.privateDate.setTime(date); |
| 75 | + } |
| 76 | + |
| 77 | + public static HiveDate valueOf(String s) { |
| 78 | + int dateStrLen = s.length(); |
| 79 | + HiveDate d = null; |
| 80 | + Calendar calendar = Calendar.getInstance(); |
| 81 | + String hourStr = null; |
| 82 | + if (s == null) { |
| 83 | + throw new IllegalArgumentException("String " + s + " can not be converted to Date"); |
| 84 | + } else { |
| 85 | + if (s.indexOf(32) != -1) { |
| 86 | + String[] dateStr = s.split("\\s+"); |
| 87 | + if (dateStr.length < 1 || dateStr.length > 2) { |
| 88 | + throw new IllegalArgumentException( |
| 89 | + "String " + s + " can not be converted to Date"); |
| 90 | + } |
| 91 | + |
| 92 | + if (dateStr.length == 2) { |
| 93 | + s = dateStr[0]; |
| 94 | + hourStr = dateStr[1]; |
| 95 | + } |
| 96 | + |
| 97 | + dateStrLen = dateStr[0].length(); |
| 98 | + } |
| 99 | + |
| 100 | + int firstDash = s.indexOf(45); |
| 101 | + int secondDash = s.indexOf(45, firstDash + 1); |
| 102 | + String mm; |
| 103 | + String dd; |
| 104 | + String day; |
| 105 | + DateParser dateParser; |
| 106 | + if (firstDash == 2 && secondDash == 6 && dateStrLen == 9) { |
| 107 | + d = new HiveDate(); |
| 108 | + dateParser = new DateParser(new SimpleDateFormat("dd-MMM-yy")); |
| 109 | + if (!dateParser.parseDate(s, d, false)) { |
| 110 | + throw new IllegalArgumentException( |
| 111 | + "String " + s + " can not be converted to Date"); |
| 112 | + } |
| 113 | + |
| 114 | + if (d != null && hourStr != null) { |
| 115 | + calendar.setTime(d); |
| 116 | + mm = String.valueOf(calendar.get(1)); |
| 117 | + dd = String.valueOf(calendar.get(2) + 1); |
| 118 | + day = String.valueOf(calendar.get(5)); |
| 119 | + d = getHiveDate(mm, dd, day, hourStr); |
| 120 | + } |
| 121 | + } else if (firstDash == 2 && secondDash == 6 && dateStrLen == 11) { |
| 122 | + d = new HiveDate(); |
| 123 | + dateParser = new DateParser(new SimpleDateFormat("dd-MMM-yyyy")); |
| 124 | + if (!dateParser.parseDate(s, d, false)) { |
| 125 | + throw new IllegalArgumentException( |
| 126 | + "String " + s + " can not be converted to Date"); |
| 127 | + } |
| 128 | + |
| 129 | + if (d != null && hourStr != null) { |
| 130 | + calendar.setTime(d); |
| 131 | + mm = String.valueOf(calendar.get(1)); |
| 132 | + dd = String.valueOf(calendar.get(2) + 1); |
| 133 | + day = String.valueOf(calendar.get(5)); |
| 134 | + d = getHiveDate(mm, dd, day, hourStr); |
| 135 | + } |
| 136 | + } else { |
| 137 | + String yyyy; |
| 138 | + if (firstDash > 0 && secondDash > 0 && secondDash < s.length() - 1) { |
| 139 | + yyyy = s.substring(0, firstDash); |
| 140 | + mm = s.substring(firstDash + 1, secondDash); |
| 141 | + dd = s.substring(secondDash + 1); |
| 142 | + d = getHiveDate(yyyy, mm, dd, hourStr); |
| 143 | + } else if (s.length() == 8 && hourStr == null) { |
| 144 | + yyyy = s.substring(0, 4); |
| 145 | + mm = s.substring(4, 6); |
| 146 | + dd = s.substring(6, 8); |
| 147 | + d = getHiveDate(yyyy, mm, dd, (String) null); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if (d == null) { |
| 152 | + throw new IllegalArgumentException("String " + s + " can not be converted to Date"); |
| 153 | + } else { |
| 154 | + return d; |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static HiveDate getHiveDate(String yyyy, String mm, String dd, String hourStr) { |
| 160 | + HiveDate d = null; |
| 161 | + if (yyyy.length() == 4 |
| 162 | + && mm.length() > 0 |
| 163 | + && mm.length() <= 2 |
| 164 | + && dd.length() <= 2 |
| 165 | + && dd.length() > 0) { |
| 166 | + int year = Integer.parseInt(yyyy); |
| 167 | + int month = Integer.parseInt(mm); |
| 168 | + int day = Integer.parseInt(dd); |
| 169 | + if (month >= 1 && month <= 12) { |
| 170 | + int maxDays = 31; |
| 171 | + switch (month) { |
| 172 | + case 2: |
| 173 | + if ((year % 4 != 0 || year % 100 == 0) && year % 400 != 0) { |
| 174 | + maxDays = 28; |
| 175 | + } else { |
| 176 | + maxDays = 29; |
| 177 | + } |
| 178 | + case 3: |
| 179 | + case 5: |
| 180 | + case 7: |
| 181 | + case 8: |
| 182 | + case 10: |
| 183 | + default: |
| 184 | + break; |
| 185 | + case 4: |
| 186 | + case 6: |
| 187 | + case 9: |
| 188 | + case 11: |
| 189 | + maxDays = 30; |
| 190 | + } |
| 191 | + |
| 192 | + if (day >= 1 && day <= maxDays) { |
| 193 | + if (hourStr == null) { |
| 194 | + d = new HiveDate(year - 1900, month - 1, day); |
| 195 | + } else { |
| 196 | + int firstColon = hourStr.indexOf(58); |
| 197 | + int secondColon = hourStr.indexOf(58, firstColon + 1); |
| 198 | + int period = hourStr.indexOf(46, secondColon + 1); |
| 199 | + if (firstColon > 0 |
| 200 | + && secondColon > 0 |
| 201 | + && secondColon < hourStr.length() - 1) { |
| 202 | + String hh = hourStr.substring(0, firstColon); |
| 203 | + String mM = hourStr.substring(firstColon + 1, secondColon); |
| 204 | + String ss = hourStr.substring(secondColon + 1); |
| 205 | + String ms = null; |
| 206 | + if (period > 0) { |
| 207 | + ss = hourStr.substring(secondColon + 1, period); |
| 208 | + ms = hourStr.substring(period + 1).trim(); |
| 209 | + } |
| 210 | + |
| 211 | + int millis; |
| 212 | + if (hh.length() == 2 && mM.length() == 2 && ss.length() == 2) { |
| 213 | + int hour = Integer.parseInt(hh); |
| 214 | + millis = Integer.parseInt(mM); |
| 215 | + int second = Integer.parseInt(ss); |
| 216 | + if (month >= 1 |
| 217 | + && month <= 12 |
| 218 | + && day >= 1 |
| 219 | + && day <= 31 |
| 220 | + && hour >= 0 & hour <= 23 |
| 221 | + && millis >= 0 |
| 222 | + && millis <= 59 |
| 223 | + && second >= 0 |
| 224 | + && second <= 59) { |
| 225 | + d = |
| 226 | + new HiveDate( |
| 227 | + year - 1900, |
| 228 | + month - 1, |
| 229 | + day, |
| 230 | + hour, |
| 231 | + millis, |
| 232 | + second); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + if (ms != null && !ms.isEmpty()) { |
| 237 | + char[] msList = new char[] {'0', '0', '0'}; |
| 238 | + |
| 239 | + for (millis = 0; millis < 3; ++millis) { |
| 240 | + if (millis < ms.length()) { |
| 241 | + msList[millis] = ms.charAt(millis); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + millis = Integer.valueOf(String.valueOf(msList)); |
| 246 | + if (millis < 999) { |
| 247 | + d.setTime(d.getTime() + (long) millis); |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + return d; |
| 257 | + } |
| 258 | + |
| 259 | + public String toString() { |
| 260 | + Calendar c = Calendar.getInstance((TimeZone) LOCAL_TIMEZONE.get()); |
| 261 | + c.setTime(this); |
| 262 | + int year = c.get(1); |
| 263 | + int month = c.get(2) + 1; |
| 264 | + int day = c.get(5); |
| 265 | + int hour = c.get(11); |
| 266 | + int minute = c.get(12); |
| 267 | + int second = c.get(13); |
| 268 | + char[] buf; |
| 269 | + if ((hour != 0 || minute != 0 || second != 0) && dateShowTime) { |
| 270 | + buf = "2000-00-00 00:00:00".toCharArray(); |
| 271 | + buf[0] = Character.forDigit(year / 1000, 10); |
| 272 | + buf[1] = Character.forDigit(year / 100 % 10, 10); |
| 273 | + buf[2] = Character.forDigit(year / 10 % 10, 10); |
| 274 | + buf[3] = Character.forDigit(year % 10, 10); |
| 275 | + buf[5] = Character.forDigit(month / 10, 10); |
| 276 | + buf[6] = Character.forDigit(month % 10, 10); |
| 277 | + buf[8] = Character.forDigit(day / 10, 10); |
| 278 | + buf[9] = Character.forDigit(day % 10, 10); |
| 279 | + buf[11] = Character.forDigit(hour / 10, 10); |
| 280 | + buf[12] = Character.forDigit(hour % 10, 10); |
| 281 | + buf[14] = Character.forDigit(minute / 10, 10); |
| 282 | + buf[15] = Character.forDigit(minute % 10, 10); |
| 283 | + buf[17] = Character.forDigit(second / 10, 10); |
| 284 | + buf[18] = Character.forDigit(second % 10, 10); |
| 285 | + return new String(buf); |
| 286 | + } else { |
| 287 | + buf = "2000-00-00".toCharArray(); |
| 288 | + buf[0] = Character.forDigit(year / 1000, 10); |
| 289 | + buf[1] = Character.forDigit(year / 100 % 10, 10); |
| 290 | + buf[2] = Character.forDigit(year / 10 % 10, 10); |
| 291 | + buf[3] = Character.forDigit(year % 10, 10); |
| 292 | + buf[5] = Character.forDigit(month / 10, 10); |
| 293 | + buf[6] = Character.forDigit(month % 10, 10); |
| 294 | + buf[8] = Character.forDigit(day / 10, 10); |
| 295 | + buf[9] = Character.forDigit(day % 10, 10); |
| 296 | + return new String(buf); |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + public int getHours() { |
| 301 | + return this.privateDate.getHours(); |
| 302 | + } |
| 303 | + |
| 304 | + public int getMinutes() { |
| 305 | + return this.privateDate.getMinutes(); |
| 306 | + } |
| 307 | + |
| 308 | + public int getSeconds() { |
| 309 | + return this.privateDate.getSeconds(); |
| 310 | + } |
| 311 | + |
| 312 | + public void setHours(int i) { |
| 313 | + this.privateDate.setHours(i); |
| 314 | + this.setTime(this.privateDate.getTime()); |
| 315 | + } |
| 316 | + |
| 317 | + public void setMinutes(int i) { |
| 318 | + this.privateDate.setMinutes(i); |
| 319 | + this.setTime(this.privateDate.getTime()); |
| 320 | + } |
| 321 | + |
| 322 | + public void setSeconds(int i) { |
| 323 | + this.privateDate.setSeconds(i); |
| 324 | + this.setTime(this.privateDate.getTime()); |
| 325 | + } |
| 326 | + |
| 327 | + public static void main(String[] args) { |
| 328 | + System.out.println((new java.util.Date()).getYear()); |
| 329 | + new HiveDate(System.currentTimeMillis()); |
| 330 | + System.out.println(valueOf("2015-01-03 16:12:11")); |
| 331 | + System.out.println(valueOf("2015-01-03")); |
| 332 | + } |
| 333 | +} |
0 commit comments