001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * 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, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.i18n.client;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: import java.util.Date;
021:
022: /**
023: * Tests formatting functionality in {@link DateTimeFormat} for the German
024: * language.
025: */
026: public class DateTimeFormat_de_Test extends GWTTestCase {
027:
028: public String getModuleName() {
029: return "com.google.gwt.i18n.I18NTest_de";
030: }
031:
032: public void test_EEEEMMMddyy() {
033: Date date = getDateFromUTC(2006 - 1900, 6, 27, 13, 10, 10);
034: assertEquals("Donnerstag,Juli 27, 2006", format(
035: "EEEE,MMMM dd, yyyy", date));
036: }
037:
038: public void test_EEEMMMddyy() {
039: Date date = getDateFromUTC(2006 - 1900, 6, 27, 13, 10, 10);
040: assertEquals("Do, Jul 27, 06", format("EEE, MMM d, yy", date));
041: }
042:
043: public void test_HHmmss() {
044: Date date = getDateFromUTC(2006 - 1900, 6, 27, 13, 10, 10);
045: assertEquals("13:10:10", format("HH:mm:ss", date));
046: }
047:
048: public void test_hhmmssa() {
049: Date date = getDateFromUTC(2006 - 1900, 6, 27, 13, 10, 10);
050: assertEquals("1:10:10 nachm.", format("h:mm:ss a", date));
051: }
052:
053: public void test_MMddyyyyHHmmssZ() {
054: Date date = getDateFromUTC(2006 - 1900, 6, 27, 13, 10, 10);
055: String tz = getTimezoneString(date, false);
056: String actual = format("MM/dd/yyyy HH:mm:ss Z", date);
057: assertEquals("07/27/2006 13:10:10 " + tz, actual);
058: }
059:
060: public void test_MMddyyyyHHmmsszzz() {
061: Date date = getDateFromUTC(2006 - 1900, 6, 27, 13, 10, 10);
062: String tz = getTimezoneString(date);
063: String actual = format("MM/dd/yyyy HH:mm:ss zzz", date);
064: assertEquals("07/27/2006 13:10:10 GMT" + tz, actual);
065: }
066:
067: public void test_predefinedFormat() {
068: Date date = getDateFromUTC(2006 - 1900, 7, 4, 13, 49, 24);
069: String tz = getTimezoneString(date);
070:
071: String fullDateFormat = DateTimeFormat.getFullDateFormat()
072: .format(date);
073: assertEquals("Freitag, 4. August 2006", fullDateFormat);
074:
075: String longDateFormat = DateTimeFormat.getLongDateFormat()
076: .format(date);
077: assertEquals("4. August 2006", longDateFormat);
078:
079: String medDateFormat = DateTimeFormat.getMediumDateFormat()
080: .format(date);
081: assertEquals("04.08.2006", medDateFormat);
082:
083: String shortDateFormat = DateTimeFormat.getShortDateFormat()
084: .format(date);
085: assertEquals("04.08.06", shortDateFormat);
086:
087: String fullTimeFormat = DateTimeFormat.getFullTimeFormat()
088: .format(date);
089: assertEquals("13:49 Uhr GMT" + tz, fullTimeFormat);
090:
091: String longTimeFormat = DateTimeFormat.getLongTimeFormat()
092: .format(date);
093: assertEquals("13:49:24 GMT" + tz, longTimeFormat);
094:
095: String medTimeFormat = DateTimeFormat.getMediumTimeFormat()
096: .format(date);
097: assertEquals("13:49:24", medTimeFormat);
098:
099: String shortTimeFormat = DateTimeFormat.getShortTimeFormat()
100: .format(date);
101: assertEquals("13:49", shortTimeFormat);
102:
103: String fullFormat = DateTimeFormat.getFullDateTimeFormat()
104: .format(date);
105: assertEquals("Freitag, 4. August 2006 13:49 Uhr GMT" + tz,
106: fullFormat);
107:
108: String longFormat = DateTimeFormat.getLongDateTimeFormat()
109: .format(date);
110: assertEquals("4. August 2006 13:49:24 GMT" + tz, longFormat);
111:
112: String medFormat = DateTimeFormat.getMediumDateTimeFormat()
113: .format(date);
114: assertEquals("04.08.2006 13:49:24", medFormat);
115:
116: String shortFormat = DateTimeFormat.getShortDateTimeFormat()
117: .format(date);
118: assertEquals("04.08.06 13:49", shortFormat);
119: }
120:
121: public void test_QQQQyy() {
122: Date date;
123:
124: date = getDateFromUTC(2006 - 1900, 0, 27, 13, 10, 10);
125: assertEquals("1. Quartal 06", format("QQQQ yy", date));
126:
127: date = getDateFromUTC(2006 - 1900, 1, 27, 13, 10, 10);
128: assertEquals("1. Quartal 06", format("QQQQ yy", date));
129:
130: date = getDateFromUTC(2006 - 1900, 2, 27, 13, 10, 10);
131: assertEquals("1. Quartal 06", format("QQQQ yy", date));
132:
133: date = getDateFromUTC(2006 - 1900, 3, 27, 13, 10, 10);
134: assertEquals("2. Quartal 06", format("QQQQ yy", date));
135:
136: date = getDateFromUTC(2006 - 1900, 4, 27, 13, 10, 10);
137: assertEquals("2. Quartal 06", format("QQQQ yy", date));
138:
139: date = getDateFromUTC(2006 - 1900, 5, 27, 13, 10, 10);
140: assertEquals("2. Quartal 06", format("QQQQ yy", date));
141:
142: date = getDateFromUTC(2006 - 1900, 6, 27, 13, 10, 10);
143: assertEquals("3. Quartal 06", format("QQQQ yy", date));
144:
145: date = getDateFromUTC(2006 - 1900, 7, 27, 13, 10, 10);
146: assertEquals("3. Quartal 06", format("QQQQ yy", date));
147:
148: date = getDateFromUTC(2006 - 1900, 8, 27, 13, 10, 10);
149: assertEquals("3. Quartal 06", format("QQQQ yy", date));
150:
151: date = getDateFromUTC(2006 - 1900, 9, 27, 13, 10, 10);
152: assertEquals("4. Quartal 06", format("QQQQ yy", date));
153:
154: date = getDateFromUTC(2006 - 1900, 10, 27, 13, 10, 10);
155: assertEquals("4. Quartal 06", format("QQQQ yy", date));
156:
157: date = getDateFromUTC(2006 - 1900, 11, 27, 13, 10, 10);
158: assertEquals("4. Quartal 06", format("QQQQ yy", date));
159: }
160:
161: public void test_QQyyyy() {
162: Date date = new Date(2006 - 1900, 0, 27);
163: date.setHours(13);
164: date.setMinutes(10);
165: date.setSeconds(10);
166: assertEquals("Q1 2006", format("QQ yyyy", date));
167: date = new Date(2006 - 1900, 1, 27);
168: date.setHours(13);
169: date.setMinutes(10);
170: date.setSeconds(10);
171: assertEquals("Q1 2006", format("QQ yyyy", date));
172: date = new Date(2006 - 1900, 2, 27);
173: date.setHours(13);
174: date.setMinutes(10);
175: date.setSeconds(10);
176: assertEquals("Q1 2006", format("QQ yyyy", date));
177: date = new Date(2006 - 1900, 3, 27);
178: date.setHours(13);
179: date.setMinutes(10);
180: date.setSeconds(10);
181: assertEquals("Q2 2006", format("QQ yyyy", date));
182: date = new Date(2006 - 1900, 4, 27);
183: date.setHours(13);
184: date.setMinutes(10);
185: date.setSeconds(10);
186: assertEquals("Q2 2006", format("QQ yyyy", date));
187: date = new Date(2006 - 1900, 5, 27);
188: date.setHours(13);
189: date.setMinutes(10);
190: date.setSeconds(10);
191: assertEquals("Q2 2006", format("QQ yyyy", date));
192: date = new Date(2006 - 1900, 6, 27);
193: date.setHours(13);
194: date.setMinutes(10);
195: date.setSeconds(10);
196: assertEquals("Q3 2006", format("QQ yyyy", date));
197: date = new Date(2006 - 1900, 7, 27);
198: date.setHours(13);
199: date.setMinutes(10);
200: date.setSeconds(10);
201: assertEquals("Q3 2006", format("QQ yyyy", date));
202: date = new Date(2006 - 1900, 8, 27);
203: date.setHours(13);
204: date.setMinutes(10);
205: date.setSeconds(10);
206: assertEquals("Q3 2006", format("QQ yyyy", date));
207: date = new Date(2006 - 1900, 9, 27);
208: date.setHours(13);
209: date.setMinutes(10);
210: date.setSeconds(10);
211: assertEquals("Q4 2006", format("QQ yyyy", date));
212: date = new Date(2006 - 1900, 10, 27);
213: date.setHours(13);
214: date.setMinutes(10);
215: date.setSeconds(10);
216: assertEquals("Q4 2006", format("QQ yyyy", date));
217: date = new Date(2006 - 1900, 11, 27);
218: date.setHours(13);
219: date.setMinutes(10);
220: date.setSeconds(10);
221: assertEquals("Q4 2006", format("QQ yyyy", date));
222: }
223:
224: public void test_quote() {
225: Date date = new Date(2006 - 1900, 6, 27);
226: date.setHours(13);
227: date.setMinutes(10);
228: date.setSeconds(10);
229: assertEquals("13 o'clock", format("HH 'o''clock'", date));
230: assertEquals("13 oclock", format("HH 'oclock'", date));
231: assertEquals("13 '", format("HH ''", date));
232: }
233:
234: public void test_yyyyMMddG() {
235: Date date = getDateFromUTC(2006 - 1900, 6, 27, 13, 10, 10);
236: String tz = getTimezoneString(date);
237:
238: String expected = "2006.07.27 n. Chr. at 13:10:10 GMT" + tz;
239: String actual = format("yyyy.MM.dd G 'at' HH:mm:ss vvvv", date);
240: assertEquals(expected, actual);
241: }
242:
243: public void test_yyyyyMMMMM() {
244: Date date = getDateFromUTC(2006 - 1900, 6, 27, 13, 10, 10);
245: assertEquals("2006.J.27 n. Chr. 01:10 nachm.", format(
246: "yyyyy.MMMMM.dd GGG hh:mm aaa", date));
247: }
248:
249: private String format(String pattern, Date toFormat) {
250: DateTimeFormat fmt = DateTimeFormat.getFormat(pattern);
251: return fmt.format(toFormat);
252: }
253:
254: private String getTimezoneString(Date date) {
255: return getTimezoneString(date, true);
256: }
257:
258: private String getTimezoneString(Date date, boolean useColon) {
259: int tzOffset = date.getTimezoneOffset();
260: int tzAbsOffset = Math.abs(tzOffset);
261: int tzAbsOffsetHrs = tzAbsOffset / 60;
262: int tzAbsOffsetMins = tzAbsOffset % 60;
263: String tzOffsetStr = (tzOffset < 0 ? "+" : "-")
264: + (tzAbsOffsetHrs < 10 ? "0" : "")
265: + Integer.toString(tzAbsOffsetHrs)
266: + (useColon ? ":" : "")
267: + (tzAbsOffsetMins < 10 ? "0" : "")
268: + Integer.toString(tzAbsOffsetMins);
269: return tzOffsetStr;
270: }
271:
272: private Date getDateFromUTC(int year, int month, int day, int hrs,
273: int mins, int secs) {
274: long localTzOffset = new Date(year, month, day, hrs, mins, secs)
275: .getTimezoneOffset();
276: long utc = Date.UTC(year, month, day, hrs, mins, secs);
277: long localTzOffsetMillis = localTzOffset * 60 * 1000;
278: long localTime = utc + localTzOffsetMillis;
279: return new Date(localTime);
280: }
281: }
|