2 *******************************************************************************
3 * Copyright (C) 2009-2013, 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"
30 SimpleDateFormatStaticSets
*gStaticSets
= NULL
;
31 UInitOnce gSimpleDateFormatStaticSetsInitOnce
= U_INITONCE_INITIALIZER
;
33 SimpleDateFormatStaticSets::SimpleDateFormatStaticSets(UErrorCode
&status
)
34 : fDateIgnorables(NULL
),
35 fTimeIgnorables(NULL
),
36 fOtherIgnorables(NULL
)
38 fDateIgnorables
= new UnicodeSet(UNICODE_STRING("[-,./[:whitespace:]]", 20), status
);
39 fTimeIgnorables
= new UnicodeSet(UNICODE_STRING("[-.:[:whitespace:]]", 19), status
);
40 fOtherIgnorables
= new UnicodeSet(UNICODE_STRING("[:whitespace:]", 14), status
);
42 // Check for null pointers
43 if (fDateIgnorables
== NULL
|| fTimeIgnorables
== NULL
|| fOtherIgnorables
== NULL
) {
44 goto ExitConstrDeleteAll
;
47 // Freeze all the sets
48 fDateIgnorables
->freeze();
49 fTimeIgnorables
->freeze();
50 fOtherIgnorables
->freeze();
52 return; // If we reached this point, everything is fine so just exit
54 ExitConstrDeleteAll
: // Remove all sets and return error
55 delete fDateIgnorables
; fDateIgnorables
= NULL
;
56 delete fTimeIgnorables
; fTimeIgnorables
= NULL
;
57 delete fOtherIgnorables
; fOtherIgnorables
= NULL
;
59 status
= U_MEMORY_ALLOCATION_ERROR
;
63 SimpleDateFormatStaticSets::~SimpleDateFormatStaticSets() {
64 delete fDateIgnorables
; fDateIgnorables
= NULL
;
65 delete fTimeIgnorables
; fTimeIgnorables
= NULL
;
66 delete fOtherIgnorables
; fOtherIgnorables
= NULL
;
70 //------------------------------------------------------------------------------
72 // smpdtfmt_cleanup Memory cleanup function, free/delete all
73 // cached memory. Called by ICU's u_cleanup() function.
75 //------------------------------------------------------------------------------
77 SimpleDateFormatStaticSets::cleanup(void)
81 gSimpleDateFormatStaticSetsInitOnce
.reset();
86 static UBool U_CALLCONV
87 smpdtfmt_cleanup(void)
89 return SimpleDateFormatStaticSets::cleanup();
92 static void U_CALLCONV
smpdtfmt_initSets(UErrorCode
&status
) {
93 ucln_i18n_registerCleanup(UCLN_I18N_SMPDTFMT
, smpdtfmt_cleanup
);
94 U_ASSERT(gStaticSets
== NULL
);
95 gStaticSets
= new SimpleDateFormatStaticSets(status
);
96 if (gStaticSets
== NULL
) {
97 status
= U_MEMORY_ALLOCATION_ERROR
;
104 UnicodeSet
*SimpleDateFormatStaticSets::getIgnorables(UDateFormatField fieldIndex
)
106 UErrorCode status
= U_ZERO_ERROR
;
107 umtx_initOnce(gSimpleDateFormatStaticSetsInitOnce
, &smpdtfmt_initSets
, status
);
108 if (U_FAILURE(status
)) {
112 switch (fieldIndex
) {
113 case UDAT_YEAR_FIELD
:
114 case UDAT_MONTH_FIELD
:
115 case UDAT_DATE_FIELD
:
116 case UDAT_STANDALONE_DAY_FIELD
:
117 case UDAT_STANDALONE_MONTH_FIELD
:
118 return gStaticSets
->fDateIgnorables
;
120 case UDAT_HOUR_OF_DAY1_FIELD
:
121 case UDAT_HOUR_OF_DAY0_FIELD
:
122 case UDAT_MINUTE_FIELD
:
123 case UDAT_SECOND_FIELD
:
124 case UDAT_HOUR1_FIELD
:
125 case UDAT_HOUR0_FIELD
:
126 return gStaticSets
->fTimeIgnorables
;
129 return gStaticSets
->fOtherIgnorables
;
135 #endif // #if !UCONFIG_NO_FORMATTING