2000-01-15 02:00:06 +00:00
|
|
|
/********************************************************************
|
|
|
|
* COPYRIGHT:
|
2001-03-21 19:46:49 +00:00
|
|
|
* Copyright (c) 1997-2001, International Business Machines Corporation and
|
2000-01-15 02:00:06 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************/
|
|
|
|
/********************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File CDTDPTST.C
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
* Name Description
|
|
|
|
* Madhu Katragadda Creation
|
|
|
|
*********************************************************************************
|
|
|
|
*/
|
|
|
|
/* INDEPTH TEST FOR DATE FORMAT */
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/uloc.h"
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/udat.h"
|
|
|
|
#include "unicode/ucal.h"
|
|
|
|
#include "unicode/unum.h"
|
|
|
|
#include "unicode/ustring.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "cintltst.h"
|
|
|
|
#include "cdtdptst.h"
|
|
|
|
#include "cformtst.h"
|
|
|
|
|
2001-05-31 23:42:59 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
|
|
|
|
void addDtFrDepTest(TestNode** root);
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void addDtFrDepTest(TestNode** root)
|
|
|
|
{
|
|
|
|
addTest(root, &TestTwoDigitYearDSTParse, "tsformat/cdtdptst/TestTwoDigitYearDSTParse");
|
|
|
|
addTest(root, &TestPartialParse994, "tsformat/cdtdptst/TestPartialParse994");
|
|
|
|
addTest(root, &TestRunTogetherPattern985, "tsformat/cdtdptst/TestRunTogetherPattern985");
|
|
|
|
addTest(root, &TestCzechMonths459, "tsformat/cdtdptst/TestCzechMonths459");
|
|
|
|
addTest(root, &TestQuotePattern161, "tsformat/cdtdptst/TestQuotePattern161");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the parsing of 2-digit years.
|
|
|
|
*/
|
|
|
|
void TestTwoDigitYearDSTParse()
|
|
|
|
{
|
|
|
|
UDateFormat *fullFmt, *fmt;
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar *pattern;
|
|
|
|
UDate d;
|
|
|
|
UChar *s;
|
|
|
|
int32_t pos;
|
|
|
|
|
2001-05-31 23:42:59 +00:00
|
|
|
pattern=(UChar*)uprv_malloc(sizeof(UChar) * (strlen("EEE MMM dd HH:mm:ss.SSS zzz yyyy G")+1 ));
|
1999-08-16 21:50:52 +00:00
|
|
|
u_uastrcpy(pattern, "EEE MMM dd HH:mm:ss.SSS zzz yyyy G");
|
2001-08-16 00:58:53 +00:00
|
|
|
fullFmt= udat_open(UDAT_IGNORE, UDAT_IGNORE,"en_US",NULL,0,pattern, u_strlen(pattern),&status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
log_err("FAIL: Error in creating a date format using udat_openPattern \n %s\n",
|
|
|
|
myErrorName(status) );
|
|
|
|
}
|
|
|
|
else {log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n");}
|
|
|
|
|
|
|
|
u_uastrcpy(pattern, "dd-MMM-yy h:mm:ss 'o''clock' a z");
|
2001-08-16 00:58:53 +00:00
|
|
|
fmt= udat_open(UDAT_IGNORE,UDAT_IGNORE,"en_US", NULL, 0,pattern, u_strlen(pattern), &status);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
|
2001-05-31 23:42:59 +00:00
|
|
|
s=(UChar*)uprv_malloc(sizeof(UChar) * (strlen("03-Apr-04 2:20:47 o'clock AM PST")+1) );
|
1999-08-16 21:50:52 +00:00
|
|
|
u_uastrcpy(s, "03-Apr-04 2:20:47 o'clock AM PST");
|
|
|
|
pos=0;
|
|
|
|
d = udat_parse(fmt, s, u_strlen(s), &pos, &status);
|
1999-12-09 06:23:48 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
log_err("FAIL: Could not parse \"%s\"\n", austrdup(s));
|
|
|
|
} else {
|
|
|
|
UCalendar *cal = ucal_open(NULL, 0, uloc_getDefault(), UCAL_TRADITIONAL, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
log_err("FAIL: Could not open calendar");
|
|
|
|
} else {
|
|
|
|
int32_t h;
|
|
|
|
ucal_setMillis(cal, d, &status);
|
|
|
|
h = ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
log_err("FAIL: Some calendar operations failed");
|
|
|
|
} else if (h != 2) {
|
|
|
|
log_err("FAIL: Parse of \"%s\" returned HOUR_OF_DAY %d\n",
|
|
|
|
austrdup(s), h);
|
|
|
|
}
|
|
|
|
ucal_close(cal);
|
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
udat_close(fullFmt);
|
|
|
|
udat_close(fmt);
|
2001-05-31 23:42:59 +00:00
|
|
|
uprv_free(pattern);
|
|
|
|
uprv_free(s);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify that strings which contain incomplete specifications are parsed
|
|
|
|
* correctly. In some instances, this means not being parsed at all, and
|
|
|
|
* returning an appropriate error.
|
|
|
|
*/
|
|
|
|
void TestPartialParse994()
|
|
|
|
{
|
|
|
|
int32_t pos;
|
|
|
|
UDateFormat *f;
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar *s;
|
|
|
|
UDate d, null;
|
|
|
|
null=0;
|
|
|
|
|
2000-06-10 05:10:28 +00:00
|
|
|
/* this is supposed to open default date format, but later on it treats it like it is "en_US"
|
|
|
|
- very bad if you try to run the tests on machine where default locale is NOT "en_US" */
|
|
|
|
/* f = udat_open(UDAT_DEFAULT, UDAT_SHORT, NULL, NULL, 0, &status); */
|
2001-08-16 00:58:53 +00:00
|
|
|
f = udat_open(UDAT_DEFAULT, UDAT_SHORT, "en_US", NULL, 0, NULL, 0,&status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)){
|
1999-08-16 21:50:52 +00:00
|
|
|
log_err("FAIL: ErrorCode received during test: %s\n", myErrorName(status));
|
2002-01-30 06:16:26 +00:00
|
|
|
return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-05-31 23:42:59 +00:00
|
|
|
s=(UChar*)uprv_malloc(sizeof(UChar) * (strlen("01/01/97 10:11:42 AM")+1) );
|
1999-08-16 21:50:52 +00:00
|
|
|
u_uastrcpy(s, "01/01/97 10:11:42 AM");
|
|
|
|
pos=0;
|
|
|
|
d = udat_parse(f, s, u_strlen(s), &pos, &status);
|
|
|
|
log_verbose("%s\n", austrdup(myDateFormat(f, d)) );
|
|
|
|
tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10:11:42", d);
|
|
|
|
tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10:", null);
|
|
|
|
tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10", null);
|
|
|
|
tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 ", null);
|
|
|
|
tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01", null);
|
|
|
|
udat_close(f);
|
2001-05-31 23:42:59 +00:00
|
|
|
uprv_free(s);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void tryPat994(UDateFormat* format, const char* pattern, const char* s, UDate expected)
|
|
|
|
{
|
|
|
|
UChar *f;
|
|
|
|
UChar *str, *pat;
|
|
|
|
UDate date;
|
|
|
|
UDate null=0;
|
|
|
|
int32_t pos;
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2001-05-31 23:42:59 +00:00
|
|
|
str=(UChar*)uprv_malloc(sizeof(UChar) * (strlen(s) + 1) );
|
1999-08-16 21:50:52 +00:00
|
|
|
u_uastrcpy(str, s);
|
2001-05-31 23:42:59 +00:00
|
|
|
pat=(UChar*)uprv_malloc(sizeof(UChar) * (strlen(pattern) + 1) );
|
1999-08-16 21:50:52 +00:00
|
|
|
u_uastrcpy(pat, pattern);
|
|
|
|
log_verbose("Pattern : %s ; String : %s\n", austrdup(pat), austrdup(str));
|
|
|
|
udat_applyPattern(format, FALSE, pat, u_strlen(pat));
|
|
|
|
pos=0;
|
|
|
|
date = udat_parse(format, str, u_strlen(str), &pos, &status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status) || date == null) {
|
1999-08-16 21:50:52 +00:00
|
|
|
log_verbose("ParseException: : %s\n", myErrorName(status) );
|
|
|
|
if (expected != null)
|
|
|
|
log_err("FAIL: Expected: %s\n", austrdup(myDateFormat(format, expected)) );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
f=myDateFormat(format, date);
|
|
|
|
log_verbose(" parse( %s ) -> %s\n", austrdup(str), austrdup(f));
|
|
|
|
if (expected == null || !(date == expected))
|
|
|
|
log_err("FAIL: Expected null");
|
|
|
|
if (u_strcmp(f, str) !=0) log_err("FAIL: Expected : %s\n", austrdup(str) );
|
|
|
|
}
|
|
|
|
|
2001-05-31 23:42:59 +00:00
|
|
|
uprv_free(str);
|
|
|
|
uprv_free(pat);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify the behavior of patterns in which digits for different fields run together
|
|
|
|
* without intervening separators.
|
|
|
|
*/
|
|
|
|
void TestRunTogetherPattern985()
|
|
|
|
{
|
|
|
|
int32_t pos;
|
2000-08-14 17:13:09 +00:00
|
|
|
UChar *pattern=NULL, *now=NULL, *then=NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
UDateFormat *format;
|
|
|
|
UDate date1, date2;
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2001-05-31 23:42:59 +00:00
|
|
|
pattern=(UChar*)uprv_malloc(sizeof(UChar) * (strlen("yyyyMMddHHmmssSSS")+1) );
|
1999-08-16 21:50:52 +00:00
|
|
|
u_uastrcpy(pattern, "yyyyMMddHHmmssSSS");
|
2001-08-16 00:58:53 +00:00
|
|
|
format = udat_open(UDAT_IGNORE, UDAT_IGNORE, NULL, NULL, 0,pattern, u_strlen(pattern), &status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)){
|
1999-08-16 21:50:52 +00:00
|
|
|
log_err("FAIL: Error in date format construction with pattern: %s\n", myErrorName(status));
|
2002-01-30 06:16:26 +00:00
|
|
|
return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
date1 = ucal_getNow();
|
|
|
|
now=myDateFormat(format, date1);
|
|
|
|
log_verbose("%s\n", austrdup(now) );
|
|
|
|
pos = 0;
|
|
|
|
date2 = udat_parse(format, now, u_strlen(now), &pos, &status);
|
|
|
|
if (date2 == 0) log_verbose("Parse stopped at : %d\n", pos);
|
|
|
|
else then=myDateFormat(format, date2);
|
|
|
|
log_verbose("%s\n", austrdup(then) );
|
|
|
|
if (!(date2 == date1)) log_err("FAIL\n");
|
|
|
|
|
|
|
|
udat_close(format);
|
2001-05-31 23:42:59 +00:00
|
|
|
uprv_free(pattern);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify the handling of Czech June and July, which have the unique attribute that
|
|
|
|
* one is a proper prefix substring of the other.
|
|
|
|
*/
|
|
|
|
void TestCzechMonths459()
|
|
|
|
{
|
|
|
|
int32_t lneed, pos;
|
2000-08-14 17:13:09 +00:00
|
|
|
UChar *pattern=NULL, *tzID=NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar *juneStr, *julyStr;
|
|
|
|
UDateFormat *fmt;
|
|
|
|
UCalendar *cal;
|
|
|
|
UDate june, july, d;
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2001-09-26 00:13:49 +00:00
|
|
|
char buffer[512];
|
|
|
|
UChar *date;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-08-16 00:58:53 +00:00
|
|
|
fmt = udat_open(UDAT_FULL, UDAT_FULL, "cs", NULL, 0, NULL, 0, &status);
|
2002-01-30 06:16:26 +00:00
|
|
|
if(U_FAILURE(status)){
|
|
|
|
log_err("Error in constructing the date format\n");
|
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
lneed=0;
|
|
|
|
lneed=udat_toPattern(fmt, TRUE, NULL, lneed, &status);
|
1999-10-07 00:07:53 +00:00
|
|
|
if(status==U_BUFFER_OVERFLOW_ERROR){
|
|
|
|
status=U_ZERO_ERROR;
|
2001-05-31 23:42:59 +00:00
|
|
|
pattern=(UChar*)uprv_malloc(sizeof(UChar) * (lneed+1) );
|
1999-08-16 21:50:52 +00:00
|
|
|
udat_toPattern(fmt, TRUE, pattern, lneed+1, &status);
|
|
|
|
}
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)){ log_err("Error in extracting the pattern\n"); }
|
2001-05-31 23:42:59 +00:00
|
|
|
tzID=(UChar*)uprv_malloc(sizeof(UChar) * 4);
|
1999-08-16 21:50:52 +00:00
|
|
|
u_uastrcpy(tzID, "GMT");
|
|
|
|
cal=ucal_open(tzID, u_strlen(tzID), "cs", UCAL_GREGORIAN, &status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)){ log_err("error in ucal_open caldef : %s\n", myErrorName(status)); }
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
ucal_setDate(cal, 1997, UCAL_JUNE, 15, &status);
|
|
|
|
june=ucal_getMillis(cal, &status);
|
|
|
|
ucal_setDate(cal, 1997, UCAL_JULY, 15, &status);
|
|
|
|
july=ucal_getMillis(cal, &status);
|
|
|
|
|
|
|
|
|
|
|
|
juneStr = myDateFormat(fmt, june);
|
|
|
|
julyStr = myDateFormat(fmt, july);
|
|
|
|
pos=0;
|
|
|
|
d = udat_parse(fmt, juneStr, u_strlen(juneStr), &pos, &status);
|
2001-09-26 00:13:49 +00:00
|
|
|
date = myDateFormat(fmt, d);
|
|
|
|
u_UCharsToChars(date, buffer, (UTextOffset)(u_strlen(date)+1));
|
1999-08-16 21:50:52 +00:00
|
|
|
if(u_strcmp(myDateFormat(fmt, june), myDateFormat(fmt, d) ) !=0)
|
|
|
|
log_err("Error in handling the czech month june\n");
|
|
|
|
else
|
2001-09-26 00:13:49 +00:00
|
|
|
log_verbose("Pass: Date = %s\n", buffer );
|
1999-08-16 21:50:52 +00:00
|
|
|
pos=0;
|
|
|
|
d = udat_parse(fmt, julyStr, u_strlen(julyStr), &pos, &status);
|
2001-09-26 00:13:49 +00:00
|
|
|
date = myDateFormat(fmt, d);
|
|
|
|
u_UCharsToChars(date, buffer, (UTextOffset)(u_strlen(date)+1));
|
1999-08-16 21:50:52 +00:00
|
|
|
if(u_strcmp(myDateFormat(fmt, july), myDateFormat(fmt, d) ) !=0)
|
|
|
|
log_err("Error in handling the czech month july\n");
|
|
|
|
else
|
2001-09-26 00:13:49 +00:00
|
|
|
log_verbose("Pass: Date = %s\n", buffer );
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
udat_close(fmt);
|
|
|
|
ucal_close(cal);
|
2001-05-31 23:42:59 +00:00
|
|
|
uprv_free(pattern);
|
|
|
|
uprv_free(tzID);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the handling of single quotes in patterns.
|
|
|
|
*/
|
|
|
|
void TestQuotePattern161()
|
|
|
|
{
|
|
|
|
UDateFormat *format;
|
|
|
|
UCalendar *cal;
|
|
|
|
UDate currentTime_1;
|
|
|
|
UChar *pattern, *tzID, *exp;
|
|
|
|
UChar *dateString;
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-12-09 06:23:48 +00:00
|
|
|
const char* expStr = "04/13/1999 at 10:42:28 AM ";
|
2001-05-31 23:42:59 +00:00
|
|
|
pattern=(UChar*)uprv_malloc(sizeof(UChar) * (strlen("MM/dd/yyyy 'at' hh:mm:ss a zzz")+1) );
|
1999-08-16 21:50:52 +00:00
|
|
|
u_uastrcpy(pattern, "MM/dd/yyyy 'at' hh:mm:ss a zzz");
|
|
|
|
|
2000-06-10 05:10:28 +00:00
|
|
|
/* this is supposed to open default date format, but later on it treats it like it is "en_US"
|
|
|
|
- very bad if you try to run the tests on machine where default locale is NOT "en_US" */
|
|
|
|
/* format= udat_openPattern(pattern, u_strlen(pattern), NULL, &status); */
|
2001-08-16 00:58:53 +00:00
|
|
|
format= udat_open(UDAT_IGNORE, UDAT_IGNORE,"en_US", NULL, 0,pattern, u_strlen(pattern), &status);
|
2002-01-30 06:16:26 +00:00
|
|
|
if(U_FAILURE(status)){
|
|
|
|
log_err("error in udat_open: %s\n", myErrorName(status));
|
|
|
|
return;
|
|
|
|
}
|
2001-05-31 23:42:59 +00:00
|
|
|
tzID=(UChar*)uprv_malloc(sizeof(UChar) * 4);
|
1999-08-16 21:50:52 +00:00
|
|
|
u_uastrcpy(tzID, "PST");
|
2000-06-10 05:10:28 +00:00
|
|
|
/* this is supposed to open default date format, but later on it treats it like it is "en_US"
|
|
|
|
- very bad if you try to run the tests on machine where default locale is NOT "en_US" */
|
|
|
|
/* cal=ucal_open(tzID, u_strlen(tzID), NULL, UCAL_TRADITIONAL, &status); */
|
|
|
|
cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)){ log_err("error in ucal_open cal : %s\n", myErrorName(status)); }
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
ucal_setDateTime(cal, 1999, UCAL_APRIL, 13, 10, 42, 28, &status);
|
|
|
|
currentTime_1 = ucal_getMillis(cal, &status);
|
|
|
|
|
|
|
|
dateString = myDateFormat(format, currentTime_1);
|
2001-05-31 23:42:59 +00:00
|
|
|
exp=(UChar*)uprv_malloc(sizeof(UChar) * (strlen(expStr) + 1) );
|
1999-12-09 06:23:48 +00:00
|
|
|
u_uastrcpy(exp, expStr);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
log_verbose("%s\n", austrdup(dateString) );
|
1999-12-09 06:23:48 +00:00
|
|
|
if(u_strncmp(dateString, exp, strlen(expStr)) !=0)
|
1999-08-16 21:50:52 +00:00
|
|
|
log_err("Error in formatting a pattern with single quotes\n");
|
2000-07-06 23:01:50 +00:00
|
|
|
|
|
|
|
udat_close(format);
|
|
|
|
ucal_close(cal);
|
2001-05-31 23:42:59 +00:00
|
|
|
uprv_free(exp);
|
|
|
|
uprv_free(tzID);
|
|
|
|
uprv_free(pattern);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
}
|