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