jscript: Simplify date_to_string and add basic tests.

Signed-off-by: Sebastian Lackner <sebastian@fds-team.de>
Signed-off-by: Jacek Caban <jacek@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Sebastian Lackner 2016-09-11 18:48:54 +02:00 committed by Alexandre Julliard
parent 4d67ffd57e
commit 20d5bbaa63
2 changed files with 18 additions and 34 deletions

View File

@ -494,8 +494,9 @@ static inline HRESULT date_to_string(DOUBLE time, BOOL show_offset, int offset,
BOOL formatAD = TRUE; BOOL formatAD = TRUE;
WCHAR week[64], month[64]; WCHAR week[64], month[64];
WCHAR buf[192];
jsstr_t *date_jsstr; jsstr_t *date_jsstr;
int len, size, year, day; int year, day;
DWORD lcid_en; DWORD lcid_en;
WCHAR sign = '-'; WCHAR sign = '-';
@ -506,66 +507,45 @@ static inline HRESULT date_to_string(DOUBLE time, BOOL show_offset, int offset,
} }
if(r) { if(r) {
WCHAR *date_str;
len = 21;
lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT); lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
size = GetLocaleInfoW(lcid_en, week_ids[(int)week_day(time)], week, sizeof(week)/sizeof(*week)); week[0] = 0;
assert(size); GetLocaleInfoW(lcid_en, week_ids[(int)week_day(time)], week, sizeof(week)/sizeof(*week));
len += size-1;
size = GetLocaleInfoW(lcid_en, month_ids[(int)month_from_time(time)], month, sizeof(month)/sizeof(*month)); month[0] = 0;
len += size-1; GetLocaleInfoW(lcid_en, month_ids[(int)month_from_time(time)], month, sizeof(month)/sizeof(*month));
year = year_from_time(time);
if(year<0)
year = -year+1;
do {
year /= 10;
len++;
} while(year);
year = year_from_time(time); year = year_from_time(time);
if(year<0) { if(year<0) {
formatAD = FALSE; formatAD = FALSE;
year = -year+1; year = -year+1;
len += 5;
} }
day = date_from_time(time);
do {
day /= 10;
len++;
} while(day);
day = date_from_time(time); day = date_from_time(time);
if(!show_offset) len -= 9; if(offset < 0) {
else if(offset == 0) len -= 5;
else if(offset < 0) {
sign = '+'; sign = '+';
offset = -offset; offset = -offset;
} }
date_jsstr = jsstr_alloc_buf(len, &date_str);
if(!date_jsstr)
return E_OUTOFMEMORY;
if(!show_offset) if(!show_offset)
sprintfW(date_str, formatNoOffsetW, week, month, day, sprintfW(buf, formatNoOffsetW, week, month, day,
(int)hour_from_time(time), (int)min_from_time(time), (int)hour_from_time(time), (int)min_from_time(time),
(int)sec_from_time(time), year, formatAD?ADW:BCW); (int)sec_from_time(time), year, formatAD?ADW:BCW);
else if(offset) else if(offset)
sprintfW(date_str, formatW, week, month, day, sprintfW(buf, formatW, week, month, day,
(int)hour_from_time(time), (int)min_from_time(time), (int)hour_from_time(time), (int)min_from_time(time),
(int)sec_from_time(time), sign, offset/60, offset%60, (int)sec_from_time(time), sign, offset/60, offset%60,
year, formatAD?ADW:BCW); year, formatAD?ADW:BCW);
else else
sprintfW(date_str, formatUTCW, week, month, day, sprintfW(buf, formatUTCW, week, month, day,
(int)hour_from_time(time), (int)min_from_time(time), (int)hour_from_time(time), (int)min_from_time(time),
(int)sec_from_time(time), year, formatAD?ADW:BCW); (int)sec_from_time(time), year, formatAD?ADW:BCW);
date_jsstr = jsstr_alloc(buf);
if(!date_jsstr)
return E_OUTOFMEMORY;
*r = jsval_string(date_jsstr); *r = jsval_string(date_jsstr);
} }
return S_OK; return S_OK;

View File

@ -2155,8 +2155,12 @@ tmp = (new Date()).toLocaleDateString();
ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte"); ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
tmp = (new Date(1600, 1, 1, 0, 0, 0, 0)).toLocaleDateString(); tmp = (new Date(1600, 1, 1, 0, 0, 0, 0)).toLocaleDateString();
ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte"); ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
tmp = (new Date(1600, 1, 1, 0, 0, 0, 0)).toLocaleString();
ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
tmp = (new Date()).toLocaleTimeString(); tmp = (new Date()).toLocaleTimeString();
ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte"); ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
tmp = (new Date()).toString();
ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
tmp = (new Date()).toTimeString(); tmp = (new Date()).toTimeString();
ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte"); ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");