OSDN Git Service

過去ログ取得の日時指定で年月日だけでもOKなように機能追加
authoryukihane <yukihane.feather@gmail.com>
Fri, 19 Aug 2011 01:06:33 +0000 (10:06 +0900)
committeryukihane <yukihane.feather@gmail.com>
Fri, 19 Aug 2011 01:06:33 +0000 (10:06 +0900)
frontend/src/saccubus/util/WayBackTimeParser.java
frontend/test/saccubus/util/WayBackTimeParserTest.java

index 69a3f35..4927300 100644 (file)
@@ -19,6 +19,8 @@ public final class WayBackTimeParser {
             "^(\\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 static final Pattern PATTERN_YYMMDD = Pattern.compile(
+            "^(\\d+)\\D+(\\d+)\\D+(\\d+)$");
 
     private WayBackTimeParser() {
     }
@@ -70,6 +72,23 @@ public final class WayBackTimeParser {
             }
         }
 
+        final Matcher mYMD = PATTERN_YYMMDD.matcher(time);
+        if (mYMD.matches()) {
+            try {
+                final DateFormat fmt = new SimpleDateFormat("yyyy:MM:dd:");
+                StringBuilder str = new StringBuilder();
+                for (int i = 1; i <= 3; i++) {
+                    str.append(mYMD.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);
+            }
+        }
+
+
         throw new IOException("Cannot parse wayback time: " + time);
     }
 }
index 5d64ec5..2f493da 100644 (file)
@@ -28,6 +28,15 @@ public class WayBackTimeParserTest {
         assertEquals(expected, actual);
     }
 
+    /** 年月日指定 */
+    @Test
+    public void testParseYYYYMMDD() throws IOException {
+        final String text = "2011/08/19";
+        final long expected = 1313679600L;
+        final long actual = WayBackTimeParser.parse(text);
+        assertEquals(expected, actual);
+    }
+
     /** 1970 年 1 月 1 日 00:00:00 GMT からの秒数指定(一般的なミリ秒指定ではないことに注意 */
     @Test
     public void testParseNumber() throws IOException {