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