1 /*
2  **********************************************************************
3  * Copyright (c) 2004-2010, International Business Machines
4  * Corporation and others.  All Rights Reserved.
5  **********************************************************************
6  * Author: John Emmons
7  **********************************************************************
8  */
9 
10 package org.unicode.cldr.posix;
11 
12 import java.io.PrintWriter;
13 
14 import org.unicode.cldr.util.CLDRFile;
15 
16 import com.ibm.icu.text.NumberingSystem;
17 
18 public class POSIX_LCTime {
19     String abday[];
20     String day[];
21     String abmon[];
22     String mon[];
23     String d_t_fmt;
24     String d_fmt;
25     String t_fmt;
26     String am_pm[];
27     String t_fmt_ampm;
28     String alt_digits[];
29 
30     // platform specific
31     String date_fmt;
32     String nlldate;
33 
POSIX_LCTime(CLDRFile doc, POSIXVariant variant)34     public POSIX_LCTime(CLDRFile doc, POSIXVariant variant) {
35         abday = new String[7];
36         day = new String[7];
37         abmon = new String[12];
38         mon = new String[12];
39         am_pm = new String[2];
40         alt_digits = new String[100];
41 
42         String[] days = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
43         String SearchLocation;
44 
45         for (int i = 0; i < 7; i++) {
46             // Get each value for abbreviated day names ( abday )
47             SearchLocation = "//ldml/dates/calendars/calendar[@type='gregorian']/days/dayContext[@type='format']/dayWidth[@type='abbreviated']/day[@type='"
48                 + days[i] + "']";
49             abday[i] = POSIXUtilities.POSIXCharName(doc.getWinningValue(SearchLocation));
50 
51             // Get each value for full month names ( day )
52             SearchLocation = "//ldml/dates/calendars/calendar[@type='gregorian']/days/dayContext[@type='format']/dayWidth[@type='wide']/day[@type='"
53                 + days[i] + "']";
54             day[i] = POSIXUtilities.POSIXCharName(doc.getWinningValue(SearchLocation));
55         }
56 
57         for (int i = 0; i < 12; i++) {
58             // Get each value for abbreviated month names ( abmon )
59             SearchLocation = "//ldml/dates/calendars/calendar[@type='gregorian']/months/monthContext[@type='format']/monthWidth[@type='abbreviated']/month[@type='"
60                 + String.valueOf(i + 1) + "']";
61             abmon[i] = POSIXUtilities.POSIXCharName(doc.getWinningValue(SearchLocation));
62 
63             // Get each value for full month names ( mon )
64             SearchLocation = "//ldml/dates/calendars/calendar[@type='gregorian']/months/monthContext[@type='format']/monthWidth[@type='wide']/month[@type='"
65                 + String.valueOf(i + 1) + "']";
66             mon[i] = POSIXUtilities.POSIXCharName(doc.getWinningValue(SearchLocation));
67         }
68 
69         // alt_digits
70         alt_digits[0] = "";
71         String numsys = doc.getWinningValue("//ldml/numbers/defaultNumberingSystem");
72         if (numsys != null && !numsys.equals("latn")) {
73             NumberingSystem ns = NumberingSystem.getInstanceByName(numsys);
74             String nativeZeroDigit = ns.getDescription().substring(0, 1);
75             // Character ThisDigit;
76             alt_digits[0] = POSIXUtilities.POSIXCharName(nativeZeroDigit);
77             char base_value = nativeZeroDigit.charAt(0);
78             for (short i = 1; i < 10; i++)
79                 alt_digits[i] = POSIXUtilities.POSIXCharName(Character.toString(((char) ((short) base_value + i))));
80             for (short i = 10; i < 100; i++)
81                 alt_digits[i] = alt_digits[i / 10] + alt_digits[i % 10];
82         }
83 
84         // t_fmt -
85         SearchLocation = "//ldml/dates/calendars/calendar[@type='gregorian']/timeFormats/timeFormatLength[@type='medium']/timeFormat[@type='standard']/pattern[@type='standard']";
86         t_fmt = POSIXUtilities.POSIXDateTimeFormat(doc.getWinningValue(SearchLocation), alt_digits[0].length() > 0,
87             variant);
88         t_fmt = t_fmt.replaceAll("\"", "/\""); // excaping of " in strings
89 
90         if (t_fmt.indexOf("%p") >= 0)
91             t_fmt_ampm = t_fmt;
92         else {
93             SearchLocation = "//ldml/dates/calendars/calendar[@type='gregorian']/dateTimeFormats/availableFormats/dateFormatItem[@id='hms']";
94             t_fmt_ampm = doc.getWinningValue(SearchLocation);
95 
96             if (t_fmt_ampm != null) {
97                 t_fmt_ampm = POSIXUtilities.POSIXDateTimeFormat(t_fmt_ampm, alt_digits[0].length() > 0, variant);
98                 t_fmt_ampm = t_fmt_ampm.replaceAll("\"", "/\""); // excaping of " in strings
99             }
100 
101             if (t_fmt_ampm.indexOf("%p") < 0)
102                 t_fmt_ampm = "";
103 
104         }
105         // d_fmt -
106         SearchLocation = "//ldml/dates/calendars/calendar[@type=\"gregorian\"]" +
107             "/dateFormats/dateFormatLength[@type=\"short\"]" +
108             "/dateFormat[@type=\"standard\"]/pattern[@type=\"standard\"]";
109         d_fmt = POSIXUtilities.POSIXDateTimeFormat(doc.getWinningValue(SearchLocation), alt_digits[0].length() > 0,
110             variant);
111         d_fmt = d_fmt.replaceAll("\"", "/\""); // excaping of " in strings
112 
113         // d_t_fmt -
114         SearchLocation = "//ldml/dates/calendars/calendar[@type=\"gregorian\"]" +
115             "/dateTimeFormats/dateTimeFormatLength[@type=\"long\"]" +
116             "/dateTimeFormat[@type=\"standard\"]/pattern[@type=\"standard\"]";
117         d_t_fmt = doc.getWinningValue(SearchLocation);
118 
119         SearchLocation = "//ldml/dates/calendars/calendar[@type=\"gregorian\"]" +
120             "/timeFormats/timeFormatLength[@type=\"long\"]" +
121             "/timeFormat[@type=\"standard\"]/pattern[@type=\"standard\"]";
122         d_t_fmt = d_t_fmt.replaceAll("\\{0\\}", doc.getWinningValue(SearchLocation));
123 
124         SearchLocation = "//ldml/dates/calendars/calendar[@type='gregorian']" +
125             "/dateFormats/dateFormatLength[@type=\"long\"]" +
126             "/dateFormat[@type=\"standard\"]/pattern[@type=\"standard\"]";
127         d_t_fmt = d_t_fmt.replaceAll("\\{1\\}", doc.getWinningValue(SearchLocation));
128 
129         d_t_fmt = POSIXUtilities.POSIXDateTimeFormat(d_t_fmt, alt_digits[0].length() > 0, variant);
130         d_t_fmt = POSIXUtilities.POSIXCharNameNP(d_t_fmt);
131         d_t_fmt = d_t_fmt.replaceAll("\"", "/\""); // excaping of " in strings
132 
133         // am_pm
134         SearchLocation = "//ldml/dates/calendars/calendar[@type='gregorian']" +
135             "/dayPeriods/dayPeriodContext[@type='format']/dayPeriodWidth[@type='wide']" +
136             "/dayPeriod[@type='am']";
137         am_pm[0] = POSIXUtilities.POSIXCharName(doc.getWinningValue(SearchLocation));
138 
139         SearchLocation = "//ldml/dates/calendars/calendar[@type='gregorian']" +
140             "/dayPeriods/dayPeriodContext[@type='format']/dayPeriodWidth[@type='wide']" +
141             "/dayPeriod[@type='pm']";
142         am_pm[1] = POSIXUtilities.POSIXCharName(doc.getWinningValue(SearchLocation));
143 
144         if (variant.platform.equals(POSIXVariant.SOLARIS)) {
145             // date_fmt
146             SearchLocation = "//ldml/dates/calendars/calendar[@type='gregorian']/dateTimeFormats/dateTimeFormatLength/dateTimeFormat/pattern";
147             date_fmt = doc.getWinningValue(SearchLocation);
148 
149             SearchLocation = "//ldml/dates/calendars/calendar[@type='gregorian']/timeFormats/timeFormatLength[@type='full']/timeFormat/pattern";
150             date_fmt = date_fmt.replaceAll("\\{0\\}", doc.getWinningValue(SearchLocation));
151 
152             SearchLocation = "//ldml/dates/calendars/calendar[@type='gregorian']/dateFormats/dateFormatLength[@type='full']/dateFormat/pattern";
153             date_fmt = date_fmt.replaceAll("\\{1\\}", doc.getWinningValue(SearchLocation));
154 
155             date_fmt = POSIXUtilities.POSIXDateTimeFormat(date_fmt, alt_digits[0].length() > 0, variant);
156             date_fmt = POSIXUtilities.POSIXCharNameNP(date_fmt);
157             date_fmt = date_fmt.replaceAll("\"", "/\""); // excaping of " in strings
158 
159         } else if (variant.platform.equals(POSIXVariant.AIX)) {
160             // nlldate - prefer 0 padded date, if only alt nodes are found then an exception is thrown, in this case use
161             // fallback
162             nlldate = "";
163             String SearchLocations[] = {
164                 "//ldml/dates/calendars/calendar[@type='gregorian']/dateTimeFormats/availableFormats/dateFormatItem[@id='yyyyMMMdd']",
165                 "//ldml/dates/calendars/calendar[@type='gregorian']/dateTimeFormats/availableFormats/dateFormatItem[@id='yyyyMMMd']" };
166             for (int i = 0; i < SearchLocations.length; i++) {
167                 nlldate = doc.getWinningValue(SearchLocation); // throws exception if only alt nodes found, returns null
168                 // if nothing found
169                 if (nlldate != null) break;
170             }
171 
172             if (nlldate != null)
173                 nlldate = POSIXUtilities.POSIXDateTimeFormat(nlldate, alt_digits[0].length() > 0, variant);
174 
175             // if no match found or erroneous data, use default dd MMM yyyy
176             if ((nlldate.indexOf("%d") < 0) || (nlldate.indexOf("%b") < 0) || (nlldate.indexOf("%Y") < 0)) {
177                 nlldate = "%d %b %Y"; // dd MMM yyyy
178                 nlldate = nlldate.replaceAll("\"", "/\""); // excaping of " in strings
179             }
180         }
181 
182     }
183 
write(PrintWriter out, POSIXVariant variant)184     public void write(PrintWriter out, POSIXVariant variant) {
185 
186         out.println("*************");
187         out.println("LC_TIME");
188         out.println("*************");
189         out.println();
190 
191         // abday
192         out.print("abday   \"");
193         for (int i = 0; i < 7; i++) {
194             out.print(abday[i]);
195             if (i < 6) {
196                 out.println("\";/");
197                 out.print("        \"");
198             } else
199                 out.println("\"");
200         }
201         out.println();
202 
203         // day
204         out.print("day     \"");
205         for (int i = 0; i < 7; i++) {
206             out.print(day[i]);
207             if (i < 6) {
208                 out.println("\";/");
209                 out.print("        \"");
210             } else
211                 out.println("\"");
212         }
213         out.println();
214 
215         // abmon
216         out.print("abmon   \"");
217         for (int i = 0; i < 12; i++) {
218             out.print(abmon[i]);
219             if (i < 11) {
220                 out.println("\";/");
221                 out.print("        \"");
222             } else
223                 out.println("\"");
224         }
225         out.println();
226 
227         // mon
228         out.print("mon     \"");
229         for (int i = 0; i < 12; i++) {
230             out.print(mon[i]);
231             if (i < 11) {
232                 out.println("\";/");
233                 out.print("        \"");
234             } else
235                 out.println("\"");
236         }
237         out.println();
238 
239         // d_fmt
240         out.println("d_fmt    \"" + d_fmt + "\"");
241         out.println();
242 
243         // t_fmt
244         out.println("t_fmt    \"" + t_fmt + "\"");
245         out.println();
246 
247         // d_t_fmt
248         out.println("d_t_fmt  \"" + d_t_fmt + "\"");
249         out.println();
250 
251         // am_pm
252         out.println("am_pm    \"" + am_pm[0] + "\";\"" + am_pm[1] + "\"");
253         out.println();
254 
255         // t_fmt_ampm
256         out.println("t_fmt_ampm  \"" + t_fmt_ampm + "\"");
257         out.println();
258 
259         if (variant.platform.equals(POSIXVariant.SOLARIS)) {
260             // date_fmt
261             out.println("date_fmt    \"" + date_fmt + "\"");
262             out.println();
263         } else if (variant.platform.equals(POSIXVariant.AIX)) {
264             // nlldate
265             out.println("nlldate    \"" + nlldate + "\"");
266             out.println();
267         }
268 
269         // alt_digits
270         if (!alt_digits[0].equals("")) {
271             out.print("alt_digits \"");
272             for (int i = 0; i < 100; i++) {
273                 out.print(alt_digits[i]);
274                 if (i < 99) {
275                     out.println("\";/");
276                     out.print("           \"");
277                 } else
278                     out.println("\"");
279             }
280         }
281         out.println("END LC_TIME");
282 
283     }
284 }
285