From: <bra...@us...> - 2008-07-15 01:37:32
|
Revision: 2448 http://archive-access.svn.sourceforge.net/archive-access/?rev=2448&view=rev Author: bradtofel Date: 2008-07-14 18:37:42 -0700 (Mon, 14 Jul 2008) Log Message: ----------- REFACTOR: Timestamp package change: org.archive.wayback.core => org.archive.wayback.util Modified Paths: -------------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ExternalExcluder.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionRule.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionServlet.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathDatePrefixQueryRequestParser.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathDateRangeQueryRequestParser.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathPrefixDatePrefixQueryRequestParser.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathPrefixDateRangeQueryRequestParser.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/ReplayRequestParser.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/BDBMap.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/CaptureSearchResults.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/SearchResult.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/WaybackRequest.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/domainprefix/DomainPrefixReplayRenderer.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/domainprefix/DomainPrefixRequestParser.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/liveweb/LiveWebCache.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/query/resultspartitioner/ResultsPartitioner.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/query/resultspartitioner/ResultsPartitionsFactory.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/query/resultspartitioner/ResultsTimelinePartitionsFactory.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/requestparser/BaseRequestParser.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/requestparser/FormRequestParser.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/LocalResourceIndex.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/NutchResourceIndex.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/filters/DateRangeFilter.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/filters/EndDateFilter.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/filters/StartDateFilter.java Added Paths: ----------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/Timestamp.java Removed Paths: ------------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/Timestamp.java Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ExternalExcluder.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ExternalExcluder.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ExternalExcluder.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -25,8 +25,8 @@ package org.archive.wayback.accesscontrol; import org.archive.wayback.core.CaptureSearchResult; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.util.ObjectFilter; +import org.archive.wayback.util.Timestamp; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionRule.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionRule.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionRule.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -29,7 +29,7 @@ import java.net.URLEncoder; import java.text.ParseException; -import org.archive.wayback.core.Timestamp; +import org.archive.wayback.util.Timestamp; /** * Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionServlet.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionServlet.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionServlet.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -36,8 +36,8 @@ import javax.servlet.http.HttpServletResponse; import org.apache.commons.httpclient.URIException; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.surt.SURTTokenizer; +import org.archive.wayback.util.Timestamp; import org.archive.wayback.webapp.ServletRequestContext; import com.sleepycat.je.DatabaseException; Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathDatePrefixQueryRequestParser.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathDatePrefixQueryRequestParser.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathDatePrefixQueryRequestParser.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -27,9 +27,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; import org.archive.wayback.requestparser.PathRequestParser; +import org.archive.wayback.util.Timestamp; /** * RequestParser implementation that extracts request info from an Archival Url Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathDateRangeQueryRequestParser.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathDateRangeQueryRequestParser.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathDateRangeQueryRequestParser.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -27,9 +27,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; import org.archive.wayback.requestparser.PathRequestParser; +import org.archive.wayback.util.Timestamp; /** * RequestParser implementation that extracts request info from an Archival Url Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathPrefixDatePrefixQueryRequestParser.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathPrefixDatePrefixQueryRequestParser.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathPrefixDatePrefixQueryRequestParser.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -27,9 +27,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; import org.archive.wayback.requestparser.PathRequestParser; +import org.archive.wayback.util.Timestamp; /** * RequestParser implementation that extracts request info from an Archival Url Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathPrefixDateRangeQueryRequestParser.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathPrefixDateRangeQueryRequestParser.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/PathPrefixDateRangeQueryRequestParser.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -27,9 +27,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; import org.archive.wayback.requestparser.PathRequestParser; +import org.archive.wayback.util.Timestamp; /** * RequestParser implementation that extracts request info from an Archival Url Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/ReplayRequestParser.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/ReplayRequestParser.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/archivalurl/requestparser/ReplayRequestParser.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -27,9 +27,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; import org.archive.wayback.requestparser.PathRequestParser; +import org.archive.wayback.util.Timestamp; /** * RequestParser implementation that extracts request info from a Replay Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/BDBMap.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/BDBMap.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/BDBMap.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -23,6 +23,8 @@ import java.io.UnsupportedEncodingException; import java.util.Properties; +import org.archive.wayback.util.Timestamp; + import com.sleepycat.je.Database; import com.sleepycat.je.DatabaseConfig; import com.sleepycat.je.DatabaseEntry; Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/CaptureSearchResults.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/CaptureSearchResults.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/CaptureSearchResults.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -29,6 +29,7 @@ import java.util.Iterator; import org.archive.wayback.exception.AnchorWindowTooSmallException; +import org.archive.wayback.util.Timestamp; /** * Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/SearchResult.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/SearchResult.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/SearchResult.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -28,6 +28,8 @@ import java.util.HashMap; import java.util.Map; +import org.archive.wayback.util.Timestamp; + /** * * Deleted: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/Timestamp.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/Timestamp.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/Timestamp.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -1,459 +0,0 @@ -/* Timestamp - * - * Created on 2005/10/18 14:00:00 - * - * Copyright (C) 2005 Internet Archive. - * - * This file is part of the Wayback Machine (crawler.archive.org). - * - * Wayback Machine is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or - * any later version. - * - * Wayback Machine is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser Public License for more details. - * - * You should have received a copy of the GNU Lesser Public License - * along with Wayback Machine; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -package org.archive.wayback.core; - -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.SimpleTimeZone; -import java.util.TimeZone; - - -/** - * Represents a moment in time as a 14-digit string, and interally as a Date. - * - * @author Brad Tofel - * @version $Date$, $Revision$ - */ -public class Timestamp { - - private final static String LOWER_TIMESTAMP_LIMIT = "10000000000000"; - private final static String UPPER_TIMESTAMP_LIMIT = "29991939295959"; - private final static String YEAR_LOWER_LIMIT = "1996"; - private final static String YEAR_UPPER_LIMIT = - String.valueOf(Calendar.getInstance().get(Calendar.YEAR)); - private final static String MONTH_LOWER_LIMIT = "01"; - private final static String MONTH_UPPER_LIMIT = "12"; - private final static String DAY_LOWER_LIMIT = "01"; - private final static String HOUR_UPPER_LIMIT = "23"; - private final static String HOUR_LOWER_LIMIT = "00"; - private final static String MINUTE_UPPER_LIMIT = "59"; - private final static String MINUTE_LOWER_LIMIT = "00"; - private final static String SECOND_UPPER_LIMIT = "59"; - private final static String SECOND_LOWER_LIMIT = "00"; - - private final static int SSE_1996 = 820454400; - - private final static String[] months = { "Jan", "Feb", "Mar", "Apr", "May", - "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - - private String dateStr = null; - private Date date = null; - - /** - * Constructor - */ - public Timestamp() { - super(); - } - - /** - * Construct and initialize structure from a 14-digit String timestamp. If - * the argument is too short, or specifies an invalid timestamp, cleanup - * will be attempted to create the earliest legal timestamp given the input. - * @param dateStr - */ - public Timestamp(final String dateStr) { - super(); - - Calendar cal = dateStrToCalendar(dateStr); - setDate(cal.getTime()); - } - - /** - * Construct and initialize structure from an integer number of seconds - * since the epoch. - * @param sse - */ - public Timestamp(final int sse) { - super(); - setSse(sse); - } - - /** - * Construct and initialize structure from an Date - * @param date - */ - public Timestamp(final Date date) { - super(); - setDate(date); - } - - /** - * set internal structure using Date argument - * @param date - */ - public void setDate(final Date date) { - this.date = (Date) date.clone(); - Calendar cal = getCalendar(); - cal.setTime(this.date); - dateStr = calendarToDateStr(cal); - } - - - /** - * @return Date for this Timestamp - */ - public Date getDate() { - return date; - } - - /** - * set internal structure using seconds since the epoch integer argument - * @param sse - */ - public void setSse(final int sse) { - setDate(new Date(((long)sse) * 1000)); - } - - /** - * initialize interal data structures for this Timestamp from the 14-digit - * argument. Will clean up timestamp as needed to yield the ealiest - * possible timestamp given the possible partial or wrong argument. - * - * @param dateStr - */ - public void setDateStr(String dateStr) { - Calendar cal = dateStrToCalendar(dateStr); - setDate(cal.getTime()); - } - - /** - * @return the 14-digit String representation of this Timestamp. - */ - - public String getDateStr() { - return dateStr; - } - - /** - * @return the integer number of seconds since epoch represented by this - * Timestamp. - */ - public int sse() { - return Math.round(date.getTime() / 1000); - } - - /** - * function that calculates integer seconds between this records - * timeStamp and the arguments timeStamp. result is the absolute number of - * seconds difference. - * - * @param otherTimeStamp - * @return int absolute seconds between the argument and this records - * timestamp. - */ - public int absDistanceFromTimestamp(final Timestamp otherTimeStamp) { - return Math.abs(distanceFromTimestamp(otherTimeStamp)); - } - - /** - * function that calculates integer seconds between this records - * timeStamp and the arguments timeStamp. result is negative if this records - * timeStamp is less than the argument, positive if it is greater, and 0 if - * the same. - * - * @param otherTimeStamp - * @return int milliseconds - */ - public int distanceFromTimestamp(final Timestamp otherTimeStamp) { - return otherTimeStamp.sse() - sse(); - } - - /** - * @return the year portion(first 4 digits) of this Timestamp - */ - public String getYear() { - return this.dateStr.substring(0, 4); - } - - /** - * @return the month portion(digits 5-6) of this Timestamp - */ - public String getMonth() { - return this.dateStr.substring(4, 6); - } - - /** - * @return the day portion(digits 7-8) of this Timestamp - */ - public String getDay() { - return this.dateStr.substring(6, 8); - } - - /** - * @return user friendly String representation of the date of this - * Timestamp. eg: "Jan 13, 1999" - */ - public String prettyDate() { - String year = dateStr.substring(0, 4); - String month = dateStr.substring(4, 6); - String day = dateStr.substring(6, 8); - int monthInt = Integer.parseInt(month) - 1; - String prettyMonth = "UNK"; - if ((monthInt >= 0) && (monthInt < months.length)) { - prettyMonth = months[monthInt]; - } - return prettyMonth + " " + day + ", " + year; - } - - /** - * @return user friendly String representation of the Time of this - * Timestamp. - */ - public String prettyTime() { - return dateStr.substring(8, 10) + ":" + dateStr.substring(10, 12) + ":" - + dateStr.substring(12, 14); - } - - /** - * @return user friendly String representation of the Date and Time of this - * Timestamp. - */ - public String prettyDateTime() { - return prettyDate() + " " + prettyTime(); - } - - /* - * - * ALL STATIC METHOD BELOW HERE: - * ============================= - * - */ - - private static String frontZeroPad(final String input, final int digits) { - int missing = digits - input.length(); - String padded = ""; - for(int i = 0; i < missing; i++) { - padded += "0"; - } - padded += input; - return padded; - } - private static String frontZeroPad(final int input, final int digits) { - return frontZeroPad(String.valueOf(input) ,digits); - } - - private static Calendar getCalendar() { - String[] ids = TimeZone.getAvailableIDs(0); - if (ids.length < 1) { - return null; - } - TimeZone gmt = new SimpleTimeZone(0, ids[0]); - return new GregorianCalendar(gmt); - } - - /** - * cleanup the dateStr argument assuming earliest values, and return a - * GMT calendar set to the time described by the dateStr. - * - * @param dateStr - * @return Calendar - */ - public static Calendar dateStrToCalendar(final String dateStr) { - - String paddedDateStr = padStartDateStr(dateStr); - - Calendar cal = getCalendar(); - int iYear = Integer.parseInt(paddedDateStr.substring(0,4)); - int iMonth = Integer.parseInt(paddedDateStr.substring(4,6)); - int iDay = Integer.parseInt(paddedDateStr.substring(6,8)); - int iHour = Integer.parseInt(paddedDateStr.substring(8,10)); - int iMinute = Integer.parseInt(paddedDateStr.substring(10,12)); - int iSecond = Integer.parseInt(paddedDateStr.substring(12,14)); - - cal.set(Calendar.YEAR,iYear); - cal.set(Calendar.MONTH,iMonth - 1); - cal.set(Calendar.DAY_OF_MONTH,iDay); - cal.set(Calendar.HOUR_OF_DAY,iHour); - cal.set(Calendar.MINUTE,iMinute); - cal.set(Calendar.SECOND,iSecond); - - return cal; - } - private static String calendarToDateStr(Calendar cal) { - return frontZeroPad(cal.get(Calendar.YEAR),4) + - frontZeroPad(cal.get(Calendar.MONTH) + 1 ,2) + - frontZeroPad(cal.get(Calendar.DAY_OF_MONTH),2) + - frontZeroPad(cal.get(Calendar.HOUR_OF_DAY),2) + - frontZeroPad(cal.get(Calendar.MINUTE),2) + - frontZeroPad(cal.get(Calendar.SECOND),2); - } - - - private static String padDigits(String input, String min, String max, - String missing) { - if(input == null) { - input = ""; - } - String finalDigits = ""; - for(int i = 0; i < missing.length(); i++) { - if(input.length() <= i) { - finalDigits = finalDigits + missing.charAt(i); - } else { - char inc = input.charAt(i); - char maxc = max.charAt(i); - char minc = min.charAt(i); - if(inc > maxc) { - inc = maxc; - } else if (inc < minc) { - inc = minc; - } - finalDigits = finalDigits + inc; - } - } - - return finalDigits; - } - - private static String boundDigits(String input, String min, String max) { - String bounded = input; - if(input.compareTo(min) < 0) { - bounded = min; - } else if(input.compareTo(max) > 0) { - bounded = max; - } - return bounded; - } - - // check each of YEAR, MONTH, DAY, HOUR, MINUTE, SECOND to make sure they - // are not too large or too small, factoring in the month, leap years, etc. - private static String boundTimestamp(String input) { - String boundTimestamp = ""; - if(input == null) { - input = ""; - } - // MAKE SURE THE YEAR IS WITHIN LEGAL BOUNDARIES: - Calendar tmpCal = getCalendar(); - tmpCal.setTime(new Date()); - - boundTimestamp = boundDigits(input.substring(0,4), - YEAR_LOWER_LIMIT,YEAR_UPPER_LIMIT); - - // MAKE SURE THE MONTH IS WITHIN LEGAL BOUNDARIES: - boundTimestamp += boundDigits(input.substring(4,6), - MONTH_LOWER_LIMIT,MONTH_UPPER_LIMIT); - - // NOW DEPENDING ON THE YEAR + MONTH, MAKE SURE THE DAY OF MONTH IS - // WITHIN LEGAL BOUNDARIES: - Calendar cal = getCalendar(); - cal.clear(); - int iYear = Integer.parseInt(boundTimestamp.substring(0,4)); - int iMonth = Integer.parseInt(boundTimestamp.substring(4,6)); - cal.set(Calendar.YEAR,iYear); - cal.set(Calendar.MONTH,iMonth - 1); - cal.set(Calendar.DAY_OF_MONTH,1); - - String maxDayOfMonth = String.valueOf(cal.getActualMaximum(Calendar.DAY_OF_MONTH)); - if(maxDayOfMonth.length() == 1) { - maxDayOfMonth = "0" + maxDayOfMonth; - } - boundTimestamp += boundDigits(input.substring(6,8), - DAY_LOWER_LIMIT,maxDayOfMonth); - - // MAKE SURE THE HOUR IS WITHIN LEGAL BOUNDARIES: - boundTimestamp += boundDigits(input.substring(8,10), - HOUR_LOWER_LIMIT,HOUR_UPPER_LIMIT); - - // MAKE SURE THE MINUTE IS WITHIN LEGAL BOUNDARIES: - boundTimestamp += boundDigits(input.substring(10,12), - MINUTE_LOWER_LIMIT,MINUTE_UPPER_LIMIT); - - // MAKE SURE THE SECOND IS WITHIN LEGAL BOUNDARIES: - boundTimestamp += boundDigits(input.substring(12,14), - SECOND_LOWER_LIMIT,SECOND_UPPER_LIMIT); - - return boundTimestamp; - } - - /** - * clean up timestamp argument assuming latest possible values for missing - * or bogus digits. - * @param timestamp String - * @return String - */ - public static String padEndDateStr(String timestamp) { - return boundTimestamp(padDigits(timestamp,LOWER_TIMESTAMP_LIMIT, - UPPER_TIMESTAMP_LIMIT,UPPER_TIMESTAMP_LIMIT)); - } - - /** - * clean up timestamp argument assuming earliest possible values for missing - * or bogus digits. - * @param timestamp String - * @return String - */ - public static String padStartDateStr(String timestamp) { - return boundTimestamp(padDigits(timestamp,LOWER_TIMESTAMP_LIMIT, - UPPER_TIMESTAMP_LIMIT,LOWER_TIMESTAMP_LIMIT)); - } - - /** - * @param dateStr - * @return Timestamp object representing the earliest date represented by - * the (possibly) partial digit-string argument. - */ - public static Timestamp parseBefore(final String dateStr) { - return new Timestamp(padStartDateStr(dateStr)); - } - - /** - * @param dateStr - * @return Timestamp object representing the latest date represented by the - * (possibly) partial digit-string argument. - */ - public static Timestamp parseAfter(final String dateStr) { - return new Timestamp(padEndDateStr(dateStr)); - } - - /** - * @param sse - * @return Timestamp object representing the seconds since epoch argument. - */ - public static Timestamp fromSse(final int sse) { - //String dateStr = ArchiveUtils.get14DigitDate(sse * 1000); - return new Timestamp(sse); - } - - /** - * @return Timestamp object representing the current date. - */ - public static Timestamp currentTimestamp() { - return new Timestamp(new Date()); - } - - /** - * @return Timestamp object representing the latest possible date. - */ - public static Timestamp latestTimestamp() { - return currentTimestamp(); - } - - /** - * @return Timestamp object representing the earliest possible date. - */ - public static Timestamp earliestTimestamp() { - return new Timestamp(SSE_1996); - } -} Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/WaybackRequest.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/WaybackRequest.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/WaybackRequest.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -38,6 +38,7 @@ import org.archive.wayback.requestparser.OpenSearchRequestParser; import org.archive.wayback.util.ObjectFilter; import org.archive.wayback.util.StringFormatter; +import org.archive.wayback.util.Timestamp; import org.archive.wayback.webapp.AccessPoint; /** Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/domainprefix/DomainPrefixReplayRenderer.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/domainprefix/DomainPrefixReplayRenderer.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/domainprefix/DomainPrefixReplayRenderer.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -38,12 +38,12 @@ import org.archive.wayback.core.Resource; import org.archive.wayback.core.CaptureSearchResult; import org.archive.wayback.core.CaptureSearchResults; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; import org.archive.wayback.exception.BadContentException; import org.archive.wayback.replay.HTMLPage; import org.archive.wayback.replay.HttpHeaderProcessor; import org.archive.wayback.replay.HttpHeaderOperation; +import org.archive.wayback.util.Timestamp; import org.archive.wayback.util.url.UrlOperations; /** Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/domainprefix/DomainPrefixRequestParser.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/domainprefix/DomainPrefixRequestParser.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/domainprefix/DomainPrefixRequestParser.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -29,10 +29,10 @@ import javax.servlet.http.HttpServletRequest; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; import org.archive.wayback.exception.BadQueryException; import org.archive.wayback.requestparser.BaseRequestParser; +import org.archive.wayback.util.Timestamp; import org.archive.wayback.webapp.AccessPoint; /** Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/liveweb/LiveWebCache.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/liveweb/LiveWebCache.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/liveweb/LiveWebCache.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -37,13 +37,13 @@ import org.archive.wayback.core.CaptureSearchResult; import org.archive.wayback.core.CaptureSearchResults; import org.archive.wayback.core.SearchResults; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; import org.archive.wayback.exception.LiveDocumentNotAvailableException; import org.archive.wayback.exception.ResourceNotInArchiveException; import org.archive.wayback.exception.WaybackException; import org.archive.wayback.resourcestore.indexer.ARCRecordToSearchResultAdapter; import org.archive.wayback.resourcestore.resourcefile.ArcResource; +import org.archive.wayback.util.Timestamp; import org.archive.wayback.util.url.AggressiveUrlCanonicalizer; /** Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/query/resultspartitioner/ResultsPartitioner.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/query/resultspartitioner/ResultsPartitioner.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/query/resultspartitioner/ResultsPartitioner.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -29,8 +29,8 @@ import java.util.SimpleTimeZone; import java.util.TimeZone; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; +import org.archive.wayback.util.Timestamp; /** * Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/query/resultspartitioner/ResultsPartitionsFactory.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/query/resultspartitioner/ResultsPartitionsFactory.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/query/resultspartitioner/ResultsPartitionsFactory.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -30,8 +30,8 @@ import org.archive.util.ArchiveUtils; import org.archive.wayback.core.CaptureSearchResults; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; +import org.archive.wayback.util.Timestamp; /** * Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/query/resultspartitioner/ResultsTimelinePartitionsFactory.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/query/resultspartitioner/ResultsTimelinePartitionsFactory.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/query/resultspartitioner/ResultsTimelinePartitionsFactory.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -29,8 +29,8 @@ import org.archive.util.ArchiveUtils; import org.archive.wayback.core.CaptureSearchResults; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; +import org.archive.wayback.util.Timestamp; /** * Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/requestparser/BaseRequestParser.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/requestparser/BaseRequestParser.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/requestparser/BaseRequestParser.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -29,9 +29,9 @@ import javax.servlet.http.HttpServletRequest; import org.archive.wayback.RequestParser; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; import org.archive.wayback.exception.BadQueryException; +import org.archive.wayback.util.Timestamp; import org.archive.wayback.webapp.AccessPoint; /** Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/requestparser/FormRequestParser.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/requestparser/FormRequestParser.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/requestparser/FormRequestParser.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -30,8 +30,8 @@ import javax.servlet.http.HttpServletRequest; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; +import org.archive.wayback.util.Timestamp; import org.archive.wayback.webapp.AccessPoint; /** Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/LocalResourceIndex.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/LocalResourceIndex.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/LocalResourceIndex.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -37,7 +37,6 @@ import org.archive.wayback.core.CaptureToUrlSearchResultAdapter; import org.archive.wayback.core.SearchResult; import org.archive.wayback.core.SearchResults; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.UrlSearchResult; import org.archive.wayback.core.UrlSearchResults; import org.archive.wayback.core.WaybackRequest; @@ -61,6 +60,7 @@ import org.archive.wayback.util.ObjectFilter; import org.archive.wayback.util.ObjectFilterChain; import org.archive.wayback.util.ObjectFilterIterator; +import org.archive.wayback.util.Timestamp; import org.archive.wayback.util.url.AggressiveUrlCanonicalizer; /** Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/NutchResourceIndex.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/NutchResourceIndex.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/NutchResourceIndex.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -40,13 +40,13 @@ import org.archive.wayback.core.CaptureSearchResult; import org.archive.wayback.core.CaptureSearchResults; import org.archive.wayback.core.SearchResults; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.core.WaybackRequest; import org.archive.wayback.exception.AccessControlException; import org.archive.wayback.exception.BadQueryException; import org.archive.wayback.exception.ConfigurationException; import org.archive.wayback.exception.ResourceIndexNotAvailableException; import org.archive.wayback.exception.ResourceNotInArchiveException; +import org.archive.wayback.util.Timestamp; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -71,10 +71,10 @@ private String searchUrlBase; private DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); private DocumentBuilder builder; - private static final String NUTCH_ARCNAME = "arcname"; - private static final String NUTCH_ARCOFFSET = "arcoffset"; -// private static final String NUTCH_FILENAME = "filename"; -// private static final String NUTCH_FILEOFFSET = "fileoffset"; +// private static final String NUTCH_ARCNAME = "arcname"; +// private static final String NUTCH_ARCOFFSET = "arcoffset"; + private static final String NUTCH_FILENAME = "filename"; + private static final String NUTCH_FILEOFFSET = "fileoffset"; private static final String NUTCH_ARCDATE = "date"; // private static final String NUTCH_ARCDATE_ALT = "arcdate"; private static final String NUTCH_DIGEST = "digest"; @@ -194,11 +194,11 @@ private List<CaptureSearchResult> itemToSearchResults(Element e) throws ResourceIndexNotAvailableException { - String fileName = getNodeNutchContent(e,NUTCH_ARCNAME); + String fileName = getNodeNutchContent(e,NUTCH_FILENAME); String httpCode = NUTCH_DEFAULT_HTTP_CODE; String digest = getNodeNutchContent(e,NUTCH_DIGEST); String mimeType = getNodeNutchContent(e,NUTCH_MIME_TYPE); - String offsetStr = getNodeNutchContent(e,NUTCH_ARCOFFSET); + String offsetStr = getNodeNutchContent(e,NUTCH_FILEOFFSET); long offset = 0; if(offsetStr != null && offsetStr.length() > 0) { offset = Long.parseLong(offsetStr); Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/filters/DateRangeFilter.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/filters/DateRangeFilter.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/filters/DateRangeFilter.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -25,8 +25,8 @@ package org.archive.wayback.resourceindex.filters; import org.archive.wayback.core.CaptureSearchResult; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.util.ObjectFilter; +import org.archive.wayback.util.Timestamp; /** * SearchResultFilter that excludes records outside of start and end range. Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/filters/EndDateFilter.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/filters/EndDateFilter.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/filters/EndDateFilter.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -25,8 +25,8 @@ package org.archive.wayback.resourceindex.filters; import org.archive.wayback.core.CaptureSearchResult; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.util.ObjectFilter; +import org.archive.wayback.util.Timestamp; /** Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/filters/StartDateFilter.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/filters/StartDateFilter.java 2008-07-15 01:30:13 UTC (rev 2447) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/resourceindex/filters/StartDateFilter.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -25,8 +25,8 @@ package org.archive.wayback.resourceindex.filters; import org.archive.wayback.core.CaptureSearchResult; -import org.archive.wayback.core.Timestamp; import org.archive.wayback.util.ObjectFilter; +import org.archive.wayback.util.Timestamp; /** * SearchResultFilter which includes all records until 1 is found before start Copied: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/Timestamp.java (from rev 2443, trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/Timestamp.java) =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/Timestamp.java (rev 0) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/Timestamp.java 2008-07-15 01:37:42 UTC (rev 2448) @@ -0,0 +1,459 @@ +/* Timestamp + * + * Created on 2005/10/18 14:00:00 + * + * Copyright (C) 2005 Internet Archive. + * + * This file is part of the Wayback Machine (crawler.archive.org). + * + * Wayback Machine is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * any later version. + * + * Wayback Machine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser Public License for more details. + * + * You should have received a copy of the GNU Lesser Public License + * along with Wayback Machine; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package org.archive.wayback.util; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.SimpleTimeZone; +import java.util.TimeZone; + + +/** + * Represents a moment in time as a 14-digit string, and interally as a Date. + * + * @author Brad Tofel + * @version $Date$, $Revision$ + */ +public class Timestamp { + + private final static String LOWER_TIMESTAMP_LIMIT = "10000000000000"; + private final static String UPPER_TIMESTAMP_LIMIT = "29991939295959"; + private final static String YEAR_LOWER_LIMIT = "1996"; + private final static String YEAR_UPPER_LIMIT = + String.valueOf(Calendar.getInstance().get(Calendar.YEAR)); + private final static String MONTH_LOWER_LIMIT = "01"; + private final static String MONTH_UPPER_LIMIT = "12"; + private final static String DAY_LOWER_LIMIT = "01"; + private final static String HOUR_UPPER_LIMIT = "23"; + private final static String HOUR_LOWER_LIMIT = "00"; + private final static String MINUTE_UPPER_LIMIT = "59"; + private final static String MINUTE_LOWER_LIMIT = "00"; + private final static String SECOND_UPPER_LIMIT = "59"; + private final static String SECOND_LOWER_LIMIT = "00"; + + private final static int SSE_1996 = 820454400; + + private final static String[] months = { "Jan", "Feb", "Mar", "Apr", "May", + "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + + private String dateStr = null; + private Date date = null; + + /** + * Constructor + */ + public Timestamp() { + super(); + } + + /** + * Construct and initialize structure from a 14-digit String timestamp. If + * the argument is too short, or specifies an invalid timestamp, cleanup + * will be attempted to create the earliest legal timestamp given the input. + * @param dateStr + */ + public Timestamp(final String dateStr) { + super(); + + Calendar cal = dateStrToCalendar(dateStr); + setDate(cal.getTime()); + } + + /** + * Construct and initialize structure from an integer number of seconds + * since the epoch. + * @param sse + */ + public Timestamp(final int sse) { + super(); + setSse(sse); + } + + /** + * Construct and initialize structure from an Date + * @param date + */ + public Timestamp(final Date date) { + super(); + setDate(date); + } + + /** + * set internal structure using Date argument + * @param date + */ + public void setDate(final Date date) { + this.date = (Date) date.clone(); + Calendar cal = getCalendar(); + cal.setTime(this.date); + dateStr = calendarToDateStr(cal); + } + + + /** + * @return Date for this Timestamp + */ + public Date getDate() { + return date; + } + + /** + * set internal structure using seconds since the epoch integer argument + * @param sse + */ + public void setSse(final int sse) { + setDate(new Date(((long)sse) * 1000)); + } + + /** + * initialize interal data structures for this Timestamp from the 14-digit + * argument. Will clean up timestamp as needed to yield the ealiest + * possible timestamp given the possible partial or wrong argument. + * + * @param dateStr + */ + public void setDateStr(String dateStr) { + Calendar cal = dateStrToCalendar(dateStr); + setDate(cal.getTime()); + } + + /** + * @return the 14-digit String representation of this Timestamp. + */ + + public String getDateStr() { + return dateStr; + } + + /** + * @return the integer number of seconds since epoch represented by this + * Timestamp. + */ + public int sse() { + return Math.round(date.getTime() / 1000); + } + + /** + * function that calculates integer seconds between this records + * timeStamp and the arguments timeStamp. result is the absolute number of + * seconds difference. + * + * @param otherTimeStamp + * @return int absolute seconds between the argument and this records + * timestamp. + */ + public int absDistanceFromTimestamp(final Timestamp otherTimeStamp) { + return Math.abs(distanceFromTimestamp(otherTimeStamp)); + } + + /** + * function that calculates integer seconds between this records + * timeStamp and the arguments timeStamp. result is negative if this records + * timeStamp is less than the argument, positive if it is greater, and 0 if + * the same. + * + * @param otherTimeStamp + * @return int milliseconds + */ + public int distanceFromTimestamp(final Timestamp otherTimeStamp) { + return otherTimeStamp.sse() - sse(); + } + + /** + * @return the year portion(first 4 digits) of this Timestamp + */ + public String getYear() { + return this.dateStr.substring(0, 4); + } + + /** + * @return the month portion(digits 5-6) of this Timestamp + */ + public String getMonth() { + return this.dateStr.substring(4, 6); + } + + /** + * @return the day portion(digits 7-8) of this Timestamp + */ + public String getDay() { + return this.dateStr.substring(6, 8); + } + + /** + * @return user friendly String representation of the date of this + * Timestamp. eg: "Jan 13, 1999" + */ + public String prettyDate() { + String year = dateStr.substring(0, 4); + String month = dateStr.substring(4, 6); + String day = dateStr.substring(6, 8); + int monthInt = Integer.parseInt(month) - 1; + String prettyMonth = "UNK"; + if ((monthInt >= 0) && (monthInt < months.length)) { + prettyMonth = months[monthInt]; + } + return prettyMonth + " " + day + ", " + year; + } + + /** + * @return user friendly String representation of the Time of this + * Timestamp. + */ + public String prettyTime() { + return dateStr.substring(8, 10) + ":" + dateStr.substring(10, 12) + ":" + + dateStr.substring(12, 14); + } + + /** + * @return user friendly String representation of the Date and Time of this + * Timestamp. + */ + public String prettyDateTime() { + return prettyDate() + " " + prettyTime(); + } + + /* + * + * ALL STATIC METHOD BELOW HERE: + * ============================= + * + */ + + private static String frontZeroPad(final String input, final int digits) { + int missing = digits - input.length(); + String padded = ""; + for(int i = 0; i < missing; i++) { + padded += "0"; + } + padded += input; + return padded; + } + private static String frontZeroPad(final int input, final int digits) { + return frontZeroPad(String.valueOf(input) ,digits); + } + + private static Calendar getCalendar() { + String[] ids = TimeZone.getAvailableIDs(0); + if (ids.length < 1) { + return null; + } + TimeZone gmt = new SimpleTimeZone(0, ids[0]); + return new GregorianCalendar(gmt); + } + + /** + * cleanup the dateStr argument assuming earliest values, and return a + * GMT calendar set to the time described by the dateStr. + * + * @param dateStr + * @return Calendar + */ + public static Calendar dateStrToCalendar(final String dateStr) { + + String paddedDateStr = padStartDateStr(dateStr); + + Calendar cal = getCalendar(); + int iYear = Integer.parseInt(paddedDateStr.substring(0,4)); + int iMonth = Integer.parseInt(paddedDateStr.substring(4,6)); + int iDay = Integer.parseInt(paddedDateStr.substring(6,8)); + int iHour = Integer.parseInt(paddedDateStr.substring(8,10)); + int iMinute = Integer.parseInt(paddedDateStr.substring(10,12)); + int iSecond = Integer.parseInt(paddedDateStr.substring(12,14)); + + cal.set(Calendar.YEAR,iYear); + cal.set(Calendar.MONTH,iMonth - 1); + cal.set(Calendar.DAY_OF_MONTH,iDay); + cal.set(Calendar.HOUR_OF_DAY,iHour); + cal.set(Calendar.MINUTE,iMinute); + cal.set(Calendar.SECOND,iSecond); + + return cal; + } + private static String calendarToDateStr(Calendar cal) { + return frontZeroPad(cal.get(Calendar.YEAR),4) + + frontZeroPad(cal.get(Calendar.MONTH) + 1 ,2) + + frontZeroPad(cal.get(Calendar.DAY_OF_MONTH),2) + + frontZeroPad(cal.get(Calendar.HOUR_OF_DAY),2) + + frontZeroPad(cal.get(Calendar.MINUTE),2) + + frontZeroPad(cal.get(Calendar.SECOND),2); + } + + + private static String padDigits(String input, String min, String max, + String missing) { + if(input == null) { + input = ""; + } + String finalDigits = ""; + for(int i = 0; i < missing.length(); i++) { + if(input.length() <= i) { + finalDigits = finalDigits + missing.charAt(i); + } else { + char inc = input.charAt(i); + char maxc = max.charAt(i); + char minc = min.charAt(i); + if(inc > maxc) { + inc = maxc; + } else if (inc < minc) { + inc = minc; + } + finalDigits = finalDigits + inc; + } + } + + return finalDigits; + } + + private static String boundDigits(String input, String min, String max) { + String bounded = input; + if(input.compareTo(min) < 0) { + bounded = min; + } else if(input.compareTo(max) > 0) { + bounded = max; + } + return bounded; + } + + // check each of YEAR, MONTH, DAY, HOUR, MINUTE, SECOND to make sure they + // are not too large or too small, factoring in the month, leap years, etc. + private static String boundTimestamp(String input) { + String boundTimestamp = ""; + if(input == null) { + input = ""; + } + // MAKE SURE THE YEAR IS WITHIN LEGAL BOUNDARIES: + Calendar tmpCal = getCalendar(); + tmpCal.setTime(new Date()); + + boundTimestamp = boundDigits(input.substring(0,4), + YEAR_LOWER_LIMIT,YEAR_UPPER_LIMIT); + + // MAKE SURE THE MONTH IS WITHIN LEGAL BOUNDARIES: + boundTimestamp += boundDigits(input.substring(4,6), + MONTH_LOWER_LIMIT,MONTH_UPPER_LIMIT); + + // NOW DEPENDING ON THE YEAR + MONTH, MAKE SURE THE DAY OF MONTH IS + // WITHIN LEGAL BOUNDARIES: + Calendar cal = getCalendar(); + cal.clear(); + int iYear = Integer.parseInt(boundTimestamp.substring(0,4)); + int iMonth = Integer.parseInt(boundTimestamp.substring(4,6)); + cal.set(Calendar.YEAR,iYear); + cal.set(Calendar.MONTH,iMonth - 1); + cal.set(Calendar.DAY_OF_MONTH,1); + + String maxDayOfMonth = String.valueOf(cal.getActualMaximum(Calendar.DAY_OF_MONTH)); + if(maxDayOfMonth.length() == 1) { + maxDayOfMonth = "0" + maxDayOfMonth; + } + boundTimestamp += boundDigits(input.substring(6,8), + DAY_LOWER_LIMIT,maxDayOfMonth); + + // MAKE SURE THE HOUR IS WITHIN LEGAL BOUNDARIES: + boundTimestamp += boundDigits(input.substring(8,10), + HOUR_LOWER_LIMIT,HOUR_UPPER_LIMIT); + + // MAKE SURE THE MINUTE IS WITHIN LEGAL BOUNDARIES: + boundTimestamp += boundDigits(input.substring(10,12), + MINUTE_LOWER_LIMIT,MINUTE_UPPER_LIMIT); + + // MAKE SURE THE SECOND IS WITHIN LEGAL BOUNDARIES: + boundTimestamp += boundDigits(input.substring(12,14), + SECOND_LOWER_LIMIT,SECOND_UPPER_LIMIT); + + return boundTimestamp; + } + + /** + * clean up timestamp argument assuming latest possible values for missing + * or bogus digits. + * @param timestamp String + * @return String + */ + public static String padEndDateStr(String timestamp) { + return boundTimestamp(padDigits(timestamp,LOWER_TIMESTAMP_LIMIT, + UPPER_TIMESTAMP_LIMIT,UPPER_TIMESTAMP_LIMIT)); + } + + /** + * clean up timestamp argument assuming earliest possible values for missing + * or bogus digits. + * @param timestamp String + * @return String + */ + public static String padStartDateStr(String timestamp) { + return boundTimestamp(padDigits(timestamp,LOWER_TIMESTAMP_LIMIT, + UPPER_TIMESTAMP_LIMIT,LOWER_TIMESTAMP_LIMIT)); + } + + /** + * @param dateStr + * @return Timestamp object representing the earliest date represented by + * the (possibly) partial digit-string argument. + */ + public static Timestamp parseBefore(final String dateStr) { + return new Timestamp(padStartDateStr(dateStr)); + } + + /** + * @param dateStr + * @return Timestamp object representing the latest date represented by the + * (possibly) partial digit-string argument. + */ + public static Timestamp parseAfter(final String dateStr) { + return new Timestamp(padEndDateStr(dateStr)); + } + + /** + * @param sse + * @return Timestamp object representing the seconds since epoch argument. + */ + public static Timestamp fromSse(final int sse) { + //String dateStr = ArchiveUtils.get14DigitDate(sse * 1000); + return new Timestamp(sse); + } + + /** + * @return Timestamp object representing the current date. + */ + public static Timestamp currentTimestamp() { + return new Timestamp(new Date()); + } + + /** + * @return Timestamp object representing the latest possible date. + */ + public static Timestamp latestTimestamp() { + return currentTimestamp(); + } + + /** + * @return Timestamp object representing the earliest possible date. + */ + public static Timestamp earliestTimestamp() { + return new Timestamp(SSE_1996); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |