Force a data object to a DateTime, if possible. Only CharArray, ByteArray can be forced to a DateTime. Numeric types and complex types cannot be forced to a DateTime. This isn't particularly efficient, so if you already know that the object you have is a DateTime you should just cast it. @pa
(Object o, byte type)
| 1093 | * if the type can't be forced to a Boolean. |
| 1094 | */ |
| 1095 | public static DateTime toDateTime(Object o, byte type) throws ExecException { |
| 1096 | try { |
| 1097 | switch (type) { |
| 1098 | case NULL: |
| 1099 | return null; |
| 1100 | case BYTEARRAY: |
| 1101 | return new DateTime(((DataByteArray) o).toString()); |
| 1102 | case CHARARRAY: |
| 1103 | // the string can contain just date part or date part plus time part |
| 1104 | return ToDate.extractDateTime((String) o); |
| 1105 | case INTEGER: |
| 1106 | return new DateTime(((Integer) o).longValue()); |
| 1107 | case LONG: |
| 1108 | return new DateTime(((Long) o).longValue()); |
| 1109 | case FLOAT: |
| 1110 | return new DateTime(((Float) o).longValue()); |
| 1111 | case DOUBLE: |
| 1112 | return new DateTime(((Double) o).longValue()); |
| 1113 | case BIGINTEGER: |
| 1114 | return new DateTime(((BigInteger) o).longValue()); |
| 1115 | case BIGDECIMAL: |
| 1116 | return new DateTime(((BigDecimal) o).longValue()); |
| 1117 | case DATETIME: |
| 1118 | return (DateTime) o; |
| 1119 | |
| 1120 | case BOOLEAN: |
| 1121 | case BYTE: |
| 1122 | case MAP: |
| 1123 | case INTERNALMAP: |
| 1124 | case TUPLE: |
| 1125 | case BAG: |
| 1126 | case UNKNOWN: |
| 1127 | default: |
| 1128 | int errCode = 1071; |
| 1129 | String msg = "Cannot convert a " + findTypeName(o) + " to a DateTime"; |
| 1130 | throw new ExecException(msg, errCode, PigException.INPUT); |
| 1131 | } |
| 1132 | } catch (ClassCastException cce) { |
| 1133 | throw cce; |
| 1134 | } catch (ExecException ee) { |
| 1135 | throw ee; |
| 1136 | } catch (NumberFormatException nfe) { |
| 1137 | int errCode = 1074; |
| 1138 | String msg = "Problem with formatting. Could not convert " + o + " to DateTime."; |
| 1139 | throw new ExecException(msg, errCode, PigException.INPUT, nfe); |
| 1140 | } catch (Exception e) { |
| 1141 | int errCode = 2054; |
| 1142 | String msg = "Internal error. Could not convert " + o + " to DateTime."; |
| 1143 | throw new ExecException(msg, errCode, PigException.BUG); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | public static DateTime toDateTime(Object o) throws ExecException { |
| 1148 | return toDateTime(o, findType(o)); |