import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
- *
+ * 過去ログ取得時に指定する文字列(日付)をパースするユーティリティクラスです.
* @author yuki
*/
public final class WayBackTimeParser {
+ private static final Pattern PATTERN_NUMBER = Pattern.compile("^\\d+$");
+ private static final Pattern PATTERN_YYMMDD_HH_MM_SS = Pattern.compile(
+ "^(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)$");
+ private static final Pattern PATTERN_YYMMDD_HH_MM = Pattern.compile(
+ "^(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)$");
+
private WayBackTimeParser() {
}
- public static String parse(String time) throws IOException {
- Date date = null;
- String waybacktime = "0";
- try {
- final DateFormat fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
- date = fmt.parse(time);
- } catch (ParseException ex2) {
- date = null;
+ /**
+ * 過去の時点を指定する文字列から、コメント取得フォーマットの"when"に指定する文字列へ変換します.
+ * この文字列は、1970 年 1 月 1 日 00:00:00 GMT からの秒数(一般的なミリ秒ではない)になります.
+ * @param time 時点指定文字列。"YYYY/MM/DD hh:mm:ss", "YYYY/MM/DD hh:mm", 秒数を表す数値,
+ * ただし年月日時分(秒)の区切りは実際には数字以外のどんな文字でも構いません.
+ * のいずれかであればパース可能です.
+ * @return パース結果.
+ * @throws IOException パース失敗.
+ */
+ public static long parse(String time) throws IOException {
+ final Matcher mNumber = PATTERN_NUMBER.matcher(time);
+ if (mNumber.matches()) {
+ return Long.parseLong(time);
}
- if (date == null) {
+
+ final Matcher mHMS = PATTERN_YYMMDD_HH_MM_SS.matcher(time);
+ if (mHMS.matches()) {
try {
- final DateFormat fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm");
- date = fmt.parse(time);
- } catch (ParseException ex3) {
- date = null;
+ final DateFormat fmt = new SimpleDateFormat("yyyy:MM:dd:HH:mm:ss:");
+ StringBuilder str = new StringBuilder();
+ for (int i = 1; i <= 6; i++) {
+ str.append(mHMS.group(i));
+ str.append(":");
+ }
+ final Date date = fmt.parse(str.toString());
+ return date.getTime() / 1000;
+ } catch (ParseException ex) {
+ throw new IOException("Cannot parse wayback time: " + time, ex);
}
}
- if (date != null) {
- waybacktime = Long.toString(date.getTime() / 1000);
- System.out.println("ok.(" + date.toString() + "):" + waybacktime);
- } else {
+
+ final Matcher mHM = PATTERN_YYMMDD_HH_MM.matcher(time);
+ if (mHM.matches()) {
try {
- long tmp_time = Long.parseLong(time);
- waybacktime = Long.toString(tmp_time);
- date = new Date(tmp_time * 1000);
- System.out.println("ok.(" + date.toString() + "):"
- + waybacktime);
- } catch (NumberFormatException ex4) {
- System.out.println("ng.");
- System.out.println("Cannot parse wayback time.");
- throw new IOException("Cannot parse wayback time.", ex4);
+ final DateFormat fmt = new SimpleDateFormat("yyyy:MM:dd:HH:mm:");
+ StringBuilder str = new StringBuilder();
+ for (int i = 1; i <= 5; i++) {
+ str.append(mHM.group(i));
+ str.append(":");
+ }
+ final Date date = fmt.parse(str.toString());
+ return date.getTime() / 1000;
+ } catch (ParseException ex) {
+ throw new IOException("Cannot parse wayback time: " + time, ex);
}
}
- return waybacktime;
+
+ throw new IOException("Cannot parse wayback time: " + time);
}
}