1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2009-2013, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
9 * This file contains the class SimpleDateFormatStaticSets
11 * SimpleDateFormatStaticSets holds the UnicodeSets that are needed for lenient
12 * parsing of literal characters in date/time strings.
13 ********************************************************************************
16 #include "unicode/utypes.h"
18 #if !UCONFIG_NO_FORMATTING
20 #include "unicode/uniset.h"
21 #include "unicode/udat.h"
32 SimpleDateFormatStaticSets
*gStaticSets
= NULL
;
33 UInitOnce gSimpleDateFormatStaticSetsInitOnce
= U_INITONCE_INITIALIZER
;
35 SimpleDateFormatStaticSets::SimpleDateFormatStaticSets(UErrorCode
&status
)
36 : fDateIgnorables(NULL
),
37 fTimeIgnorables(NULL
),
38 fOtherIgnorables(NULL
)
40 fDateIgnorables
= new UnicodeSet(UNICODE_STRING("[-,./[:whitespace:]]", 20), status
);
41 fTimeIgnorables
= new UnicodeSet(UNICODE_STRING("[-.:[:whitespace:]]", 19), status
);
42 fOtherIgnorables
= new UnicodeSet(UNICODE_STRING("[:whitespace:]", 14), status
);
44 // Check for null pointers
45 if (fDateIgnorables
== NULL
|| fTimeIgnorables
== NULL
|| fOtherIgnorables
== NULL
) {
46 goto ExitConstrDeleteAll
;
49 // Freeze all the sets
50 fDateIgnorables
->freeze();
51 fTimeIgnorables
->freeze();
52 fOtherIgnorables
->freeze();
54 return; // If we reached this point, everything is fine so just exit
56 ExitConstrDeleteAll
: // Remove all sets and return error
57 delete fDateIgnorables
; fDateIgnorables
= NULL
;
58 delete fTimeIgnorables
; fTimeIgnorables
= NULL
;
59 delete fOtherIgnorables
; fOtherIgnorables
= NULL
;
61 status
= U_MEMORY_ALLOCATION_ERROR
;
65 SimpleDateFormatStaticSets::~SimpleDateFormatStaticSets() {
66 delete fDateIgnorables
; fDateIgnorables
= NULL
;
67 delete fTimeIgnorables
; fTimeIgnorables
= NULL
;
68 delete fOtherIgnorables
; fOtherIgnorables
= NULL
;
72 //------------------------------------------------------------------------------
74 // smpdtfmt_cleanup Memory cleanup function, free/delete all
75 // cached memory. Called by ICU's u_cleanup() function.
77 //------------------------------------------------------------------------------
79 SimpleDateFormatStaticSets::cleanup(void)
83 gSimpleDateFormatStaticSetsInitOnce
.reset();
88 static UBool U_CALLCONV
89 smpdtfmt_cleanup(void)
91 return SimpleDateFormatStaticSets::cleanup();
94 static void U_CALLCONV
smpdtfmt_initSets(UErrorCode
&status
) {
95 ucln_i18n_registerCleanup(UCLN_I18N_SMPDTFMT
, smpdtfmt_cleanup
);
96 U_ASSERT(gStaticSets
== NULL
);
97 gStaticSets
= new SimpleDateFormatStaticSets(status
);
98 if (gStaticSets
== NULL
) {
99 status
= U_MEMORY_ALLOCATION_ERROR
;
106 UnicodeSet
*SimpleDateFormatStaticSets::getIgnorables(UDateFormatField fieldIndex
)
108 UErrorCode status
= U_ZERO_ERROR
;
109 umtx_initOnce(gSimpleDateFormatStaticSetsInitOnce
, &smpdtfmt_initSets
, status
);
110 if (U_FAILURE(status
)) {
114 switch (fieldIndex
) {
115 case UDAT_YEAR_FIELD
:
116 case UDAT_MONTH_FIELD
:
117 case UDAT_DATE_FIELD
:
118 case UDAT_STANDALONE_DAY_FIELD
:
119 case UDAT_STANDALONE_MONTH_FIELD
:
120 return gStaticSets
->fDateIgnorables
;
122 case UDAT_HOUR_OF_DAY1_FIELD
:
123 case UDAT_HOUR_OF_DAY0_FIELD
:
124 case UDAT_MINUTE_FIELD
:
125 case UDAT_SECOND_FIELD
:
126 case UDAT_HOUR1_FIELD
:
127 case UDAT_HOUR0_FIELD
:
128 return gStaticSets
->fTimeIgnorables
;
131 return gStaticSets
->fOtherIgnorables
;
137 #endif // #if !UCONFIG_NO_FORMATTING