From: Stefan Weiss on
On 24/04/10 03:11, Thomas 'PointedEars' Lahn wrote:
> Stefan Weiss wrote:
>
>> By the way, there's a project which is trying to port all PHP functions
>> to JS, for some reason. I didn't look at their code, but you may get
>> some inspirations from their implementation of the date() function:
>>
>> http://phpjs.org/functions/index
>
> Like how NOT to do it? It looks like the expected result of a million code
> monkeys educated probably by w3schools.com, adding to PPK's misconceptions,
> starting with undeclared `that = this' when `that' is not ever used, and
> substr() calls.

As I said, I didn't look at the code. Undeclared and unused variables?
That doesn't look good. I don't have a problem with the substr() calls.
That method is pretty well supported, and since they say they're porting
PHP functions to *JavaScript* (tm), substr() should be available ;)


--
stefan
From: Garrett Smith on
Stefan Weiss wrote:
> On 24/04/10 03:11, Thomas 'PointedEars' Lahn wrote:
>> Stefan Weiss wrote:
>>
[...]

> As I said, I didn't look at the code. Undeclared and unused variables?
> That doesn't look good. I don't have a problem with the substr() calls.
> That method is pretty well supported, and since they say they're porting
> PHP functions to *JavaScript* (tm), substr() should be available ;)

I did not immediately notice a call to String.prototype.substr, however
that method should be avoided. The did not use that method in their
`substr` method.

ECMA-262 Non-normative Annex defines String.prototype.substr

| The substr method takes two arguments, start and length, and returns
| a substring of the result of converting this object to a string,
| starting from character position start and running for length
| characters (or through the end of the string is length is undefined).
| If start is negative, it is treated as (sourceLength + start) where
| sourceLength is the length of the string. The result is a string
| value, not a String object.

Testing that with an start=-1 and length=-1, the last character of a
string can be obtained.

var lastChar = "foo".substr(-1, -1);
alert(lastChar);

Internet Explorer Result:
"foo"
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Stefan Weiss on
On 24/04/10 07:35, Garrett Smith wrote:
> Stefan Weiss wrote:
>> I don't have a problem with the substr() calls.
>> That method is pretty well supported, and since they say they're porting
>> PHP functions to *JavaScript* (tm), substr() should be available ;)
>
> I did not immediately notice a call to String.prototype.substr, however
> that method should be avoided. The did not use that method in their
> `substr` method.
>
> ECMA-262 Non-normative Annex defines String.prototype.substr
>
> | The substr method takes two arguments, start and length, and returns
> | a substring of the result of converting this object to a string,
> | starting from character position start and running for length
> | characters (or through the end of the string is length is undefined).
> | If start is negative, it is treated as (sourceLength + start) where
> | sourceLength is the length of the string. The result is a string
> | value, not a String object.
>
> Testing that with an start=-1 and length=-1, the last character of a
> string can be obtained.
>
> var lastChar = "foo".substr(-1, -1);
> alert(lastChar);
>
> Internet Explorer Result:
> "foo"

Which Internet Explorer? I get "" as a result if length is negative (in
any browser), but I think you meant

var lastChar = "foo".substr(-1, 1);

which returns "f" in MSIE, and is indeed not working as described in the
(non-normative) annex. There's no mention of negative values for the
"start" argument on MSDN [0].

I finally did look at the date() function from PHP.js, and they're not
using any negative argument values, so that shouldn't be a compatibility
problem even with Microsoft's idea of substr(). But I agree that due to
the different behavior, substring() is a more portable alternative, and
should be preferred.


[0] http://msdn.microsoft.com/en-us/library/0esxc5wy(v=VS.85).aspx

--
stefan
From: Garrett Smith on
Stefan Weiss wrote:
> On 24/04/10 07:35, Garrett Smith wrote:
>> Stefan Weiss wrote:

[...]

> Which Internet Explorer? I get "" as a result if length is negative (in
> any browser), but I think you meant

You're right, sorry. However it is clear that JScript does not follow
the spec's non-normative recommendation.

IE7, using JScript 5.7:
"foo".substr(-1)
"foo"

"foo".substr(-1, -1)
""

String.prototype.slice works consistently as specified.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Dr J R Stockton on
In comp.lang.javascript message <21e70fcb-f302-49f6-a8b5-7a2834a77928(a)5g
2000yqj.googlegroups.com>, Thu, 22 Apr 2010 17:46:38, nick
<nick___(a)fastmail.fm> posted:
>On Apr 22, 9:56�am, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk>
>wrote:
>> [nick:]
>> >ISO-8601 (example: 2005-08-15T15:52:01+0000)
>> >const string DateTime::ISO8601 = Y-m-d\TH:i:sO ;
>> >O �Difference to Greenwich time (GMT) in hours �Example: +0200
>>
>> That should really be to UTC. �And it is not in hours - 200 hours is
>> 8 1/3 rimes round the world - but in hours & minutes.
>
>Good catch on the hours. Why the distinction between GMT and UTC?

The distinction is that GMT had 86400 seconds per day every day, and to
agree with the Sun those seconds vary slightly in length, whereas UTC
uses SI seconds (derived from Caesium) and occasionally needs to add (or
subtract) one at the end of a half-year. The term GMT is technically
obsolete AIUI, and UT is a modern equivalent. But UT looks like a typo
for UTC. IIRC, by law UK time is GMT; but the time signals are UTC.
The difference between UT & UTC is not allowed to exceed 0.9 seconds.

The USA prefers not to use GMT because their Greenwich is disreputable &
in the wrong longitude.




>The title of 5.4.2 is "Representations other than complete." What do
>you suppose that means?

That's 4.4.5 in my 3rd Edition of 2004 : is yours earlier or later?
Either it's 2000 (1998 had no 5.4.2) or it's later than 2004 (in which
case I want a PDF of it!) (and someone should tell Wikipedia!).

It means when some of the full YYYYMMDDThhmmss or YYYY-MM-DDThh:mm:ss is
omitted.


>> You should provide a version which is correct, and if necessary an
>> alternative compatible withwww.php.net.
>
>Ok, but with different names from the PHP constant.. maybe ISO8601_E
>and ISO8601_B. ISO8601 keeps the PHP behavior.

It seems wrong to include "ISO8601" for something non-compliant.
"PHP8601"?


>> My DATE2 provides, AFAIR, all reasonable formats and useful
>> combinations.
>
>Cool, so you should be able to tell me if I've done a good job when I
>finish mine ;)

DATE2 Format String "a, D o Y Y-r-D Y-O X-WV-C YMD" can give
"Mon, 31 Dec 2001 2001-xii-31 2001-365 2002-W01-1 20011231"
in which the "xii" is a single Unicode character. For three-character
xii, set DATE2.Mnth.

new DATE2().getXEasterSunday() -> Sun, 2010 Apr 04, 00:00:00 GMT
or Sun, 2010 Apr 04, 00:00:00 GMT+0100
// for UTC, change G to U in DATE2.Fmt

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.