本文整理汇总了C++中SimpleDateFormat::toPattern方法的典型用法代码示例。如果您正苦于以下问题:C++ SimpleDateFormat::toPattern方法的具体用法?C++ SimpleDateFormat::toPattern怎么用?C++ SimpleDateFormat::toPattern使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SimpleDateFormat
的用法示例。
在下文中一共展示了SimpleDateFormat::toPattern方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: logln
void
IntlTestDateFormat::describeTest()
{
// Assume it's a SimpleDateFormat and get some info
SimpleDateFormat *s = (SimpleDateFormat*)fFormat;
UnicodeString str;
logln(fTestName + " Pattern " + s->toPattern(str));
}
示例2: replaceFieldTypesExample
static void replaceFieldTypesExample() {
// Use repalceFieldTypes API to replace zone 'zzzz' with 'vvvv'
u_printf("========================================================================\n");
u_printf(" replaceFieldTypeExample()\n");
u_printf("\n");
u_printf(" Use replaceFieldTypes API to replace zone 'zzzz' with 'vvvv'\n");
u_printf("========================================================================\n");
//! [replaceFieldTypesExample]
UFILE *out = u_finit(stdout, NULL, "UTF-8");
UErrorCode status =U_ZERO_ERROR;
UnicodeString pattern,dateReturned;
Locale locale =Locale::getFrance();
Calendar *cal = Calendar::createInstance(status);
cal->set (1999,9,13,23,58,59);
UDate date = cal->getTime(status);
TimeZone *zone = TimeZone::createTimeZone(UnicodeString("Europe/Paris"));
DateTimePatternGenerator *dtfg = DateTimePatternGenerator::createInstance(locale,status);
SimpleDateFormat *sdf = new SimpleDateFormat("EEEE d MMMM y HH:mm:ss zzzz",locale,status);
sdf->setTimeZone(*zone);
pattern = sdf->toPattern(pattern);
u_fprintf(out, "%S\n", UnicodeString("Pattern before replacement:").getTerminatedBuffer());
u_fprintf(out, "%S\n", pattern.getTerminatedBuffer());
dateReturned.remove();
dateReturned = sdf->format(date, dateReturned, status);
u_fprintf(out, "%S\n", UnicodeString("Date/Time format in fr_FR:").getTerminatedBuffer());
u_fprintf(out, "%S\n", dateReturned.getTerminatedBuffer());
// Replace zone "zzzz" in the pattern with "vvvv"
UnicodeString newPattern = dtfg->replaceFieldTypes(pattern, "vvvv", status);
// Apply the new pattern
sdf->applyPattern(newPattern);
dateReturned.remove();
dateReturned = sdf->format(date, dateReturned, status);
u_fprintf(out, "%S\n", UnicodeString("Pattern after replacement:").getTerminatedBuffer());
u_fprintf(out, "%S\n", newPattern.getTerminatedBuffer());
u_fprintf(out, "%S\n", UnicodeString("Date/Time format in fr_FR:").getTerminatedBuffer());
u_fprintf(out, "%S\n", dateReturned.getTerminatedBuffer());
delete sdf;
delete dtfg;
delete zone;
delete cal;
u_fclose(out);
//! [replaceFieldTypesExample]
/* output of the sample code:
*************************************************************************************************
Pattern before replacement:
EEEE d MMMM y HH:mm:ss zzzz
Date/Time format in fr_FR:
jeudi 14 octobre 1999 05:58:59 heure avancée d’Europe centrale
Pattern after replacement:
EEEE d MMMM y HH:mm:ss vvvv
Date/Time format in fr_FR:
jeudi 14 octobre 1999 05:58:59 heure de l’Europe centrale
*************************************************************************************************/
}
示例3: getDatePattern
std::string GlobalizationNDK::getDatePattern(const std::string& args)
{
DateFormat::EStyle dstyle = DateFormat::kShort, tstyle = DateFormat::kShort;
if (!args.empty()) {
Json::Reader reader;
Json::Value root;
bool parse = reader.parse(args, root);
if (!parse) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDatePattern: invalid json data: %s",
args.c_str());
return errorInJson(PARSING_ERROR, "Parameters not valid json format!");
}
Json::Value options = root["options"];
std::string error;
if (!handleDateOptions(options, dstyle, tstyle, error))
return errorInJson(PARSING_ERROR, error);
}
UErrorCode status = U_ZERO_ERROR;
const Locale& loc = Locale::getDefault();
DateFormat* df = DateFormat::createDateTimeInstance(dstyle, tstyle, loc);
if (!df) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDatePattern: unable to create DateFormat instance!");
return errorInJson(UNKNOWN_ERROR, "Unable to create DateFormat instance!");
}
std::auto_ptr<DateFormat> deleter(df);
if (df->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDatePattern: DateFormat instance not SimpleDateFormat!");
return errorInJson(UNKNOWN_ERROR, "DateFormat instance not SimpleDateFormat!");
}
SimpleDateFormat* sdf = (SimpleDateFormat*) df;
UnicodeString pt;
sdf->toPattern(pt);
std::string ptUtf8;
pt.toUTF8String(ptUtf8);
const TimeZone& tz = sdf->getTimeZone();
UnicodeString tzName;
tz.getDisplayName(tzName);
std::string tzUtf8;
tzName.toUTF8String(tzUtf8);
int utc_offset = tz.getRawOffset() / 1000; // UTC_OFFSET in seconds.
int dst_offset = tz.getDSTSavings() / 1000; // DST_OFFSET in seconds;
return resultInJson(ptUtf8, tzUtf8, utc_offset, dst_offset);
}
示例4: DateFormat
RelativeDateFormat::RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatStyle dateStyle,
const Locale& locale, UErrorCode& status) :
DateFormat(), fDateTimeFormatter(NULL), fDatePattern(), fTimePattern(), fCombinedFormat(NULL),
fDateStyle(dateStyle), fLocale(locale), fDatesLen(0), fDates(NULL),
fCombinedHasDateAtStart(FALSE), fCapitalizationInfoSet(FALSE),
fCapitalizationOfRelativeUnitsForUIListMenu(FALSE), fCapitalizationOfRelativeUnitsForStandAlone(FALSE),
fCapitalizationBrkIter(NULL)
{
if(U_FAILURE(status) ) {
return;
}
if (timeStyle < UDAT_NONE || timeStyle > UDAT_SHORT) {
// don't support other time styles (e.g. relative styles), for now
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
UDateFormatStyle baseDateStyle = (dateStyle > UDAT_SHORT)? (UDateFormatStyle)(dateStyle & ~UDAT_RELATIVE): dateStyle;
DateFormat * df;
// Get fDateTimeFormatter from either date or time style (does not matter, we will override the pattern).
// We do need to get separate patterns for the date & time styles.
if (baseDateStyle != UDAT_NONE) {
df = createDateInstance((EStyle)baseDateStyle, locale);
fDateTimeFormatter=dynamic_cast<SimpleDateFormat *>(df);
if (fDateTimeFormatter == NULL) {
status = U_UNSUPPORTED_ERROR;
return;
}
fDateTimeFormatter->toPattern(fDatePattern);
if (timeStyle != UDAT_NONE) {
df = createTimeInstance((EStyle)timeStyle, locale);
SimpleDateFormat *sdf = dynamic_cast<SimpleDateFormat *>(df);
if (sdf != NULL) {
sdf->toPattern(fTimePattern);
delete sdf;
}
}
} else {
// does not matter whether timeStyle is UDAT_NONE, we need something for fDateTimeFormatter
df = createTimeInstance((EStyle)timeStyle, locale);
fDateTimeFormatter=dynamic_cast<SimpleDateFormat *>(df);
if (fDateTimeFormatter == NULL) {
status = U_UNSUPPORTED_ERROR;
delete df;
return;
}
fDateTimeFormatter->toPattern(fTimePattern);
}
// Initialize the parent fCalendar, so that parse() works correctly.
initializeCalendar(NULL, locale, status);
loadDates(status);
}
示例5: pos
/**
* @bug 4029195
*/
void DateFormatRegressionTest::Test4029195(void)
{
UErrorCode status = U_ZERO_ERROR;
UDate today = Calendar::getNow();
logln((UnicodeString) "today: " + today);
SimpleDateFormat *sdf = (SimpleDateFormat*) DateFormat::createDateInstance();
if (failure(status, "SimpleDateFormat::createDateInstance")) {
return;
}
UnicodeString pat;
if(sdf == NULL){
dataerrln("Error calling DateFormat::createDateTimeInstance");
return;
}
pat = sdf->toPattern(pat);
logln("pattern: " + pat);
UnicodeString fmtd;
FieldPosition pos(FieldPosition::DONT_CARE);
fmtd = sdf->format(today, fmtd, pos);
logln("today: " + fmtd);
sdf->applyPattern("G yyyy DDD");
UnicodeString todayS;
todayS = sdf->format(today, todayS, pos);
logln("today: " + todayS);
//try {
today = sdf->parse(todayS, status);
failure(status, "sdf->parse");
logln((UnicodeString)"today date: " + today);
/*} catch(Exception e) {
logln("Error reparsing date: " + e.getMessage());
}*/
//try {
UnicodeString rt;
rt = sdf->format(sdf->parse(todayS, status), rt, pos);
failure(status, "sdf->parse");
logln("round trip: " + rt);
if(rt != todayS)
errln("Fail: Want " + todayS + " Got " + rt);
/*}
catch (ParseException e) {
errln("Fail: " + e);
e.printStackTrace();
}*/
delete sdf;
}
示例6:
UnicodeString&
RelativeDateFormat::toPatternTime(UnicodeString& result, UErrorCode& status) const
{
if (!U_FAILURE(status)) {
result.remove();
if ( fTimeFormat ) {
SimpleDateFormat* sdtfmt = dynamic_cast<SimpleDateFormat*>(fTimeFormat);
if (sdtfmt != NULL) {
sdtfmt->toPattern(result);
} else {
status = U_UNSUPPORTED_ERROR;
}
}
}
return result;
}
示例7: stringConverter
bool
BCountry::TimeFormat(BString& format, bool longFormat) const
{
icu_4_2::DateFormat* dateFormatter;
dateFormatter = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter;
SimpleDateFormat* dateFormatterImpl
= static_cast<SimpleDateFormat*>(dateFormatter);
UnicodeString ICUString;
ICUString = dateFormatterImpl->toPattern(ICUString);
BStringByteSink stringConverter(&format);
ICUString.toUTF8(stringConverter);
return true;
}
示例8: InvokeCallbackForDatePattern
/*
Function:
InvokeCallbackForDatePattern
Gets the ICU date pattern for the specified locale and EStyle and invokes the callback with the result.
*/
bool InvokeCallbackForDatePattern(Locale& locale, DateFormat::EStyle style, EnumCalendarInfoCallback callback, const void* context)
{
LocalPointer<DateFormat> dateFormat(DateFormat::createDateInstance(style, locale));
if (dateFormat.isNull())
return false;
// cast to SimpleDateFormat so we can call toPattern()
SimpleDateFormat* sdf = dynamic_cast<SimpleDateFormat*>(dateFormat.getAlias());
if (sdf == NULL)
return false;
UnicodeString pattern;
sdf->toPattern(pattern);
callback(pattern.getTerminatedBuffer(), context);
return true;
}
示例9: stringConverter
bool
BCountry::TimeFormat(BString& format, bool longFormat) const
{
icu_4_2::DateFormat* dateFormatter;
dateFormatter = DateFormat::createTimeInstance(
longFormat ? DateFormat::FULL : DateFormat::SHORT,
*fICULocale);
SimpleDateFormat* dateFormatterImpl
= static_cast<SimpleDateFormat*>(dateFormatter);
UnicodeString ICUString;
ICUString = dateFormatterImpl->toPattern(ICUString);
BStringByteSink stringConverter(&format);
ICUString.toUTF8(stringConverter);
return true;
}
示例10: stringConverter
status_t
BFormattingConventions::GetDateTimeFormat(BDateFormatStyle dateStyle,
BTimeFormatStyle timeStyle, BString& outFormat) const
{
if (dateStyle < 0 || dateStyle >= B_DATE_FORMAT_STYLE_COUNT)
return B_BAD_VALUE;
if (timeStyle < 0 || timeStyle >= B_TIME_FORMAT_STYLE_COUNT)
return B_BAD_VALUE;
outFormat = fExplicitDateTimeFormats[dateStyle][timeStyle].Length()
? fExplicitDateTimeFormats[dateStyle][timeStyle]
: fCachedDateTimeFormats[dateStyle][timeStyle];
if (outFormat.Length() > 0)
return B_OK;
ObjectDeleter<DateFormat> dateFormatter(
DateFormat::createDateTimeInstance((DateFormat::EStyle)dateStyle,
(DateFormat::EStyle)timeStyle, *fICULocale));
if (dateFormatter.Get() == NULL)
return B_NO_MEMORY;
SimpleDateFormat* dateFormatterImpl
= static_cast<SimpleDateFormat*>(dateFormatter.Get());
UnicodeString icuString;
dateFormatterImpl->toPattern(icuString);
BStringByteSink stringConverter(&outFormat);
icuString.toUTF8(stringConverter);
CoerceFormatForClock(outFormat);
if (dateStyle != B_FULL_DATE_FORMAT) {
// use abbreviated timezone in short timezone format
CoerceFormatToAbbreviatedTimezone(outFormat);
}
fCachedDateTimeFormats[dateStyle][timeStyle] = outFormat;
return B_OK;
}
示例11: IllegalArgumentException
string DateTimeFormat::StyleFormatter::getPattern(Locale *locale) {
DateFormat *f = NULL;
switch (iType) {
case DATE:
f = DateFormat::getDateInstance(iDateStyle, locale);
break;
case TIME:
f = DateFormat::getTimeInstance(iTimeStyle, locale);
break;
case DATETIME:
f = DateFormat::getDateTimeInstance(iDateStyle, iTimeStyle, locale);
break;
}
SimpleDateFormat *sdf = dynamic_cast<SimpleDateFormat*>(f);
if (sdf != 0) {
throw IllegalArgumentException("No datetime pattern for locale: " + locale);
}
return sdf->toPattern();
}
示例12: addPatternExample
static void addPatternExample() {
u_printf("========================================================================\n");
u_printf(" addPatternExample()\n");
u_printf("\n");
u_printf(" Use addPattern API to add new '. von' to existing pattern\n");
u_printf("========================================================================\n");
//! [addPatternExample]
UErrorCode status =U_ZERO_ERROR;
UnicodeString conflictingPattern,dateReturned, pattern;
Locale locale=Locale::getFrance();
Calendar *cal = Calendar::createInstance(status);
cal->set (1999,9,13,23,58,59);
UDate date = cal->getTime(status);
// Create an DateTimePatternGenerator instance for the given locale
DateTimePatternGenerator *dtfg= DateTimePatternGenerator::createInstance(locale,status);
SimpleDateFormat *sdf = new SimpleDateFormat(dtfg->getBestPattern("MMMMddHmm",status),locale,status);
// Add '. von' to the existing pattern
dtfg->addPattern("dd'. von' MMMM", true, conflictingPattern,status);
// Apply the new pattern
sdf->applyPattern(dtfg->getBestPattern("MMMMddHmm",status));
dateReturned = sdf->format(date, dateReturned, status);
pattern =sdf->toPattern(pattern);
u_printf("%s\n", "New Pattern for FRENCH: ");
u_printf("%S\n", pattern.getTerminatedBuffer());
u_printf("%s\n", "Date Time in new Pattern: ");
u_printf("%S\n", dateReturned.getTerminatedBuffer());
delete dtfg;
delete sdf;
delete cal;
//! [addPatternExample]
/* output of the sample code:
************************************************************************************************
New Pattern for FRENCH: dd. 'von' MMMM HH:mm
Date Time in new Pattern: 13. von octobre 23:58
*************************************************************************************************/
}
示例13: testAPI
//.........这里部分代码省略.........
return;
}
format->setTimeZone(*zone);
UnicodeString dateReturned, expectedResult;
dateReturned.remove();
dateReturned = format->format(sampleDate, dateReturned, status);
expectedResult=UnicodeString("14. Okt 8:58", -1, US_INV);
if ( dateReturned != expectedResult ) {
errln("ERROR: Simple test in getBestPattern with Locale::getGermany()).");
}
// add new pattern
status = U_ZERO_ERROR;
conflictingStatus = gen->addPattern(UnicodeString("d'. von' MMMM", -1, US_INV), true, conflictingPattern, status);
if (U_FAILURE(status)) {
errln("ERROR: Could not addPattern - d\'. von\' MMMM");
}
status = U_ZERO_ERROR;
UnicodeString testPattern=gen->getBestPattern(UnicodeString("MMMMdd"), status);
testPattern=gen->getBestPattern(UnicodeString("MMMddHmm"), status);
format->applyPattern(gen->getBestPattern(UnicodeString("MMMMddHmm"), status));
dateReturned.remove();
dateReturned = format->format(sampleDate, dateReturned, status);
expectedResult=UnicodeString("14. von Oktober 8:58", -1, US_INV);
if ( dateReturned != expectedResult ) {
errln("ERROR: Simple test addPattern failed!: d\'. von\' MMMM ");
}
delete format;
// get a pattern and modify it
format = (SimpleDateFormat *)DateFormat::createDateTimeInstance(DateFormat::kFull, DateFormat::kFull,
deLocale);
format->setTimeZone(*zone);
UnicodeString pattern;
pattern = format->toPattern(pattern);
dateReturned.remove();
dateReturned = format->format(sampleDate, dateReturned, status);
expectedResult=CharsToUnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 Mitteleurop\\u00E4ische Sommerzeit");
if ( dateReturned != expectedResult ) {
errln("ERROR: Simple test uses full date format.");
errln(UnicodeString(" Got: ") + dateReturned + UnicodeString(" Expected: ") + expectedResult);
}
// modify it to change the zone.
UnicodeString newPattern = gen->replaceFieldTypes(pattern, UnicodeString("vvvv"), status);
format->applyPattern(newPattern);
dateReturned.remove();
dateReturned = format->format(sampleDate, dateReturned, status);
expectedResult=UnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 Frankreich");
if ( dateReturned != expectedResult ) {
errln("ERROR: Simple test modify the timezone!");
errln(UnicodeString(" Got: ")+ dateReturned + UnicodeString(" Expected: ") + expectedResult);
}
// setDeciaml(), getDeciaml()
gen->setDecimal(newDecimal);
if (newDecimal != gen->getDecimal()) {
errln("ERROR: unexpected result from setDecimal() and getDecimal()!.\n");
}
// setAppenItemName() , getAppendItemName()
gen->setAppendItemName(UDATPG_HOUR_FIELD, newAppendItemName);
if (newAppendItemName != gen->getAppendItemName(UDATPG_HOUR_FIELD)) {
errln("ERROR: unexpected result from setAppendItemName() and getAppendItemName()!.\n");
}
// setAppenItemFormat() , getAppendItemFormat()
示例14: if
status_t
ICUTimeData::_SetLCTimePattern(DateFormat* format, char* destination,
int destinationSize)
{
SimpleDateFormat* simpleFormat = dynamic_cast<SimpleDateFormat*>(format);
if (!simpleFormat)
return B_BAD_TYPE;
// convert ICU-type pattern to posix (i.e. strftime()) format string
UnicodeString icuPattern;
simpleFormat->toPattern(icuPattern);
UnicodeString posixPattern;
if (icuPattern.length() > 0) {
UChar lastCharSeen = 0;
int lastCharCount = 1;
bool inSingleQuotes = false;
bool inDoubleQuotes = false;
// we loop one character past the end on purpose, which will result in a
// final -1 char to be processed, which in turn will let us handle the
// last character (via lastCharSeen)
for (int i = 0; i <= icuPattern.length(); ++i) {
UChar currChar = icuPattern.charAt(i);
if (lastCharSeen != 0 && currChar == lastCharSeen) {
lastCharCount++;
continue;
}
if (!inSingleQuotes && !inDoubleQuotes) {
switch (lastCharSeen) {
case L'a':
posixPattern.append(UnicodeString("%p", ""));
break;
case L'd':
if (lastCharCount == 2)
posixPattern.append(UnicodeString("%d", ""));
else
posixPattern.append(UnicodeString("%e", ""));
break;
case L'D':
posixPattern.append(UnicodeString("%j", ""));
break;
case L'c':
// fall through, to handle 'c' the same as 'e'
case L'e':
if (lastCharCount == 4)
posixPattern.append(UnicodeString("%A", ""));
else if (lastCharCount <= 2)
posixPattern.append(UnicodeString("%u", ""));
else
posixPattern.append(UnicodeString("%a", ""));
break;
case L'E':
if (lastCharCount == 4)
posixPattern.append(UnicodeString("%A", ""));
else
posixPattern.append(UnicodeString("%a", ""));
break;
case L'k':
// fall through, to handle 'k' the same as 'h'
case L'h':
if (lastCharCount == 2)
posixPattern.append(UnicodeString("%I", ""));
else
posixPattern.append(UnicodeString("%l", ""));
break;
case L'H':
if (lastCharCount == 2)
posixPattern.append(UnicodeString("%H", ""));
else
posixPattern.append(UnicodeString("%k", ""));
break;
case L'm':
posixPattern.append(UnicodeString("%M", ""));
break;
case L'L':
// fall through, to handle 'L' the same as 'M'
case L'M':
if (lastCharCount == 4)
posixPattern.append(UnicodeString("%B", ""));
else if (lastCharCount == 3)
posixPattern.append(UnicodeString("%b", ""));
else
posixPattern.append(UnicodeString("%m", ""));
break;
case L's':
posixPattern.append(UnicodeString("%S", ""));
break;
case L'w':
posixPattern.append(UnicodeString("%V", ""));
break;
case L'y':
if (lastCharCount == 2)
posixPattern.append(UnicodeString("%y", ""));
else
posixPattern.append(UnicodeString("%Y", ""));
break;
case L'Y':
posixPattern.append(UnicodeString("%G", ""));
break;
case L'z':
//.........这里部分代码省略.........