JScript, Localization and Those Wacky Newfoundlanders

I was talking about localization in general the other day.  Today, some brief notes on localization in JScript Classic.

A while back, a coworker asked me:

Does toLocaleString() do anything different than toString() when locale changes?  I am especially interested date formats when you run code in a place inside the United States that has weird Daylight Savings Time rules.

That's actually quite a complicated question.  I'm going to break that down into many sub-questions.

What locale does JScript Classic use when it needs to change its output based on locale?

It depends on the host.  JScript and VBScript have their own locale settings which may be independent from the host, user and machine settings.  A web browser host could, for example, set the script engine to be French if it were displaying a French web page. 

IE has changed their algorithm (both intentionally and accidentally) many times over the years.  Sometimes they used the locale of the user, sometimes they always defaulted to US-English.  I believe we are now in "always default to US-English" mode in IE.  It's confusing because the script engines have the ability to change the locale used for the error messages independent of the locale used to format dates, numbers, etc.  This allows for exotic scenarios like a VBScript host which displays French error messages while running scripts which use Japanese date formats.

I am not aware of any hosts which actually use these features!  We added them when IE4 shipped, but it turned out that IE didn't need the feature after all.  However, in the interests of completeness, I'm going to document JScript's behaviour as though there actually were a host that exposed this feature.

How many methods are there in JScript intended for localization, and what are they?

Nine.  On the String prototype there are three: toLocaleLowerCase, toLocaleUpperCase and localeCompare. The Date prototype has toLocaleDateStringtoLocaleTimeString, and toLocaleStringThe Array, Object, and Number prototype objects each have an implementation of toLocaleString.

Do any of these methods change their output when the script engine is in a different locale?

Yes, eight of them do.  Of course, they depend on the script engine locale, which as I noted above may different from the user locale, depending on the whim of the script host.

Object.prototype.toLocaleString simply calls toString.  No localization behaviour here unless of course you have overridden the prototype.

Array.prototype.toLocaleString uses the operating system's localized list separator and recursively calls toLocaleString on each of its members to generate a localized, comma-or-whatever-your-locale-prefers separated list.

Number.prototype.toLocaleString uses GetNumberFormat to get the number separator and decimal point.  If for any reason that fails, we fall back to OLE Automation's VariantChangeTypeEx, which is locale-sensitive.

Date.prototype.toLocaleDateStringtoLocaleTimeString, and toLocaleString are complicated by some bizarre weirdnesses in the Win32 NLS API.  To work around various problems, only dates between 1600 and 10000 AD are localized. Hebrew date formats for years after 2240 AD are also not supported.  Once we jump through those hurdles, the Win32 APIs GetDateFormat and GetTimeFormat are used to format the strings.  (I'm vaguely recalling that there was also a bug in there involving the Thai calendar but I don't remember the details.)

String.prototype.toLocaleLowerCase, toLocaleUppercase, toLowerCase and toUpperCase are buggy -- they use the default user locale, not the script engine locale.  Looks like I was careless when I implemented the script engine locale feature.  I wouldn't expect those bugs to be fixed any time soon, since as far as I know, I'm the first person to find the bugs since I implemented them!

They just use LCMapCase to change the case of a string using the localized rules.

String.prototype.localeCompare uses the script engine locale to call CompareString on two Unicode strings.

What about Daylight Savings Time?

The JScript runtime library always determines the local time zone offset by calling into various operating system APIs.  If the operating system gets the DST calculation right in whatever weird state you're in, JScript will also get it right.  If the operating system gets it wrong, complain to the Windows guys, not me.

Speaking of that, exactly which of the United States have weird Daylight Savings Time rules?

They are Alabama, Alaska, Arkansas, California, Colorado, Connecticut, Delaware, Florida, Georgia, Idaho, Illinois, western and eastern portions of Indiana, Iowa, Kansas, Kentucky, Louisiana, Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana, Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina, North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina, South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia, Wisconsin and Wyoming. 

These bizarre states actually legally mandate millions of people to change their clocks twice a year in a confusing, pointless and error-prone light-saving gimmick.

Arizona, Hawaii and central portions of Indiana have a single, sensible, simple DST rule: "we do not observe Daylight Savings Time."

For what it's worth, My Home And Native Land doesn't fare much better.  All young Canadians grow up knowing the phrase "Coming up at two-o'clock-two-thirty-in-Newfoundland..." 

A half-hour timezone?  What the heck?

