78
|
1 /*
|
|
2 * Copyright cast(C) 2005 db4objects Inc. http://www.db4o.com
|
|
3 *
|
|
4 * All rights reserved. This program and the accompanying materials
|
|
5 * are made available under the terms of the Eclipse Public License v1.0
|
|
6 * which accompanies this distribution, and is available at
|
|
7 * http://www.eclipse.org/legal/epl-v10.html
|
|
8 *
|
|
9 * Contributors:
|
|
10 * db4objects - Initial API and implementation
|
|
11 * Tom Schindl<tom.schindl@bestsolution.at> - bugfix for 217940
|
|
12 */
|
|
13 module org.eclipse.core.internal.databinding.conversion.DateConversionSupport;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import java.text.ParsePosition;
|
|
18 import java.util.Date;
|
|
19
|
|
20 import org.eclipse.core.internal.databinding.BindingMessages;
|
|
21
|
|
22 import com.ibm.icu.text.DateFormat;
|
|
23 import com.ibm.icu.text.SimpleDateFormat;
|
|
24
|
|
25 /**
|
|
26 * Base support for date/string conversion handling according to the default
|
|
27 * locale or in plain long milliseconds.
|
|
28 * <p>
|
|
29 * NOTE: parse(format(date)) will generally *not* be equal to date, since the
|
|
30 * string representation may not cover the sub-second range, time-only string
|
|
31 * representations will be counted from the beginning of the era, etc.
|
|
32 * </p>
|
|
33 */
|
|
34 public abstract class DateConversionSupport {
|
85
|
35 private const static int DATE_FORMAT=DateFormat.SHORT;
|
|
36 private const static int DEFAULT_FORMATTER_INDEX=0;
|
78
|
37
|
85
|
38 private const static int NUM_VIRTUAL_FORMATTERS=1;
|
78
|
39
|
|
40 /**
|
|
41 * Alternative formatters for date, time and date/time.
|
|
42 * Raw milliseconds are covered as a special case.
|
|
43 */
|
|
44 // TODO: These could be shared, but would have to be synchronized.
|
85
|
45 private static DateFormat[] formatters;
|
|
46 static this(){
|
|
47 formatters = [ cast(DateFormat)
|
|
48 new SimpleDateFormat(BindingMessages.getString(BindingMessages.DATE_FORMAT_DATE_TIME)),
|
|
49 new SimpleDateFormat(BindingMessages.getString(BindingMessages.DATEFORMAT_TIME)),
|
78
|
50 DateFormat.getDateTimeInstance(DATE_FORMAT, DateFormat.SHORT),
|
85
|
51 DateFormat.getDateInstance(DATE_FORMAT),
|
|
52 DateFormat.getTimeInstance(DateFormat.SHORT),
|
78
|
53 DateFormat.getDateTimeInstance(DATE_FORMAT,DateFormat.MEDIUM),
|
85
|
54 DateFormat.getTimeInstance(DateFormat.MEDIUM)
|
|
55 ];
|
|
56 }
|
78
|
57
|
|
58 /**
|
|
59 * Tries all available formatters to parse the given string according to the
|
|
60 * default locale or as a raw millisecond value and returns the result of the
|
|
61 * first successful run.
|
|
62 *
|
|
63 * @param str A string specifying a date according to the default locale or in raw milliseconds
|
|
64 * @return The parsed date, or null, if no available formatter could interpret the input string
|
|
65 */
|
|
66 protected Date parse(String str) {
|
|
67 for (int formatterIdx = 0; formatterIdx < formatters.length; formatterIdx++) {
|
|
68 Date parsed=parse(str,formatterIdx);
|
|
69 if(parsed !is null) {
|
|
70 return parsed;
|
|
71 }
|
|
72 }
|
|
73 return null;
|
|
74 }
|
|
75
|
|
76 protected Date parse(String str,int formatterIdx) {
|
|
77 if(formatterIdx>=0) {
|
|
78 ParsePosition pos=new ParsePosition(0);
|
|
79 if (str is null) {
|
|
80 return null;
|
|
81 }
|
|
82 Date date=formatters[formatterIdx].parse(str,pos);
|
|
83 if(pos.getErrorIndex() !is -1||pos.getIndex() !is str.length()) {
|
|
84 return null;
|
|
85 }
|
|
86 return date;
|
|
87 }
|
|
88 try {
|
|
89 long millisecs=Long.parseLong(str);
|
|
90 return new Date(millisecs);
|
|
91 }
|
|
92 catch(NumberFormatException exc) {
|
|
93 }
|
|
94 return null;
|
|
95 }
|
|
96
|
|
97 /**
|
|
98 * Formats the given date with the default formatter according to the default locale.
|
|
99 * @param date a date
|
|
100 * @return a string representation of the given date according to the default locale
|
|
101 */
|
|
102 protected String format(Date date) {
|
|
103 return format(date,DEFAULT_FORMATTER_INDEX);
|
|
104 }
|
|
105
|
|
106 protected String format(Date date,int formatterIdx) {
|
|
107 if(formatterIdx>=0) {
|
|
108 return formatters[formatterIdx].format(date);
|
|
109 }
|
85
|
110 return String_valueOf(date.getTime());
|
78
|
111 }
|
|
112
|
|
113 protected int numFormatters() {
|
|
114 return formatters.length+NUM_VIRTUAL_FORMATTERS;
|
|
115 }
|
|
116
|
|
117 /**
|
|
118 * Returns the date format for the provided <code>index</code>.
|
|
119 * <p>
|
|
120 * This is for testing purposes only and should not be a part of the API if
|
|
121 * this class was to be exposed.
|
|
122 * </p>
|
|
123 *
|
|
124 * @param index
|
|
125 * @return date format
|
|
126 */
|
|
127 protected DateFormat getDateFormat(int index) {
|
|
128 if (index < 0 || index >= formatters.length) {
|
85
|
129 throw new IllegalArgumentException(Format("'index' [{}] is out of bounds.", index )); //$NON-NLS-1$//$NON-NLS-2$
|
78
|
130 }
|
|
131
|
|
132 return formatters[index];
|
|
133 }
|
|
134 }
|