4 // Copyright (C) 2009, International Business Machines Corporation and others.
5 // All Rights Reserved.
7 // This file contains the class SimpleDateFormatStaticSets
9 // SimpleDateFormatStaticSets holds the UnicodeSets that are needed for lenient
10 // parsing of literal characters in date/time strings.
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_FORMATTING
17 #include "unicode/uniset.h"
18 #include "unicode/udat.h"
28 SimpleDateFormatStaticSets
*SimpleDateFormatStaticSets::gStaticSets
= NULL
;
30 SimpleDateFormatStaticSets::SimpleDateFormatStaticSets(UErrorCode
*status
)
31 : fDateIgnorables(NULL
),
32 fTimeIgnorables(NULL
),
33 fOtherIgnorables(NULL
)
35 fDateIgnorables
= new UnicodeSet("[-,./[:whitespace:]]", *status
);
36 fTimeIgnorables
= new UnicodeSet("[-.:[:whitespace:]]", *status
);
37 fOtherIgnorables
= new UnicodeSet("[:whitespace:]", *status
);
39 // Check for null pointers
40 if (fDateIgnorables
== NULL
|| fTimeIgnorables
== NULL
|| fOtherIgnorables
== NULL
) {
41 goto ExitConstrDeleteAll
;
44 // Freeze all the sets
45 fDateIgnorables
->freeze();
46 fTimeIgnorables
->freeze();
47 fOtherIgnorables
->freeze();
49 return; // If we reached this point, everything is fine so just exit
51 ExitConstrDeleteAll
: // Remove all sets and return error
52 delete fDateIgnorables
; fDateIgnorables
= NULL
;
53 delete fTimeIgnorables
; fTimeIgnorables
= NULL
;
54 delete fOtherIgnorables
; fOtherIgnorables
= NULL
;
56 *status
= U_MEMORY_ALLOCATION_ERROR
;
60 SimpleDateFormatStaticSets::~SimpleDateFormatStaticSets() {
61 delete fDateIgnorables
; fDateIgnorables
= NULL
;
62 delete fTimeIgnorables
; fTimeIgnorables
= NULL
;
63 delete fOtherIgnorables
; fOtherIgnorables
= NULL
;
67 //------------------------------------------------------------------------------
69 // smpdtfmt_cleanup Memory cleanup function, free/delete all
70 // cached memory. Called by ICU's u_cleanup() function.
72 //------------------------------------------------------------------------------
74 SimpleDateFormatStaticSets::cleanup(void)
76 delete SimpleDateFormatStaticSets::gStaticSets
;
77 SimpleDateFormatStaticSets::gStaticSets
= NULL
;
83 static UBool U_CALLCONV
84 smpdtfmt_cleanup(void)
86 return SimpleDateFormatStaticSets::cleanup();
90 void SimpleDateFormatStaticSets::initSets(UErrorCode
*status
)
92 SimpleDateFormatStaticSets
*p
;
94 UMTX_CHECK(NULL
, gStaticSets
, p
);
96 p
= new SimpleDateFormatStaticSets(status
);
99 *status
= U_MEMORY_ALLOCATION_ERROR
;
103 if (U_FAILURE(*status
)) {
109 if (gStaticSets
== NULL
) {
119 ucln_i18n_registerCleanup(UCLN_I18N_SMPDTFMT
, smpdtfmt_cleanup
);
123 UnicodeSet
*SimpleDateFormatStaticSets::getIgnorables(UDateFormatField fieldIndex
)
125 UErrorCode status
= U_ZERO_ERROR
;
129 if (U_FAILURE(status
)) {
133 switch (fieldIndex
) {
134 case UDAT_YEAR_FIELD
:
135 case UDAT_MONTH_FIELD
:
136 case UDAT_DATE_FIELD
:
137 case UDAT_STANDALONE_DAY_FIELD
:
138 case UDAT_STANDALONE_MONTH_FIELD
:
139 return gStaticSets
->fDateIgnorables
;
141 case UDAT_HOUR_OF_DAY1_FIELD
:
142 case UDAT_HOUR_OF_DAY0_FIELD
:
143 case UDAT_MINUTE_FIELD
:
144 case UDAT_SECOND_FIELD
:
145 case UDAT_HOUR1_FIELD
:
146 case UDAT_HOUR0_FIELD
:
147 return gStaticSets
->fTimeIgnorables
;
150 return gStaticSets
->fOtherIgnorables
;
157 #endif // #if !UCONFIG_NO_FORMATTING