1 /******************************************************************************
2 * Copyright (C) 2008-2014, International Business Machines Corporation and
3 * others. All Rights Reserved.
4 *******************************************************************************
5 */
6 //! [dtitvfmtPreDefined1]
7 #include <iostream>
8 #include "unicode/dtitvfmt.h"
9 #include "unicode/ustdio.h"
10 //! [dtitvfmtPreDefined1]
11
12 using namespace std;
13
dtitvfmtPreDefined()14 static void dtitvfmtPreDefined() {
15
16 u_printf("===============================================================================\n");
17 u_printf(" dtitvfmtPreDefined()\n");
18 u_printf("\n");
19 u_printf(" Use DateIntervalFormat to get date interval format for pre-defined skeletons:\n");
20 u_printf(" yMMMd, MMMMd, jm per locale\n");
21 u_printf("===============================================================================\n");
22
23 //! [dtitvfmtPreDefined]
24 UFILE *out = u_finit(stdout, NULL, "UTF-8");
25 UErrorCode status =U_ZERO_ERROR;
26 // create 3 Date Intervals
27 UnicodeString data[] = {
28 UnicodeString("2007-10-10 10:10:10"),
29 UnicodeString("2008-10-10 10:10:10"),
30 UnicodeString("2008-11-10 10:10:10"),
31 UnicodeString("2008-11-10 15:10:10")
32 };
33 Calendar *cal = Calendar::createInstance(status);
34 cal->set(2007,10,10,10,10,10);
35 UDate date1 = cal->getTime(status);
36 cal->set(2008,10,10,10,10,10);
37 UDate date2 = cal->getTime(status);
38 cal->set(2008,11,10,10,10,10);
39 UDate date3 = cal->getTime(status);
40 cal->set(2008,11,10,15,10,10);
41 UDate date4 = cal->getTime(status);
42 DateInterval* dtitvsample[] = {
43 new DateInterval(date1,date2),
44 new DateInterval(date2,date3),
45 new DateInterval(date3,date4),
46 };
47 UnicodeString skeletons[] = {
48 UnicodeString("yMMMd"),
49 UnicodeString("MMMMd"),
50 UnicodeString("jm"),
51 0,
52 };
53 u_fprintf(out,"%-10s%-22s%-22s%-35s%-35s\n", "Skeleton","from","to","Date Interval in en_US","Date Interval in Ja");
54 int i=0;
55 UnicodeString formatEn,formatJa;
56 FieldPosition pos=0;
57 for (int j=0;skeletons[j]!=NULL ;j++) {
58 u_fprintf(out,"%-10S%-22S%-22S",skeletons[j].getTerminatedBuffer(),data[i].getTerminatedBuffer(),data[i+1].getTerminatedBuffer());
59 //create a DateIntervalFormat instance for given skeleton, locale
60 DateIntervalFormat* dtitvfmtEn = DateIntervalFormat::createInstance(skeletons[j], Locale::getEnglish(),status);
61 DateIntervalFormat* dtitvfmtJa = DateIntervalFormat::createInstance(skeletons[j], Locale::getJapanese(),status);
62 formatEn.remove();
63 formatJa.remove();
64 //get the DateIntervalFormat
65 dtitvfmtEn->format(dtitvsample[i],formatEn,pos,status);
66 dtitvfmtJa->format(dtitvsample[i],formatJa,pos,status);
67 u_fprintf(out,"%-35S%-35S\n", formatEn.getTerminatedBuffer(),formatJa.getTerminatedBuffer());
68 delete dtitvfmtEn;
69 delete dtitvfmtJa;
70 i++;
71 }
72 u_fclose(out);
73 //! [dtitvfmtPreDefined]
74 }
75
dtitvfmtCustomized()76 static void dtitvfmtCustomized() {
77
78 u_printf("===============================================================================\n");
79 u_printf("\n");
80 u_printf(" dtitvfmtCustomized()\n");
81 u_printf("\n");
82 u_printf(" Use DateIntervalFormat to create customized date interval format for yMMMd, Hm");
83 u_printf("\n");
84 u_printf("================================================================================\n");
85 //! [dtitvfmtCustomized]
86 UFILE *out = u_finit(stdout, NULL, "UTF-8");
87 UErrorCode status =U_ZERO_ERROR;
88 UnicodeString data[] = {
89 UnicodeString("2007-9-10 10:10:10"),
90 UnicodeString("2007-10-10 10:10:10"),
91 UnicodeString("2007-10-10 22:10:10")
92 };
93 // to create 2 Date Intervals
94 Calendar *cal1 = Calendar::createInstance(status);
95 cal1->set(2007,9,10,10,10,10);
96 Calendar *cal2 = Calendar::createInstance(status);
97 cal2->set(2007,10,10,10,10,10);
98 Calendar *cal3 = Calendar::createInstance(status);
99 cal3->set(2007,10,10,22,10,10);
100 DateInterval* dtitvsample[] = {
101 new DateInterval(cal1->getTime(status),cal2->getTime(status)),
102 new DateInterval(cal2->getTime(status),cal3->getTime(status))
103 };
104 UnicodeString skeletons[] = {
105 UnicodeString("yMMMd"),
106 UnicodeString("Hm"),
107 0,
108 };
109 u_printf("%-10s%-22s%-22s%-45s%-35s\n", "Skeleton", "from","to", "Date Interval in en_US","Date Interval in Ja");
110 // Create an empty DateIntervalInfo object
111 DateIntervalInfo dtitvinf = DateIntervalInfo(status);
112 // Set Date Time internal pattern for MONTH, HOUR_OF_DAY
113 dtitvinf.setIntervalPattern("yMMMd", UCAL_MONTH, "y 'Diff' MMM d --- MMM d",status);
114 dtitvinf.setIntervalPattern("Hm", UCAL_HOUR_OF_DAY, "yyyy MMM d HH:mm ~ HH:mm",status);
115 // Set fallback interval pattern
116 dtitvinf.setFallbackIntervalPattern("{0} ~~~ {1}",status);
117 // Get the DateIntervalFormat with the custom pattern
118 UnicodeString formatEn,formatJa;
119 FieldPosition pos=0;
120 for (int i=0;i<2;i++){
121 for (int j=0;skeletons[j]!=NULL;j++) {
122 u_fprintf(out,"%-10S%-22S%-22S", skeletons[i].getTerminatedBuffer(),data[j].getTerminatedBuffer(), data[j+1].getTerminatedBuffer());
123 DateIntervalFormat* dtitvfmtEn = DateIntervalFormat::createInstance(skeletons[i],Locale::getEnglish(),dtitvinf,status);
124 DateIntervalFormat* dtitvfmtJa = DateIntervalFormat::createInstance(skeletons[i],Locale::getJapanese(),dtitvinf,status);
125 formatEn.remove();
126 formatJa.remove();
127 dtitvfmtEn->format(dtitvsample[j],formatEn,pos,status);
128 dtitvfmtJa->format(dtitvsample[j],formatJa,pos,status);
129 u_fprintf(out,"%-45S%-35S\n", formatEn.getTerminatedBuffer(),formatJa.getTerminatedBuffer());
130 }
131 }
132 u_fclose(out);
133 //! [dtitvfmtCustomized]
134 }
135
main(int argc,char * argv[])136 int main (int argc, char* argv[])
137 {
138 dtitvfmtPreDefined();
139 dtitvfmtCustomized();
140 return 0;
141 }
142