本文整理汇总了C++中SimpleDateFormat::setTimeZone方法的典型用法代码示例。如果您正苦于以下问题:C++ SimpleDateFormat::setTimeZone方法的具体用法?C++ SimpleDateFormat::setTimeZone怎么用?C++ SimpleDateFormat::setTimeZone使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SimpleDateFormat
的用法示例。
在下文中一共展示了SimpleDateFormat::setTimeZone方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: NumericDateFormatters
// Constructor that takes the actual patterns for hour-minute,
// minute-second, and hour-minute-second respectively.
NumericDateFormatters(
const UnicodeString &hm,
const UnicodeString &ms,
const UnicodeString &hms,
UErrorCode &status) :
hourMinute(hm, status),
minuteSecond(ms, status),
hourMinuteSecond(hms, status) {
const TimeZone *gmt = TimeZone::getGMT();
hourMinute.setTimeZone(*gmt);
minuteSecond.setTimeZone(*gmt);
hourMinuteSecond.setTimeZone(*gmt);
}
示例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: testAPI
//.........这里部分代码省略.........
}
if ( *cloneDTPatternGen != *instFromLocale ) {
errln("ERROR: inconsistency is found in cloned object.");
}
delete instFromLocale;
delete cloneDTPatternGen;
// ======= Test simple use cases
logln("Testing simple use cases");
status = U_ZERO_ERROR;
Locale deLocale=Locale::getGermany();
UDate sampleDate=LocaleTest::date(99, 9, 13, 23, 58, 59);
DateTimePatternGenerator *gen = DateTimePatternGenerator::createInstance(deLocale, status);
if (U_FAILURE(status)) {
dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getGermany()) - exitting");
return;
}
UnicodeString findPattern = gen->getBestPattern(UnicodeString("MMMddHmm"), status);
SimpleDateFormat *format = new SimpleDateFormat(findPattern, deLocale, status);
if (U_FAILURE(status)) {
dataerrln("ERROR: Could not create SimpleDateFormat (Locale::getGermany())");
delete gen;
return;
}
TimeZone *zone = TimeZone::createTimeZone(UnicodeString("ECT"));
if (zone==NULL) {
dataerrln("ERROR: Could not create TimeZone ECT");
delete gen;
delete format;
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();
示例4: SimpleDateFormat
/**
* @bug 4106807
*/
void DateFormatRegressionTest::Test4106807(void)
{
UDate dt;
DateFormat *df = DateFormat::createDateTimeInstance();
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *sdfs [] = {
new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss"), status),
new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'Z'"), status),
new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss''"), status),
new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'a''a'"), status),
new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status)
};
if(U_FAILURE(status)) {
dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
delete sdfs[0];
delete sdfs[1];
delete sdfs[2];
delete sdfs[3];
delete sdfs[4];
return;
}
failure(status, "new SimpleDateFormat");
UnicodeString strings [] = {
(UnicodeString) "19980211140000",
(UnicodeString) "19980211140000",
(UnicodeString) "19980211140000",
(UnicodeString) "19980211140000a",
(UnicodeString) "19980211140000 "
};
/*Object[] data = {
new SimpleDateFormat("yyyyMMddHHmmss"), "19980211140000",
new SimpleDateFormat("yyyyMMddHHmmss'Z'"), "19980211140000",
new SimpleDateFormat("yyyyMMddHHmmss''"), "19980211140000",
new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), "19980211140000a",
new SimpleDateFormat("yyyyMMddHHmmss %"), "19980211140000 ",
};*/
GregorianCalendar *gc = new GregorianCalendar(status);
failure(status, "new GregorianCalendar");
TimeZone *timeZone = TimeZone::createDefault();
TimeZone *gmt = timeZone->clone();
gmt->setRawOffset(0);
for(int32_t i = 0; i < 5; i++) {
SimpleDateFormat *format = sdfs[i];
UnicodeString dateString = strings[i];
//try {
format->setTimeZone(*gmt);
dt = format->parse(dateString, status);
// {sfb} some of these parses will fail purposely
if(U_FAILURE(status))
break;
status = U_ZERO_ERROR;
UnicodeString fmtd;
FieldPosition pos(FieldPosition::DONT_CARE);
fmtd = df->format(dt, fmtd, pos);
logln(fmtd);
//logln(df->format(dt));
gc->setTime(dt, status);
failure(status, "gc->getTime");
logln(UnicodeString("") + gc->get(UCAL_ZONE_OFFSET, status));
failure(status, "gc->get");
UnicodeString s;
s = format->format(dt, s, pos);
logln(s);
/*}
catch (ParseException e) {
logln("No way Jose");
}*/
}
delete timeZone;
delete df;
for(int32_t j = 0; j < 5; j++)
delete sdfs [j];
delete gc;
delete gmt;
}
示例5: SimpleTimeZone
/**
* @bug 4162593
* TimeZone broken at midnight. The TimeZone code fails to handle
* transitions at midnight correctly.
*/
void
TimeZoneRegressionTest::Test4162593()
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *fmt = new SimpleDateFormat("z", Locale::getUS(), status);
if(U_FAILURE(status)) {
dataerrln("Error creating calendar %s", u_errorName(status));
delete fmt;
return;
}
const int32_t ONE_HOUR = 60*60*1000;
SimpleTimeZone *asuncion = new SimpleTimeZone(-4*ONE_HOUR, "America/Asuncion" /*PY%sT*/,
UCAL_OCTOBER, 1, 0 /*DOM*/, 0*ONE_HOUR,
UCAL_MARCH, 1, 0 /*DOM*/, 0*ONE_HOUR, 1*ONE_HOUR, status);
/* Zone
* Starting time
* Transition expected between start+1H and start+2H
*/
TimeZone *DATA_TZ [] = {
0, 0, 0 };
int32_t DATA_INT [] [5] = {
// These years must be AFTER the Gregorian cutover
{1998, UCAL_SEPTEMBER, 30, 22, 0},
{2000, UCAL_FEBRUARY, 28, 22, 0},
{2000, UCAL_FEBRUARY, 29, 22, 0},
};
UBool DATA_BOOL [] = {
TRUE,
FALSE,
TRUE,
};
UnicodeString zone [4];// = new String[4];
DATA_TZ[0] =
new SimpleTimeZone(2*ONE_HOUR, "Asia/Damascus" /*EE%sT*/,
UCAL_APRIL, 1, 0 /*DOM*/, 0*ONE_HOUR,
UCAL_OCTOBER, 1, 0 /*DOM*/, 0*ONE_HOUR, 1*ONE_HOUR, status);
DATA_TZ[1] = asuncion; DATA_TZ[2] = asuncion;
for(int32_t j = 0; j < 3; j++) {
TimeZone *tz = (TimeZone*)DATA_TZ[j];
TimeZone::setDefault(*tz);
fmt->setTimeZone(*tz);
// Must construct the Date object AFTER setting the default zone
int32_t *p = (int32_t*)DATA_INT[j];
UDate d = CalendarRegressionTest::makeDate(p[0], p[1], p[2], p[3], p[4]);
UBool transitionExpected = DATA_BOOL[j];
UnicodeString temp;
logln(tz->getID(temp) + ":");
for (int32_t i = 0; i < 4; ++i) {
FieldPosition pos(0);
zone[i].remove();
zone[i] = fmt->format(d+ i*ONE_HOUR, zone[i], pos);
logln(UnicodeString("") + i + ": " + d + " / " + zone[i]);
//d += (double) ONE_HOUR;
}
if(zone[0] == zone[1] &&
(zone[1] == zone[2]) != transitionExpected &&
zone[2] == zone[3]) {
logln(UnicodeString("Ok: transition ") + transitionExpected);
}
else {
errln("Fail: boundary transition incorrect");
}
}
delete fmt;
delete asuncion;
delete DATA_TZ[0];
}