MDateAndTime { public static StringType parseDateOrTime(String st, long[] dt) { assert dt.length >= MAX_INDEX; st = st.trim(); String year = "0"; String month = "0"; String day = "0"; String hour = "0"; String minute = "0"; String seconds = "0"; Matcher matcher; if((matcher = DATE_PATTERN.matcher(st)).matches()) { StringType type = StringType.DATE_ST; year = matcher.group(DATE_YEAR_GROUP); month = matcher.group(DATE_MONTH_GROUP); day = matcher.group(DATE_DAY_GROUP); if(matcher.group(TIME_GROUP) != null) { type = StringType.DATETIME_ST; hour = matcher.group(TIME_HOUR_GROUP); minute = matcher.group(TIME_MINUTE_GROUP); seconds = matcher.group(TIME_SECOND_GROUP); } if(stringsToLongs(dt, true, year, month, day, hour, minute, seconds) && isValidDateTime_Zeros(dt)) { return type; } return (type == StringType.DATETIME_ST) ? StringType.INVALID_DATETIME_ST : StringType.INVALID_DATE_ST; } else if((matcher = TIME_WITH_DAY_PATTERN.matcher(st)).matches()) { day = matcher.group(MDateAndTime.TIME_WITH_DAY_DAY_GROUP); hour = matcher.group(MDateAndTime.TIME_WITH_DAY_HOUR_GROUP); minute = matcher.group(MDateAndTime.TIME_WITH_DAY_MIN_GROUP); seconds = matcher.group(MDateAndTime.TIME_WITH_DAY_SEC_GROUP); if(stringsToLongs(dt, false, year, month, day, hour, minute, seconds) && isValidHrMinSec(dt, false, false)) { int sign = 1; if(dt[DAY_INDEX] < 0) { dt[DAY_INDEX] *= (sign = -1); } dt[HOUR_INDEX] = sign * (dt[HOUR_INDEX] += dt[DAY_INDEX] * 24); dt[DAY_INDEX] = 0; return StringType.TIME_ST; } return StringType.INVALID_TIME_ST; } else if((matcher = TIME_WITHOUT_DAY_PATTERN.matcher(st)).matches()) { hour = matcher.group(MDateAndTime.TIME_WITHOUT_DAY_HOUR_GROUP); minute = matcher.group(MDateAndTime.TIME_WITHOUT_DAY_MIN_GROUP); seconds = matcher.group(MDateAndTime.TIME_WITHOUT_DAY_SEC_GROUP); if(stringsToLongs(dt, false, year, month, day, hour, minute, seconds) && isValidHrMinSec(dt, false, false)) { return StringType.TIME_ST; } return StringType.INVALID_TIME_ST; } else { String[] parts = st.split("\\s++"); if(parts.length == 2) { String[] dTok = parts[0].split(DELIM); String[] tTok = parts[1].split(DELIM); if((dTok.length == 3) && (tTok.length == 3)) { if(stringsToLongs(dt, true, dTok[0], dTok[1], dTok[2], tTok[0], tTok[1], tTok[2]) && isValidDateTime_Zeros(dt)) { return StringType.DATETIME_ST; } return StringType.INVALID_DATETIME_ST; } } else if(parts.length == 1) { String[] dTok = parts[0].split(DELIM); if(dTok.length == 3) { if(stringsToLongs(dt, true, dTok[0], dTok[1], dTok[2]) && isValidDateTime_Zeros(dt)) { return StringType.DATE_ST; } return StringType.INVALID_DATE_ST; } } } return StringType.UNPARSABLE; }  static String getMonthName(int numericRep, String locale, TExecutionContext context); static long[] fromJodaDateTime(AbstractDateTime date); static MutableDateTime toJodaDateTime(long[] dt, String tz); static MutableDateTime toJodaDateTime(long[] dt, DateTimeZone dtz); static String dateToString(int encodedDate); static int parseAndEncodeDate(String input); static int encodeDate(long millis, String tz); static int encodeDate(long[] dt); static int encodeDate(long y, long m, long d); static long[] decodeDate(long encodedDate); static long[] parseDate(long val); static long[] parseDateTime(long val); static String dateTimeToString(long encodedDateTime); static String dateTimeToString(long[] dt); static StringType parseDateOrTime(String st, long[] dt); static long parseAndEncodeDateTime(String input); static long encodeDateTime(long millis, String tz); static long encodeDateTime(BaseDateTime dt); static long encodeDateTime(long[] dt); static long encodeDateTime(long year, long month, long day, long hour, long min, long sec); static long[] decodeDateTime(long encodedDateTime); static String timeToString(int encodedTime); static String timeToString(long[] dt); static String timeToString(long h, long m, long s); static void timeToDatetime(long[] dt); static int parseTime(String string, TExecutionContext context); static long[] decodeTime(long encodedTime); static int encodeTime(long millis, String tz); static int encodeTime(long[] dt, TExecutionContext context); static int encodeTime(long h, long m, long s, TExecutionContext context); static int parseAndEncodeTimestamp(String input, String tz, TExecutionContext context); static long[] decodeTimestamp(long encodedTimestamp, String tz); static int encodeTimestamp(long[] dt, String tz, TExecutionContext context); static int encodeTimestamp(BaseDateTime dateTime, TExecutionContext context); static int encodeTimestamp(long millis, TExecutionContext context); static boolean isValidTimestamp(BaseDateTime dt); static int getTimestamp(long[] dt, String tz); static String timestampToString(long encodedTimestamp, String tz); static DateTimeZone parseTimeZone(String tz); static boolean isValidDateTime_Zeros(long[] dt); static boolean isValidDateTime(long[] dt, ZeroFlag... flags); static boolean isHrMinSecNegative(long[] dt); static boolean isHrMinSecNegative(long h, long m, long s); static boolean isValidHrMinSec(long[] dt, boolean checkHour, boolean isFromDateTime); static boolean isValidHrMinSec(long h, long m, long s, boolean checkHour, boolean isFromDateTime); static boolean isZeroDayMonth(long[] dt); static boolean isValidDate_Zeros(long[] dt); static boolean isValidDate_NoZeros(long[] dt); static boolean isValidDate(long[] dt, ZeroFlag... flags); static boolean isValidDate(long y, long m, long d, ZeroFlag... flags); static long getLastDay(long[] dt); static long getLastDay(long year, long month); static boolean isValidType(StringType type); static final NoAttrTClass DATE; static final NoAttrTClass DATETIME; static final NoAttrTClass TIME; static final NoAttrTClass YEAR; static final NoAttrTClass TIMESTAMP; static final Map<String, String[]> MONTHS; static final int YEAR_INDEX; static final int MONTH_INDEX; static final int DAY_INDEX; static final int HOUR_INDEX; static final int MIN_INDEX; static final int SEC_INDEX; static final int MAX_INDEX; static final long TIMESTAMP_MIN; static final long TIMESTAMP_MAX; static final long TS_ERROR_VALUE; static final int TIME_MAX; static final int TIME_MIN; }
@Test public void testParseDateOrTime() { doParseDateOrTime(TIME_ST, "1 1:1:0", 0, 0, 0, 25, 1, 0); doParseDateOrTime(TIME_ST, "-12:0:2", 0, 0, 0, -12, 0, 2); doParseDateOrTime(TIME_ST, "-1 3:4:5", 0, 0, 0, -27, 4, 5); doParseDateOrTime(TIME_ST, "12:30:10", 0, 0, 0, 12, 30, 10); doParseDateOrTime(TIME_ST, "25:30:10", 0, 0, 0, 25, 30, 10); doParseDateOrTime(TIME_ST, "25:30:10-0400", 0, 0, 0, 25, 30, 10); doParseDateOrTime(TIME_ST, "25:30:10-04:00", 0, 0, 0, 25, 30, 10); doParseDateOrTime(TIME_ST, "25:30:10-040000", 0, 0, 0, 25, 30, 10); doParseDateOrTime(TIME_ST, "25:30:10-04:00:00", 0, 0, 0, 25, 30, 10); doParseDateOrTime(DATE_ST, "2002-12-30", 2002, 12, 30, 0, 0, 0); doParseDateOrTime(DATE_ST, "2002/12/30", 2002, 12, 30, 0, 0, 0); doParseDateOrTime(DATE_ST, "02-12-30", 2002, 12, 30, 0, 0, 0); doParseDateOrTime(DATE_ST, "02/12/30", 2002, 12, 30, 0, 0, 0); doParseDateOrTime(DATE_ST, "95-12-30", 1995, 12, 30, 0, 0, 0); doParseDateOrTime(DATE_ST, "95/12/30", 1995, 12, 30, 0, 0, 0); doParseDateOrTime(DATE_ST, "2014-08-27-0400", 2014, 8, 27, 0, 0, 0); doParseDateOrTime(DATE_ST, "2014-08-27-04:00", 2014, 8, 27, 0, 0, 0); doParseDateOrTime(DATE_ST, "2014-08-27-040000", 2014, 8, 27, 0, 0, 0); doParseDateOrTime(DATE_ST, "2014-08-27-04:00:00", 2014, 8, 27, 0, 0, 0); doParseDateOrTime(DATE_ST, "0000-00-00", 0, 0, 0, 0, 0, 0); doParseDateOrTime(DATE_ST, "0000-00-01", 0, 0, 1, 0, 0, 0); doParseDateOrTime(DATE_ST, "0000-01-00", 0, 1, 0, 0, 0, 0); doParseDateOrTime(DATE_ST, "0000-01-01", 0, 1, 1, 0, 0, 0); doParseDateOrTime(DATE_ST, "0001-00-00", 1, 0, 0, 0, 0, 0); doParseDateOrTime(DATE_ST, "0001-00-01", 1, 0, 1, 0, 0, 0); doParseDateOrTime(DATE_ST, "0001-01-00", 1, 1, 0, 0, 0, 0); doParseDateOrTime(DATE_ST, "0001-01-01", 1, 1, 1, 0, 0, 0); doParseDateOrTime(DATETIME_ST, "1900-1-2 12:30:10", 1900, 1, 2, 12, 30, 10); doParseDateOrTime(DATETIME_ST, "2012-04-09T12:45:00", 2012, 4, 9, 12, 45, 0); doParseDateOrTime(DATETIME_ST, "2013-04-03T14:55:08.249Z-05:00", 2013, 4, 3, 14, 55, 8); doParseDateOrTime(DATETIME_ST, "2013/04/03 14.55.08", 2013, 4, 3, 14, 55, 8); doParseDateOrTime(DATETIME_ST, "2014-05-21 16:00:35.091000-0400", 2014, 5, 21, 16, 0, 35); doParseDateOrTime(DATETIME_ST, "2014-05-21 16:00:35.091000-04:00", 2014, 5, 21, 16, 0, 35); doParseDateOrTime(DATETIME_ST, "2014-05-21 16:00:35.091000-040000", 2014, 5, 21, 16, 0, 35); doParseDateOrTime(DATETIME_ST, "2014-05-21 16:00:35.091000-04:00:00", 2014, 5, 21, 16, 0, 35); doParseDateOrTime(INVALID_TIME_ST, "", 0, 0, 0, 0, 0, 0); doParseDateOrTime(INVALID_DATE_ST, "01-01-02am", 2001, 1, 0, 0, 0, 0); doParseDateOrTime(INVALID_DATETIME_ST, "1900-1-2 25:30:10", 1900, 1, 2, 25, 30, 10); doParseDateOrTime(UNPARSABLE, "01:02:03:04", 0, 0, 0, 0, 0, 0); doParseDateOrTime(UNPARSABLE, "2012-04-09TT12:45:00", 0, 0, 0, 0, 0, 0); }