2 *******************************************************************************
3 * Copyright (C) 2009-2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
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.
11 ********************************************************************************
14 #include "unicode/utypes.h"
16 #if !UCONFIG_NO_FORMATTING
18 #include "unicode/uniset.h"
19 #include "unicode/udat.h"
29 SimpleDateFormatStaticSets
*SimpleDateFormatStaticSets::gStaticSets
= NULL
;
31 SimpleDateFormatStaticSets::SimpleDateFormatStaticSets(UErrorCode
*status
)
32 : fDateIgnorables(NULL
),
33 fTimeIgnorables(NULL
),
34 fOtherIgnorables(NULL
)
36 fDateIgnorables
= new UnicodeSet(UNICODE_STRING("[-,./[:whitespace:]]", 20), *status
);
37 fTimeIgnorables
= new UnicodeSet(UNICODE_STRING("[-.:[:whitespace:]]", 19), *status
);
38 fOtherIgnorables
= new UnicodeSet(UNICODE_STRING("[:whitespace:]", 14), *status
);
40 // Check for null pointers
41 if (fDateIgnorables
== NULL
|| fTimeIgnorables
== NULL
|| fOtherIgnorables
== NULL
) {
42 goto ExitConstrDeleteAll
;
45 // Freeze all the sets
46 fDateIgnorables
->freeze();
47 fTimeIgnorables
->freeze();
48 fOtherIgnorables
->freeze();
50 return; // If we reached this point, everything is fine so just exit
52 ExitConstrDeleteAll
: // Remove all sets and return error
53 delete fDateIgnorables
; fDateIgnorables
= NULL
;
54 delete fTimeIgnorables
; fTimeIgnorables
= NULL
;
55 delete fOtherIgnorables
; fOtherIgnorables
= NULL
;
57 *status
= U_MEMORY_ALLOCATION_ERROR
;
61 SimpleDateFormatStaticSets::~SimpleDateFormatStaticSets() {
62 delete fDateIgnorables
; fDateIgnorables
= NULL
;
63 delete fTimeIgnorables
; fTimeIgnorables
= NULL
;
64 delete fOtherIgnorables
; fOtherIgnorables
= NULL
;
68 //------------------------------------------------------------------------------
70 // smpdtfmt_cleanup Memory cleanup function, free/delete all
71 // cached memory. Called by ICU's u_cleanup() function.
73 //------------------------------------------------------------------------------
75 SimpleDateFormatStaticSets::cleanup(void)
77 delete SimpleDateFormatStaticSets::gStaticSets
;
78 SimpleDateFormatStaticSets::gStaticSets
= NULL
;
84 static UBool U_CALLCONV
85 smpdtfmt_cleanup(void)
87 return SimpleDateFormatStaticSets::cleanup();
91 void SimpleDateFormatStaticSets::initSets(UErrorCode
*status
)
93 SimpleDateFormatStaticSets
*p
;
95 UMTX_CHECK(NULL
, gStaticSets
, p
);
97 p
= new SimpleDateFormatStaticSets(status
);
100 *status
= U_MEMORY_ALLOCATION_ERROR
;
104 if (U_FAILURE(*status
)) {
110 if (gStaticSets
== NULL
) {
120 ucln_i18n_registerCleanup(UCLN_I18N_SMPDTFMT
, smpdtfmt_cleanup
);
124 UnicodeSet
*SimpleDateFormatStaticSets::getIgnorables(UDateFormatField fieldIndex
)
126 UErrorCode status
= U_ZERO_ERROR
;
130 if (U_FAILURE(status
)) {
134 switch (fieldIndex
) {
135 case UDAT_YEAR_FIELD
:
136 case UDAT_MONTH_FIELD
:
137 case UDAT_DATE_FIELD
:
138 case UDAT_STANDALONE_DAY_FIELD
:
139 case UDAT_STANDALONE_MONTH_FIELD
:
140 return gStaticSets
->fDateIgnorables
;
142 case UDAT_HOUR_OF_DAY1_FIELD
:
143 case UDAT_HOUR_OF_DAY0_FIELD
:
144 case UDAT_MINUTE_FIELD
:
145 case UDAT_SECOND_FIELD
:
146 case UDAT_HOUR1_FIELD
:
147 case UDAT_HOUR0_FIELD
:
148 return gStaticSets
->fTimeIgnorables
;
151 return gStaticSets
->fOtherIgnorables
;
158 #endif // #if !UCONFIG_NO_FORMATTING