Closed Bug 291494 Opened 20 years ago Closed 19 years ago

Javascript toLocaleFormat needed

Categories

(Core :: JavaScript Engine, enhancement)

enhancement
Not set
normal

Tracking

()

VERIFIED FIXED

People

(Reporter: andrew, Assigned: shaver)

References

Details

(Keywords: dev-doc-complete)

Attachments

(2 files)

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b2) Gecko/20050421 Firefox/1.0+
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b2) Gecko/20050421 Firefox/1.0+

There is a lot of duplication of code and a lot of limitations to the way we
handle locale specific date formatting in javascript (such as permission issues
accessingnsIDateTimeFormatter  from unprivileged code) and having to maintain
localized strings such as in dateFormat.properties. Also see bug 158218.

Basically toLocaleDateString and toLocaleTimeString do not provide a enough
flexibility, however a fairly straightforward solution exists. Create a
Date.prototype.toLocaleFormat that is basically a wrapper to strftime. This
would provide the needed flexibility and allow us to consolidate/simplify a lot
of date handling routines at near 0 code size cost.

Reproducible: Always

Steps to Reproduce:
Severity: normal → enhancement
Status: UNCONFIRMED → NEW
Ever confirmed: true
So, yeah.  Because it's just a thin wrapper around strftime, I'm tempted to try
to remove the PRMJTime intermediary, which clamps us to 16 bits of year
(apparently an ECMA problem!).

Anyway, this makes my calendar-hacking a lot more pleasant!
Assignee: general → shaver
Status: NEW → ASSIGNED
ECMA does not restrict years before or since the epoch to fit in 16 bits.  What
gives you that idea?

/be
No, the problem as I read
http://lxr.mozilla.org/mozilla/source/js/src/jsdate.c#1574
is that PRMJTime limits us to 16-bit years, and ECMA requires (reasonably, IMO)
that we do better.
Blocks: 260120
Attachment #181554 - Flags: review?(brendan)
Attachment #181554 - Flags: review?(brendan)
Attachment #181554 - Flags: review+
Attachment #181554 - Flags: approval1.8b2+
(That was me r+a'ing there.)
Flags: testcase?
This is a first attempt at a test for toLocaleFormat. I've tested it on Windows
but don't have Linux to test with. I "think" it will work regardless of
timezone. I've tested in EDT, PDT and RDT. There may be a bug in %I for Russian
time for example, but I'm not entirely positive yet. Help and feedback
appreciated.  

How should the differences between Windows and *nix be handled?

testing the format strings from cygwin man strftime on windows:
see
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strftime.2c_.wcsftime.asp?frame=true&hidetoc=true>


supported and match the man strftime page.
%a, %A, %b, %B, %d, %H, %I, %j, %m, %M, %p, %S, %U, %w, %W, %x, %y, %Y, %Z, %% 


supported but have a different format 
%c, %X, %z 

not supported (are just ignored and toLocaleFormat returns the date like 
"Wed Dec 31 1969 19:00:00 GMT-0500 (Eastern Standard Time)")
%C, %D, %e, %F, %g, %G, %h, %n, %r, %R, %t, %T, %u, %V

With no format string toLocaleFormat returns 
"Wednesday, December 31, 1969 19:00:00"
shaver, can you look at the results for
<http://test.bclary.com/results/mozilla.org/js/2005-06-10-01-11-firefox-1.8b2_2005060903-jsshell-opt-results-js.html>
and tell me which of the failures you consider real?
Some of those are definitely bogus, like

Date.toLocaleFormat("%a %b %e %H:%M:%S %Y") == Date.toLocaleFormat("%c")

but most of the others look like the limitations of strftime on the underlying
libraries.   I'm a little surprised at how few format specifiers it seems to
support, but maybe I shouldn't be.

We knew going in that we'd be limited by the platform's strftime, so maybe these
are just "implementation dependent", like toLocaleString is.  Hrmph.
Looks like this was checked in as:
3.67	shaver%mozilla.org	2005-05-05 09:08	 	Bug 291494: add
Date.prototype.toLocaleFormat for better control over localized Date formatting,
as well as access to things like week-number and Julian dates. r+a=brendan.

Can we mark it fixed?

I added the following test to the library with the offending testcases commented
out.

RCS file: /cvsroot/mozilla/js/tests/js1_5/Date/browser.js,v
done
Checking in browser.js;
/cvsroot/mozilla/js/tests/js1_5/Date/browser.js,v  <--  browser.js
initial revision: 1.1
done
RCS file: /cvsroot/mozilla/js/tests/js1_5/Date/shell.js,v
done
Checking in shell.js;
/cvsroot/mozilla/js/tests/js1_5/Date/shell.js,v  <--  shell.js
initial revision: 1.1
done
RCS file: /cvsroot/mozilla/js/tests/js1_5/Date/toLocaleFormat.js,v
done
Checking in toLocaleFormat.js;
/cvsroot/mozilla/js/tests/js1_5/Date/toLocaleFormat.js,v  <--  toLocaleFormat.js
initial revision: 1.1
done
Flags: testcase? → testcase+
Status: ASSIGNED → RESOLVED
Closed: 19 years ago
Resolution: --- → FIXED
v ff 1.5.0.1, 1.5, 1.6 20060217 win/linux/mac
Status: RESOLVED → VERIFIED
Is this functionality actually documented anywhere? devmo doesn't seem to have anything, either under the 1.5 or 'new to 1.6' docs. I was looking for this kind of functionality the other day and gave up, and I only found this bug whilst looking for something completely unrelated!
http://developer.mozilla.org/en/docs/User:Waldo:Date.toLocaleFormat has some documentation, but that wasn't touched for a year. Trying dev-doc-needed keyword.

Jeff?
Keywords: dev-doc-needed
Bug 395794 morphed into "write documentation for toLocaleFormat".
I'm trying this using the JS shell built against trunk and it's not working.  It's set up to use JS 1.8.  Here's what I get:

js> d = Date();
Wed Oct 03 2007 15:19:25 GMT-0400 (EDT)
js> print(d.toLocaleFormat("%A, %B %e, %Y"));
typein:4: TypeError: d.toLocaleFormat is not a function
js> version();
180

Shouldn't this be working?
I've got the doc posted for it despite the fact that it's not working for me at present. :)

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Date:toLocaleFormat
You want |d = new Date()| not |d = Date()|
update compatibility note for Windows:
/cvsroot/mozilla/js/tests/js1_5/extensions/toLocaleFormat-02.js,v  <--  toLocaleFormat-02.js
new revision: 1.2; previous revision: 1.1
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: