001    /*
002     * Copyright 1999,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.feedparser.tools;
018    
019    import java.text.SimpleDateFormat;
020    import java.util.Date;
021    import java.util.Locale;
022    import java.util.TimeZone;
023    
024    /**
025     *
026     * RFC 822 date parsing utility.  Designed for parsing the ISO subset used in
027     * Dublin Core, RSS 1.0, and Atom.
028     *
029     * 
030     http://asg.web.cmu.edu/rfc/rfc822.html#sec-5
031     * 
032     5.1 SYNTAX
033     * 
034     * date-time   =  [ day "," ] date time        ; dd mm yy
035     * ;  hh:mm:ss zzz
036     * 
037     * day         =  "Mon"  / "Tue" /  "Wed"  / "Thu"
038     * /  "Fri"  / "Sat" /  "Sun"
039     * 
040     * date        =  1*2DIGIT month 2DIGIT        ; day month year
041     * ;  e.g. 20 Jun 82
042     * 
043     * month       =  "Jan"  /  "Feb" /  "Mar"  /  "Apr"
044     * /  "May"  /  "Jun" /  "Jul"  /  "Aug"
045     * /  "Sep"  /  "Oct" /  "Nov"  /  "Dec"
046     * 
047     * time        =  hour zone                    ; ANSI and Military
048     * 
049     * hour        =  2DIGIT ":" 2DIGIT [":" 2DIGIT]
050     * ; 00:00:00 - 23:59:59
051     * 
052     * zone        =  "UT"  / "GMT"                ; Universal Time
053     * ; North American : UT
054     * /  "EST" / "EDT"                ;  Eastern:  - 5/ - 4
055     * /  "CST" / "CDT"                ;  Central:  - 6/ - 5
056     * /  "MST" / "MDT"                ;  Mountain: - 7/ - 6
057     * /  "PST" / "PDT"                ;  Pacific:  - 8/ - 7
058     * /  1ALPHA                       ; Military: Z = UT;
059     * ;  A:-1; (J not used)
060     * ;  M:-12; N:+1; Y:+12
061     * / ( ("+" / "-") 4DIGIT )        ; Local differential
062     * ;  hours+min. (HHMM)
063     * 
064     5.2 SEMANTICS
065     * 
066     * If included, day-of-week must be the day implied by the date specification.
067     * 
068     * Time zone may be indicated in several ways. "UT" is Univer- sal Time
069     * (formerly called "Greenwich Mean Time"); "GMT" is per- mitted as a reference
070     * to Universal Time. The military standard uses a single character for each
071     * zone. "Z" is Universal Time. "A" indicates one hour earlier, and "M"
072     * indicates 12 hours ear- lier; "N" is one hour later, and "Y" is 12 hours
073     * later. The letter "J" is not used. The other remaining two forms are taken
074     * from ANSI standard X3.51-1975. One allows explicit indication of the amount
075     * of offset from UT; the other uses common 3-character strings for indicating
076     * time zones in North America.
077     * 
078     * 
079     * 
080     * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a>
081     * @version $Id: RFC822DateParser.java 373572 2006-01-30 19:28:41Z mvdb $
082     */
083    public class RFC822DateParser {
084    
085        private static SimpleDateFormat df
086            = new SimpleDateFormat( "EEE, dd MMM yyyy hh:mm:ss z", Locale.ENGLISH );
087    
088        public static Date parse( String input ) throws java.text.ParseException {
089    
090            //NOTE: SimpleDateFormat uses GMT[-+]hh:mm for the TZ which breaks
091            //things a bit.  Before we go on we have to repair this.
092    
093            return df.parse( input );
094            
095        }
096    
097        public static String toString( Date date, TimeZone tz ) {
098    
099            df.setTimeZone( tz );
100    
101            return df.format( date );
102            
103        }
104        
105    }
106