1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 2008-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8 //! [getBestPatternExample1]
9 #include <iostream>
10 #include "unicode/smpdtfmt.h"
11 #include "unicode/dtptngen.h"
12 #include "unicode/ustdio.h"
13 //! [getBestPatternExample1]
14
15 using namespace std;
16 using namespace icu;
17
getBestPatternExample()18 static void getBestPatternExample() {
19
20 u_printf("========================================================================\n");
21 u_printf(" getBestPatternExample()\n");
22 u_printf("\n");
23 u_printf(" Use DateTimePatternGenerator to create customized date/time pattern:\n");
24 u_printf(" yQQQQ,yMMMM, MMMMd, hhmm, jjmm per locale\n");
25 u_printf("========================================================================\n");
26 //! [getBestPatternExample]
27 UnicodeString skeletons [] = {
28 UnicodeString("yQQQQ"), // year + full name of quarter, i.e., 4th quarter 1999
29 UnicodeString("yMMMM"), // year + full name of month, i.e., October 1999
30 UnicodeString("MMMMd"), // full name of month + day of the month, i.e., October 25
31 UnicodeString("hhmm"), // 12-hour-cycle format, i.e., 1:32 PM
32 UnicodeString("jjmm"), // preferred hour format for the given locale, i.e., 24-hour-cycle format for fr_FR
33 0,
34 };
35
36 Locale locales[] = {
37 Locale ("en_US"),
38 Locale ("fr_FR"),
39 Locale ("zh_CN"),
40 };
41
42 const char* filename = "sample.txt";
43 /* open a UTF-8 file for writing */
44 UFILE* f = u_fopen(filename, "w", NULL,"UTF-8");
45 UnicodeString dateReturned;
46 UErrorCode status =U_ZERO_ERROR;
47 Calendar *cal = Calendar::createInstance(status);
48 cal->set (1999,9,13,23,58,59);
49 UDate date = cal->getTime(status);
50 u_fprintf(f, "%-20S%-20S%-20S%-20S\n", UnicodeString("Skeleton").getTerminatedBuffer(),UnicodeString("en_US").getTerminatedBuffer(),UnicodeString("fr_FR").getTerminatedBuffer(),UnicodeString("zh_CN").getTerminatedBuffer());
51 for (int i=0;skeletons[i]!=NULL;i++) {
52 u_fprintf(f, "%-20S",skeletons[i].getTerminatedBuffer());
53 for (int j=0;j<sizeof(locales)/sizeof(locales[0]);j++) {
54 // create a DateTimePatternGenerator instance for given locale
55 DateTimePatternGenerator *dtfg= DateTimePatternGenerator::createInstance(locales[j],status);
56 // use getBestPattern method to get the best pattern for the given skeleton
57 UnicodeString pattern = dtfg->getBestPattern(skeletons[i],status);
58 // Constructs a SimpleDateFormat with the best pattern generated above and the given locale
59 SimpleDateFormat *sdf = new SimpleDateFormat(pattern,locales[j],status);
60 dateReturned.remove();
61 // Get the format of the given date
62 sdf->format(date,dateReturned,status);
63 /* write Unicode string to file */
64 u_fprintf(f, "%-20S", dateReturned.getTerminatedBuffer());
65 delete dtfg;
66 delete sdf;
67 }
68 u_fprintf(f,"\n");
69 }
70 /* close the file resource */
71 u_fclose(f);
72 delete cal;
73 //! [getBestPatternExample]
74 }
75
addPatternExample()76 static void addPatternExample() {
77
78 u_printf("========================================================================\n");
79 u_printf(" addPatternExample()\n");
80 u_printf("\n");
81 u_printf(" Use addPattern API to add new '. von' to existing pattern\n");
82 u_printf("========================================================================\n");
83 //! [addPatternExample]
84 UErrorCode status =U_ZERO_ERROR;
85 UnicodeString conflictingPattern,dateReturned, pattern;
86 Locale locale=Locale::getFrance();
87 Calendar *cal = Calendar::createInstance(status);
88 cal->set (1999,9,13,23,58,59);
89 UDate date = cal->getTime(status);
90 // Create an DateTimePatternGenerator instance for the given locale
91 DateTimePatternGenerator *dtfg= DateTimePatternGenerator::createInstance(locale,status);
92 SimpleDateFormat *sdf = new SimpleDateFormat(dtfg->getBestPattern("MMMMddHmm",status),locale,status);
93 // Add '. von' to the existing pattern
94 dtfg->addPattern("dd'. von' MMMM", true, conflictingPattern,status);
95 // Apply the new pattern
96 sdf->applyPattern(dtfg->getBestPattern("MMMMddHmm",status));
97 dateReturned = sdf->format(date, dateReturned, status);
98 pattern =sdf->toPattern(pattern);
99 u_printf("%s\n", "New Pattern for FRENCH: ");
100 u_printf("%S\n", pattern.getTerminatedBuffer());
101 u_printf("%s\n", "Date Time in new Pattern: ");
102 u_printf("%S\n", dateReturned.getTerminatedBuffer());
103 delete dtfg;
104 delete sdf;
105 delete cal;
106
107 //! [addPatternExample]
108 /* output of the sample code:
109 ************************************************************************************************
110 New Pattern for FRENCH: dd. 'von' MMMM HH:mm
111 Date Time in new Pattern: 13. von octobre 23:58
112
113 *************************************************************************************************/
114 }
115
replaceFieldTypesExample()116 static void replaceFieldTypesExample() {
117 // Use repalceFieldTypes API to replace zone 'zzzz' with 'vvvv'
118 u_printf("========================================================================\n");
119 u_printf(" replaceFieldTypeExample()\n");
120 u_printf("\n");
121 u_printf(" Use replaceFieldTypes API to replace zone 'zzzz' with 'vvvv'\n");
122 u_printf("========================================================================\n");
123 //! [replaceFieldTypesExample]
124 UFILE *out = u_finit(stdout, NULL, "UTF-8");
125 UErrorCode status =U_ZERO_ERROR;
126 UnicodeString pattern,dateReturned;
127 Locale locale =Locale::getFrance();
128 Calendar *cal = Calendar::createInstance(status);
129 cal->set (1999,9,13,23,58,59);
130 UDate date = cal->getTime(status);
131 TimeZone *zone = TimeZone::createTimeZone(UnicodeString("Europe/Paris"));
132 DateTimePatternGenerator *dtfg = DateTimePatternGenerator::createInstance(locale,status);
133 SimpleDateFormat *sdf = new SimpleDateFormat("EEEE d MMMM y HH:mm:ss zzzz",locale,status);
134 sdf->setTimeZone(*zone);
135 pattern = sdf->toPattern(pattern);
136 u_fprintf(out, "%S\n", UnicodeString("Pattern before replacement:").getTerminatedBuffer());
137 u_fprintf(out, "%S\n", pattern.getTerminatedBuffer());
138 dateReturned.remove();
139 dateReturned = sdf->format(date, dateReturned, status);
140 u_fprintf(out, "%S\n", UnicodeString("Date/Time format in fr_FR:").getTerminatedBuffer());
141 u_fprintf(out, "%S\n", dateReturned.getTerminatedBuffer());
142 // Replace zone "zzzz" in the pattern with "vvvv"
143 UnicodeString newPattern = dtfg->replaceFieldTypes(pattern, "vvvv", status);
144 // Apply the new pattern
145 sdf->applyPattern(newPattern);
146 dateReturned.remove();
147 dateReturned = sdf->format(date, dateReturned, status);
148 u_fprintf(out, "%S\n", UnicodeString("Pattern after replacement:").getTerminatedBuffer());
149 u_fprintf(out, "%S\n", newPattern.getTerminatedBuffer());
150 u_fprintf(out, "%S\n", UnicodeString("Date/Time format in fr_FR:").getTerminatedBuffer());
151 u_fprintf(out, "%S\n", dateReturned.getTerminatedBuffer());
152 delete sdf;
153 delete dtfg;
154 delete zone;
155 delete cal;
156 u_fclose(out);
157 //! [replaceFieldTypesExample]
158 }
159
main(int argc,char * argv[])160 int main (int argc, char* argv[])
161 {
162 getBestPatternExample();
163 addPatternExample();
164 replaceFieldTypesExample();
165 return 0;
166 }
167