Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/nwalsh/xalan/CVS.java


1   package com.nwalsh.xalan;
2   
3   import java.io.*;
4   import java.util.Calendar;
5   import java.util.GregorianCalendar;
6   import java.util.Date;
7   import java.util.Locale;
8   import java.util.TimeZone;
9   import java.text.DateFormat;
10  import java.text.ParseException;
11  
12  /**
13   * <p>Saxon extension to convert CVS date strings into local time</p>
14   *
15   *
16   * <p>Copyright (C) 2000 Norman Walsh.</p>
17   *
18   * <p>This class provides a
19   * <a href="http://users.iclway.co.uk/mhkay/saxon/">Saxon</a>
20   * extension to turn the CVS date strings, which are UTC:</p>
21   *
22   * <pre>&#36;Date: 2000/11/09 02:34:20 &#36;</pre>
23   *
24   * <p>into legibly formatted local time:</p>
25   *
26   * <pre>Wed Nov 08 18:34:20 PST 2000</pre>
27   *
28   * <p>(I happened to be in California when I wrote this documentation.)</p>
29  
30   * <p><b>Change Log:</b></p>
31   * <dl>
32   * <dt>1.0</dt>
33   * <dd><p>Initial release.</p></dd>
34   * </dl>
35   *
36   * @author Norman Walsh
37   * <a href="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
38   *
39   *
40   */
41  public class CVS {
42    /**
43     * <p>Constructor for CVS</p>
44     *
45     * <p>All of the methods are static, so the constructor does nothing.</p>
46     */
47    public CVS() {
48    }
49  
50    /**
51     * <p>Convert a CVS date string into local time.</p>
52     *
53     * @param cvsDate The CVS date string.
54     *
55     * @return The date, converted to local time and reformatted.
56     */
57    public String localTime (String cvsDate) {
58      // A cvsDate has the following form "$Date: 2002/02/06 15:42:22 $"
59      if (!cvsDate.startsWith("$Date: ")) {
60        return cvsDate;
61      }
62  
63      String yrS = cvsDate.substring(7,11);
64      String moS = cvsDate.substring(12,14);
65      String daS = cvsDate.substring(15,17);
66      String hrS = cvsDate.substring(18,20);
67      String miS = cvsDate.substring(21,23);
68      String seS = cvsDate.substring(24,26);
69  
70      TimeZone tz = TimeZone.getTimeZone("GMT+0");
71      GregorianCalendar gmtCal = new GregorianCalendar(tz);
72  
73      try {
74        gmtCal.set(Integer.parseInt(yrS),
75       Integer.parseInt(moS)-1,
76       Integer.parseInt(daS),
77       Integer.parseInt(hrS),
78       Integer.parseInt(miS),
79       Integer.parseInt(seS));
80      } catch (NumberFormatException e) {
81        // nop
82      }
83  
84      Date d = gmtCal.getTime();
85  
86      return d.toString();
87    }
88  }