本文整理汇总了C++中DateFormat类的典型用法代码示例。如果您正苦于以下问题:C++ DateFormat类的具体用法?C++ DateFormat怎么用?C++ DateFormat使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DateFormat类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: while
/**
* Test that the equals method works correctly.
*/
void IntlTestDateFormatAPI::TestEquals(void)
{
UErrorCode status = U_ZERO_ERROR;
// Create two objects at different system times
DateFormat *a = DateFormat::createInstance();
UDate start = Calendar::getNow();
while (Calendar::getNow() == start) ; // Wait for time to change
DateFormat *b = DateFormat::createInstance();
if (a == NULL || b == NULL){
dataerrln("Error calling DateFormat::createInstance()");
delete a;
delete b;
return;
}
if (!(*a == *b))
errln("FAIL: DateFormat objects created at different times are unequal.");
if (b->getDynamicClassID() == SimpleDateFormat::getStaticClassID())
{
double ONE_YEAR = 365*24*60*60*1000.0;
((SimpleDateFormat*)b)->set2DigitYearStart(start + 50*ONE_YEAR, status);
if (U_FAILURE(status))
errln("FAIL: setTwoDigitStartDate failed.");
else if (*a == *b)
errln("FAIL: DateFormat objects with different two digit start dates are equal.");
}
delete a;
delete b;
}
示例2: d
void DateFormatRegressionTest::Test714(void)
{
//try {
UDate d(978103543000.);
DateFormat *fmt = DateFormat::createDateTimeInstance(DateFormat::NONE,
DateFormat::MEDIUM,
Locale::getUS());
if (fmt == NULL) {
dataerrln("Error calling DateFormat::createDateTimeInstance");
return;
}
UnicodeString s;
UnicodeString tests =
(UnicodeString) "7:25:43 AM" ;
UErrorCode status = U_ZERO_ERROR;
fmt->format (d,s);
if(U_FAILURE(status))
{
errln((UnicodeString) "Fail, errmsg " + u_errorName(status));
return;
}
if(s != tests)
{
errln((UnicodeString) "Fail: " + s + " != " + tests);
}
else
{
logln("OK: " + s + " == " + tests);
}
delete fmt;
}
示例3: udat_open
U_CAPI UDateFormat* U_EXPORT2
udat_open(UDateFormatStyle timeStyle,
UDateFormatStyle dateStyle,
const char *locale,
const UChar *tzID,
int32_t tzIDLength,
const UChar *pattern,
int32_t patternLength,
UErrorCode *status)
{
DateFormat *fmt;
if(U_FAILURE(*status)) {
return 0;
}
if(gOpener!=NULL) { // if it's registered
fmt = (DateFormat*) (*gOpener)(timeStyle,dateStyle,locale,tzID,tzIDLength,pattern,patternLength,status);
if(fmt!=NULL) {
return (UDateFormat*)fmt;
} // else fall through.
}
if(timeStyle != UDAT_PATTERN) {
if(locale == 0) {
fmt = DateFormat::createDateTimeInstance((DateFormat::EStyle)dateStyle,
(DateFormat::EStyle)timeStyle);
}
else {
fmt = DateFormat::createDateTimeInstance((DateFormat::EStyle)dateStyle,
(DateFormat::EStyle)timeStyle,
Locale(locale));
}
}
else {
UnicodeString pat((UBool)(patternLength == -1), pattern, patternLength);
if(locale == 0) {
fmt = new SimpleDateFormat(pat, *status);
}
else {
fmt = new SimpleDateFormat(pat, Locale(locale), *status);
}
}
if(fmt == 0) {
*status = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
if(tzID != 0) {
TimeZone *zone = TimeZone::createTimeZone(UnicodeString((UBool)(tzIDLength == -1), tzID, tzIDLength));
if(zone == 0) {
*status = U_MEMORY_ALLOCATION_ERROR;
delete fmt;
return 0;
}
fmt->adoptTimeZone(zone);
}
return (UDateFormat*)fmt;
}
示例4: errorInJson
std::string GlobalizationNDK::stringToDate(const std::string& args)
{
if (args.empty())
return errorInJson(PARSING_ERROR, "No dateString provided!");
Json::Reader reader;
Json::Value root;
bool parse = reader.parse(args, root);
if (!parse) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToDate: invalid json data: %s",
args.c_str());
return errorInJson(PARSING_ERROR, "Parameters not valid json format!");
}
Json::Value dateString = root["dateString"];
if (!dateString.isString()) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToDate: invalid dateString type: %d",
dateString.type());
return errorInJson(PARSING_ERROR, "dateString not a string!");
}
std::string dateValue = dateString.asString();
if (dateValue.empty()) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToDate: empty dateString.");
return errorInJson(PARSING_ERROR, "dateString is empty!");
}
Json::Value options = root["options"];
DateFormat::EStyle dstyle, tstyle;
std::string error;
if (!handleDateOptions(options, dstyle, tstyle, error))
return errorInJson(PARSING_ERROR, error);
const Locale& loc = Locale::getDefault();
DateFormat* df = DateFormat::createDateTimeInstance(dstyle, tstyle, loc);
if (!df) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToDate: unable to create DateFormat instance!");
return errorInJson(UNKNOWN_ERROR, "Unable to create DateFormat instance!");
}
std::auto_ptr<DateFormat> deleter(df);
UnicodeString uDate = UnicodeString::fromUTF8(dateValue);
UErrorCode status = U_ZERO_ERROR;
UDate date = df->parse(uDate, status);
// Note: not sure why U_ERROR_WARNING_START is returned when parse succeeded.
if (status != U_ZERO_ERROR && status != U_ERROR_WARNING_START) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToDate: DataFormat::parse error: %d: %s",
status, dateValue.c_str());
return errorInJson(PARSING_ERROR, "Failed to parse dateString!");
}
return resultDateInJson(date);
}
示例5: slog2f
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);
}
示例6: main
int main(int argc, char **argv) {
Calendar *cal;
TimeZone *zone;
DateFormat *fmt;
UErrorCode status = U_ZERO_ERROR;
UnicodeString str;
UDate date;
// The languages in which we will display the date
static char* LANGUAGE[] = {
"en", "de", "fr"
};
static const int32_t N_LANGUAGE = sizeof(LANGUAGE)/sizeof(LANGUAGE[0]);
// The time zones in which we will display the time
static char* TIMEZONE[] = {
"America/Los_Angeles",
"America/New_York",
"Europe/Paris",
"Europe/Berlin"
};
static const int32_t N_TIMEZONE = sizeof(TIMEZONE)/sizeof(TIMEZONE[0]);
// Create a calendar
cal = Calendar::createInstance(status);
check(status, "Calendar::createInstance");
zone = createZone("GMT"); // Create a GMT zone
cal->adoptTimeZone(zone);
cal->clear();
cal->set(1999, Calendar::JUNE, 4);
date = cal->getTime(status);
check(status, "Calendar::getTime");
for (int32_t i=0; i<N_LANGUAGE; ++i) {
Locale loc(LANGUAGE[i]);
// Create a formatter for DATE
fmt = DateFormat::createDateInstance(DateFormat::kFull, loc);
// Format the date
str.remove();
fmt->format(date, str, status);
// Display the formatted date string
printf("Date (%s): ", LANGUAGE[i]);
uprintf(escape(str));
printf("\n\n");
}
printf("Exiting successfully\n");
return 0;
}
示例7: SimpleDateFormat
UDate L10n::ParseDateTime(const std::string& dateTimeString, const std::string& dateTimeFormat, const Locale& locale) const
{
UErrorCode success = U_ZERO_ERROR;
UnicodeString utf16DateTimeString = UnicodeString::fromUTF8(dateTimeString.c_str());
UnicodeString utf16DateTimeFormat = UnicodeString::fromUTF8(dateTimeFormat.c_str());
DateFormat* dateFormatter = new SimpleDateFormat(utf16DateTimeFormat, locale, success);
UDate date = dateFormatter->parse(utf16DateTimeString, success);
delete dateFormatter;
return date;
}
示例8: logln
/**
* @bug 4060212
*/
void DateFormatRegressionTest::Test4060212(void)
{
UnicodeString dateString = "1995-040.05:01:29";
logln( "dateString= " + dateString );
logln("Using yyyy-DDD.hh:mm:ss");
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *formatter = new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status);
if (failure(status, "new SimpleDateFormat", TRUE)) return;
ParsePosition pos(0);
UDate myDate = formatter->parse( dateString, pos );
UnicodeString myString;
DateFormat *fmt = DateFormat::createDateTimeInstance( DateFormat::FULL,
DateFormat::LONG);
if (fmt == NULL) {
dataerrln("Error calling DateFormat::createDateTimeInstance");
delete formatter;
return;
}
myString = fmt->format( myDate, myString);
logln( myString );
Calendar *cal = new GregorianCalendar(status);
failure(status, "new GregorianCalendar");
cal->setTime(myDate, status);
failure(status, "cal->setTime");
if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get"))
errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) +
" Want 40");
// this is an odd usage of "ddd" and it doesn't
// work now that date values are range checked per #3579.
logln("Using yyyy-ddd.hh:mm:ss");
delete formatter;
formatter = NULL;
formatter = new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status);
if(failure(status, "new SimpleDateFormat")) return;
pos.setIndex(0);
myDate = formatter->parse( dateString, pos );
myString = fmt->format( myDate, myString );
logln( myString );
cal->setTime(myDate, status);
failure(status, "cal->setTime");
if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get"))
errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) +
" Want 40");
delete formatter;
delete fmt;
delete cal;
}
示例9: CreateDateTimeInstance
std::string L10n::LocalizeDateTime(const UDate& dateTime, const DateTimeType& type, const DateFormat::EStyle& style) const
{
UnicodeString utf16Date;
DateFormat* dateFormatter = CreateDateTimeInstance(type, style, currentLocale);
dateFormatter->format(dateTime, utf16Date);
char utf8Date[512];
CheckedArrayByteSink sink(utf8Date, ARRAY_SIZE(utf8Date));
utf16Date.toUTF8(sink);
ENSURE(!sink.Overflowed());
delete dateFormatter;
return std::string(utf8Date, sink.NumberOfBytesWritten());
}
示例10: getDateFormat
UnicodeString&
CalendarTimeZoneTest::dateToString(UDate d, UnicodeString& str)
{
str.remove();
DateFormat* format = getDateFormat();
if (format == 0)
{
str += "DATE_FORMAT_FAILURE";
return str;
}
format->format(d, str);
releaseDateFormat(format);
return str;
}
示例11: umsg_set_timezone
static void umsg_set_timezone(MessageFormatter_object *mfo,
intl_error& err)
{
MessageFormat *mf = (MessageFormat *)mfo->mf_data.umsgf;
TimeZone *used_tz = NULL;
const Format **formats;
int32_t count;
/* Unfortanely, this cannot change the time zone for arguments that
* appear inside complex formats because ::getFormats() returns NULL
* for all uncached formats, which is the case for complex formats
* unless they were set via one of the ::setFormat() methods */
if (mfo->mf_data.tz_set) {
return; /* already done */
}
formats = mf->getFormats(count);
if (formats == NULL) {
intl_errors_set(&err, U_MEMORY_ALLOCATION_ERROR,
"Out of memory retrieving subformats", 0);
}
for (int i = 0; U_SUCCESS(err.code) && i < count; i++) {
DateFormat* df = dynamic_cast<DateFormat*>(
const_cast<Format *>(formats[i]));
if (df == NULL) {
continue;
}
if (used_tz == NULL) {
zval nullzv, *zvptr = &nullzv;
ZVAL_NULL(zvptr);
used_tz = timezone_process_timezone_argument(zvptr, &err, "msgfmt_format");
if (used_tz == NULL) {
continue;
}
}
df->setTimeZone(*used_tz);
}
if (U_SUCCESS(err.code)) {
mfo->mf_data.tz_set = 1;
}
}
示例12: dataerrln
void
CalendarLimitTest::TestCalendarExtremeLimit()
{
UErrorCode status = U_ZERO_ERROR;
Calendar *cal = Calendar::createInstance(status);
if (failure(status, "Calendar::createInstance", TRUE)) return;
cal->adoptTimeZone(TimeZone::createTimeZone("GMT"));
DateFormat *fmt = DateFormat::createDateTimeInstance();
if(!fmt || !cal) {
dataerrln("can't open cal and/or fmt");
return;
}
fmt->adoptCalendar(cal);
((SimpleDateFormat*) fmt)->applyPattern("HH:mm:ss.SSS Z, EEEE, MMMM d, yyyy G");
// This test used to test the algorithmic limits of the dates that
// GregorianCalendar could handle. However, the algorithm has
// been rewritten completely since then and the prior limits no
// longer apply. Instead, we now do basic round-trip testing of
// some extreme (but still manageable) dates.
UDate m;
logln("checking 1e16..1e17");
for ( m = 1e16; m < 1e17; m *= 1.1) {
test(m, cal, fmt);
}
logln("checking -1e14..-1e15");
for ( m = -1e14; m > -1e15; m *= 1.1) {
test(m, cal, fmt);
}
// This is 2^52 - 1, the largest allowable mantissa with a 0
// exponent in a 64-bit double
UDate VERY_EARLY_MILLIS = - 4503599627370495.0;
UDate VERY_LATE_MILLIS = 4503599627370495.0;
// I am removing the previousDouble and nextDouble calls below for
// two reasons: 1. As part of jitterbug 986, I am deprecating
// these methods and removing calls to them. 2. This test is a
// non-critical boundary behavior test.
test(VERY_EARLY_MILLIS, cal, fmt);
//test(previousDouble(VERY_EARLY_MILLIS), cal, fmt);
test(VERY_LATE_MILLIS, cal, fmt);
//test(nextDouble(VERY_LATE_MILLIS), cal, fmt);
delete fmt;
}
示例13: dataerrln
/**
* @bug 4071441
*/
void DateFormatRegressionTest::Test4071441(void)
{
DateFormat *fmtA = DateFormat::createInstance();
DateFormat *fmtB = DateFormat::createInstance();
if (fmtA == NULL || fmtB == NULL){
dataerrln("Error calling DateFormat::createInstance");
delete fmtA;
delete fmtB;
return;
}
// {sfb} Is it OK to cast away const here?
Calendar *calA = (Calendar*) fmtA->getCalendar();
Calendar *calB = (Calendar*) fmtB->getCalendar();
if(!calA || !calB) {
errln("Couldn't get proper calendars, exiting");
delete fmtA;
delete fmtB;
return;
}
UDate epoch = date(0, 0, 0);
UDate xmas = date(61, UCAL_DECEMBER, 25);
UErrorCode status = U_ZERO_ERROR;
calA->setTime(epoch, status);
failure(status, "calA->setTime");
calB->setTime(epoch, status);
failure(status, "calB->setTime");
if (*calA != *calB)
errln("Fail: Can't complete test; Calendar instances unequal");
if (*fmtA != *fmtB)
errln("Fail: DateFormat unequal when Calendars equal");
calB->setTime(xmas, status);
failure(status, "calB->setTime");
if (*calA == *calB)
errln("Fail: Can't complete test; Calendar instances equal");
if (*fmtA != *fmtB)
errln("Fail: DateFormat unequal when Calendars equivalent");
logln("DateFormat.equals ok");
delete fmtA;
delete fmtB;
}
示例14: main
int main(int argc, char **argv) {
Calendar *cal;
DateFormat *fmt;
DateFormat *defFmt;
UErrorCode status = U_ZERO_ERROR;
Locale greece("el", "GR");
UnicodeString str, str2;
// Create a calendar in the Greek locale
cal = Calendar::createInstance(greece, status);
check(status, "Calendar::createInstance");
// Create a formatter
fmt = DateFormat::createDateInstance(DateFormat::kFull, greece);
fmt->setCalendar(*cal);
// Create a default formatter
defFmt = DateFormat::createDateInstance(DateFormat::kFull);
defFmt->setCalendar(*cal);
// Loop over various months
for (int32_t month = Calendar::JANUARY;
month <= Calendar::DECEMBER;
++month) {
// Set the calendar to a date
cal->clear();
cal->set(1999, month, 4);
// Format the date in default locale
str.remove();
defFmt->format(cal->getTime(status), str, status);
check(status, "DateFormat::format");
printf("Date: ");
uprintf(escape(str));
printf("\n");
// Format the date for Greece
str.remove();
fmt->format(cal->getTime(status), str, status);
check(status, "DateFormat::format");
printf("Greek formatted date: ");
uprintf(escape(str));
printf("\n\n");
}
// Clean up
delete fmt;
delete cal;
printf("Exiting successfully\n");
return 0;
}
示例15: 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;
}