OSDN Git Service

69a3f35300c304ec650d8b0d05c2afe5c6e2d6f3
[coroid/inqubus.git] / frontend / src / saccubus / util / WayBackTimeParser.java
1 package saccubus.util;
2
3 import java.io.IOException;
4 import java.text.DateFormat;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
10
11 /**
12  * 過去ログ取得時に指定する文字列(日付)をパースするユーティリティクラスです.
13  * @author yuki
14  */
15 public final class WayBackTimeParser {
16
17     private static final Pattern PATTERN_NUMBER = Pattern.compile("^\\d+$");
18     private static final Pattern PATTERN_YYMMDD_HH_MM_SS = Pattern.compile(
19             "^(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)$");
20     private static final Pattern PATTERN_YYMMDD_HH_MM = Pattern.compile(
21             "^(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)$");
22
23     private WayBackTimeParser() {
24     }
25
26     /**
27      * 過去の時点を指定する文字列から、コメント取得フォーマットの"when"に指定する文字列へ変換します.
28      * この文字列は、1970 年 1 月 1 日 00:00:00 GMT からの秒数(一般的なミリ秒ではない)になります.
29      * @param time 時点指定文字列。"YYYY/MM/DD hh:mm:ss", "YYYY/MM/DD hh:mm", 秒数を表す数値,
30      * ただし年月日時分(秒)の区切りは実際には数字以外のどんな文字でも構いません.
31      * のいずれかであればパース可能です.
32      * @return パース結果.
33      * @throws IOException パース失敗.
34      */
35     public static long parse(String time) throws IOException {
36         final Matcher mNumber = PATTERN_NUMBER.matcher(time);
37         if (mNumber.matches()) {
38             return Long.parseLong(time);
39         }
40
41         final Matcher mHMS = PATTERN_YYMMDD_HH_MM_SS.matcher(time);
42         if (mHMS.matches()) {
43             try {
44                 final DateFormat fmt = new SimpleDateFormat("yyyy:MM:dd:HH:mm:ss:");
45                 StringBuilder str = new StringBuilder();
46                 for (int i = 1; i <= 6; i++) {
47                     str.append(mHMS.group(i));
48                     str.append(":");
49                 }
50                 final Date date = fmt.parse(str.toString());
51                 return date.getTime() / 1000;
52             } catch (ParseException ex) {
53                 throw new IOException("Cannot parse wayback time: " + time, ex);
54             }
55         }
56
57         final Matcher mHM = PATTERN_YYMMDD_HH_MM.matcher(time);
58         if (mHM.matches()) {
59             try {
60                 final DateFormat fmt = new SimpleDateFormat("yyyy:MM:dd:HH:mm:");
61                 StringBuilder str = new StringBuilder();
62                 for (int i = 1; i <= 5; i++) {
63                     str.append(mHM.group(i));
64                     str.append(":");
65                 }
66                 final Date date = fmt.parse(str.toString());
67                 return date.getTime() / 1000;
68             } catch (ParseException ex) {
69                 throw new IOException("Cannot parse wayback time: " + time, ex);
70             }
71         }
72
73         throw new IOException("Cannot parse wayback time: " + time);
74     }
75 }