Yep.  They're half an hour ahead of the rest of us in Newfoundland.  One year Newfoundland decided to try an experiment in "Double Daylight Savings Time" -- they sprang forward TWO hours.  Of course, they were already sprung forward half an hour as it was.  It didn't get dark until 11 PM. There was no end of problems. The failed experiment was described in the Newfoundland assembly a few years later like this:

We were up all night, all day; we could not get to sleep. You could not get a coffee in the morning at Tim Horton's because there was no coffee; there was no morning. It was just going around - the sun never set.  Newfoundland - the place where the sun never set. I think the Minister of Environment at that time was the honourable John Butt.  I believe he led the way in that great endeavour. He had Newfoundlanders going around - they did not know whether they were asleep, whether they should go to bed, whether they should get up. I think it was at 1:00 a.m. that we used to get a little bit of dusk, and before you knew it, it was daylight again. John even had the sun running the way he wanted it. He had the sun slowed down. That is how the Tories operated. He said he did that after consultation with the people. The only thing he consulted with must have been a crystal ball or something. He must have looked into a crystal ball, because before half the summer was out, the Premier of the day was trying to have the time switched back again.

Somehow I managed to segue from localization rules to Newfie partisan politics; this would probably be a good time to stop.

Comments (6)

  1. Steve Clarke says:

    Hey, I’m one of those Wacky Newfoundlanders. 🙂

    Prime time here starts at 9:30PM. All the good shows run until 12:30am. We don’t get to see Saturday Night Live, The Tonight Show, or Letterman until well after midnight.

    I installed .TEXT over the weekend to host our local .NET users group blogs and discovered that Newfoundland time wasn’t included in the time zones. I bought one of the first Palm Pilots – you know when they were still U.S. Robotics – and they didn’t have Newfoundland time. I spent over a year trying to get my first SSL certificate (back in the day) only to discover the reason they wouldn’t validate my request is that they expected to see "Newfound Land" for the province name (I had already tried "New Foundland", another common misspelling).

    I rember the year with double daylight savings time. I was probably 10 or 12 years old. I couldn’t get to sleep very well that year, thought I did love it. I got to stay out much longer in the evenings. I don’t know if I’d like it now.

  2. Actually Eric,

    The reason for DST is blindingly clear here in Washington State. It’s all about the school kids. In the winter, it doesn’t get light until 8:30AM and it gets dark at about 5PM.

    DST allows school to be more closely aligned with daylight hours in the winter. In Alaska, it’s far enough north that DST doesn’t help, similarly, for Arizona it’s far enough south that the skew in daylight hours between winter and summer isn’t as big a deal.

  3. Eric Lippert says:

    I’m not following your point. We don’t have daylight savings time in the winter. In the winter we are on standard time.

  4. I was checking out Officezealot today and I noticed this article from Eric Lippert: JScript, Localization and Those Wacky Newfoundlanders "For what it’s worth, My Home And Native Land doesn’t fare much better. All young Canadians grow up knowing the phrase "Coming up at two-o’clock-two-thirty-in-Newfoundland…" A half-hour timezone? What the heck? Yep. They’re half an hour ahead of the rest of us in Newfoundland. One year Newfoundland decided to try an experiment in "Double Daylight Savings Time" — they sprang forward TWO hours. Of course, they were already sprung forward half an hour as it was. It didn’t get dark until 11 PM. There was no end of problems. " Yes we are indeed quite wacky!! (and I am not even referring to the timezone issue). The half hour thing is a little strange at times but it makes for a heck of a New Year’s Party every year – first province/state in North America to ring in the new year. I also noticed when I signed up at Channel 9 that our timezone wasn’t included in list. Tsk Tsk ;-)…

  5. Jeff says:

    In California, we love our DST – more beach time! As a programmer – I hate it. BTW, I was amazed you enumerated the 47 "bizarre" states versus the 3 "sensible" states. Hmmmm.. I guess that’s one way to look at it. My favorite is Laughlin, Nevada, where the casinos are, across the Colorado river from Bullhead City, Arizona, where the parking lots are, and the time changes on the boat ride!

  6. Eric Lippert says:

    > I guess that’s one way to look at it.

    It is in fact the _sensible_ way to look at it.

    > In California, we love our DST – more beach time!

    See, you’ve fallen into their trap. DST doesn’t give you more time any more than getting up an hour earlier and going to bed an hour earlier gives you more time. It just moves time around, which we can do on our own.

Skip to main content