KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > util > CVSDateFormatter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.util;
12
13
14 import java.text.ParseException JavaDoc;
15 import java.text.SimpleDateFormat JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.Locale JavaDoc;
18 import java.util.TimeZone JavaDoc;
19
20 /**
21  * Utility class for converting timestamps used in Entry file lines. The format
22  * required in the Entry file is ISO C asctime() function (Sun Apr 7 01:29:26 1996).
23  * <p>
24  * To be compatible with asctime(), the day field in the entryline format is
25  * padded with a space and not a zero. Most other CVS clients use string comparison
26  * for timestamps based on the result of the C function asctime().
27  * </p>
28  */

29 public class CVSDateFormatter {
30     
31     private static final String JavaDoc ENTRYLINE_FORMAT = "E MMM dd HH:mm:ss yyyy"; //$NON-NLS-1$
32
private static final String JavaDoc SERVER_FORMAT = "dd MMM yyyy HH:mm:ss";//$NON-NLS-1$
33
private static final int ENTRYLINE_TENS_DAY_OFFSET = 8;
34     
35     private static final SimpleDateFormat JavaDoc serverFormat = new SimpleDateFormat JavaDoc(SERVER_FORMAT, Locale.US);
36     private static SimpleDateFormat JavaDoc entryLineFormat = new SimpleDateFormat JavaDoc(ENTRYLINE_FORMAT, Locale.US);
37     
38     static {
39         entryLineFormat.setTimeZone(TimeZone.getTimeZone("GMT")); //$NON-NLS-1$
40
}
41     static synchronized public Date JavaDoc serverStampToDate(String JavaDoc text) throws ParseException JavaDoc {
42         serverFormat.setTimeZone(getTimeZone(text));
43         Date JavaDoc date = serverFormat.parse(text);
44         return date;
45     }
46     
47     static synchronized public Date JavaDoc entryLineToDate(String JavaDoc text) throws ParseException JavaDoc {
48         try {
49             if (text.charAt(ENTRYLINE_TENS_DAY_OFFSET) == ' ') {
50                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc(text);
51                 buf.setCharAt(ENTRYLINE_TENS_DAY_OFFSET, '0');
52                 text = buf.toString();
53             }
54         } catch (StringIndexOutOfBoundsException JavaDoc e) {
55             throw new ParseException JavaDoc(e.getMessage(), ENTRYLINE_TENS_DAY_OFFSET);
56         }
57         return entryLineFormat.parse(text);
58     }
59
60     static synchronized public String JavaDoc dateToEntryLine(Date JavaDoc date) {
61         if (date == null) return ""; //$NON-NLS-1$
62
String JavaDoc passOne = entryLineFormat.format(date);
63         if (passOne.charAt(ENTRYLINE_TENS_DAY_OFFSET) != '0') return passOne;
64         StringBuffer JavaDoc passTwo = new StringBuffer JavaDoc(passOne);
65         passTwo.setCharAt(ENTRYLINE_TENS_DAY_OFFSET, ' ');
66         return passTwo.toString();
67     }
68     
69     static synchronized public String JavaDoc dateToNotifyServer(Date JavaDoc date) {
70         serverFormat.setTimeZone(TimeZone.getTimeZone("GMT"));//$NON-NLS-1$
71
return serverFormat.format(date) + " GMT"; //$NON-NLS-1$
72
}
73     
74     /*
75      * Converts timezone text from date string from CVS server and
76      * returns a timezone representing the received timezone.
77      * Timezone string is of the following format: [-|+]MMSS
78      */

79     static private TimeZone JavaDoc getTimeZone(String JavaDoc dateFromServer) {
80         if (dateFromServer.lastIndexOf("0000") != -1) //$NON-NLS-1$
81
return TimeZone.getTimeZone("GMT");//$NON-NLS-1$
82
String JavaDoc tz = null;
83         StringBuffer JavaDoc resultTz = new StringBuffer JavaDoc("GMT");//$NON-NLS-1$
84
if (dateFromServer.indexOf("-") != -1) {//$NON-NLS-1$
85
resultTz.append("-");//$NON-NLS-1$
86
tz = dateFromServer.substring(dateFromServer.indexOf("-"));//$NON-NLS-1$
87
} else if (dateFromServer.indexOf("+") != -1) {//$NON-NLS-1$
88
resultTz.append('+');
89             tz = dateFromServer.substring(dateFromServer.indexOf("+"));//$NON-NLS-1$
90
}
91         try {
92             if(tz!=null) {
93                 resultTz.append(tz.substring(1, 3) /*hours*/ + ":" + tz.substring(3, 5) /*minutes*/);//$NON-NLS-1$
94
return TimeZone.getTimeZone(resultTz.toString());
95             }
96         } catch(IndexOutOfBoundsException JavaDoc e) {
97             return TimeZone.getTimeZone("GMT");//$NON-NLS-1$
98
}
99         return TimeZone.getTimeZone("GMT");//$NON-NLS-1$
100
}
101 }
102
Popular Tags