localtime() is broken [might be Territory?]
Rick Murray (539) 13851 posts |
The example is simple:
If I use System configuration is UK territory, daylight savings, and +1h timezone. While this is technically invalid (there is no UK+DST+1h), given the difficulties of creating territory modules, are we going to cling to some pedantic notion of what is a valid time? |
Chris Mahoney (1684) 2165 posts |
I seem to get the same results. In Configure > Time and date I have Locality set to UTC+12 and *Time returns the correct local time. gmtime() also returns the correct UTC time. localtime(), on the other hand, returns UTC if DST is disabled, and UTC+1 if it’s enabled. I don’t know how to set/view the Territory (is there a UI for that or do I have to do it from the CLI?) so I’m guessing that mine’s set to UK. It seems that localtime() always uses your selected territory, which is probably UK on the majority of systems (especially for people like me that have only found RISC OS via the Pi – I didn’t even know that there was such a thing as a “territory” until now). If the UK is a territory then I’m assuming that a territory is usually a country. In that case I shudder to think what happens if you set your territory to a country that spans multiple time zones! |
Rick Murray (539) 13851 posts |
Ought to be a dead easy fix. UTC plus DST offset plus time zone offset. It would appear to be the latter part that we are missing? |
Steve Revill (20) 1361 posts |
Long and short is:
The core of the problem appears to be that you’re an English speaker living outside the UK and you’re trying to bodge RISC OS by using the UK territory with a manually set timezone. Territories are about regions of the earth’s surface and they tie up the language and the geography, that *TIME shows the “right” time is a poor measure of what the territory system is trying to achieve. There is a ‘France’ territory module on the common downloads page (“Territory modules” at the bottom), though I guess you might want to roll a special one with the week/month names in English. Or, for ultimate bodge, add a second timezone within the UK territory and selected that with *CONFIGURE CEST but in both cases leave the timezone configure keyword alone! |
Steve Pampling (1551) 8172 posts |
Hmmm? Which of the official languages of Belgium is tied to the timezone they are in?1 Or should we move a little further inland and ask which of the Swiss languages their timezone is nailed to? Rick was making the point that: Language <> timezone and timezone <> language. 1 Four IIRC, Flemish, French, English, and German. The last being a small enclave |
Steffen Huber (91) 1953 posts |
I don’t know why the behaviour is as strange as Steve describes. The sane behaviour would be: If you have Auto Timezone, use the Territory to determine which timezone is the correct one. If the user has gone to the length of manually setting the (no doubt for him correct) Timezone, just trust the user and use it. Ah, wait. If I have understood all this correctly, I am inclined to say that the currently implemented behaviour is not strange, but broken. Completely broken. |
Frank de Bruijn (160) 228 posts |
English an official language in Belgium? Doubt it. Dutch/Flemish, French and German, as far as I know.
Indeed, totally broken behaviour. |
Steve Pampling (1551) 8172 posts |
I bow to greater knowledge, although I think you’d concede that Wallonians would probably rather speak English than Flemish1 and this is reciprocated in the north. On visits I always used to ask what people would prefer me and friends to attempt to speak: several times the answer was English, you seem quite good at that2, but don’t speak German. 1 Probably rather not speak than not speak French :) |
Rick Murray (539) 13851 posts |
…Territory is Not Fit For Purpose.
And? I want my computer to be fundamentally “British”, only I’m not living in the UK. Given that RISC OS provides no alternative…
(my highlight) Furthermore, it looks as if setlocale() uses a named “country/timezone” pairing; so we can’t even try to read the configured timezone offset and say “bodge it by 60 minutes”. This brings in all sorts of additional complications – not only trying to backparse a timezone into some sort of named version, but the necessity – one would presume – to have some sort of territory support for knowing which timezone names are which, and everybody else using said software to have this. Out of interest, what country would you classify the HNA timezone as? :-)
The core of the problem appears to be that the people that originally designed the Territory system really had little idea about how to implement such a thing. If they had actually bothered to think a little, they might have realised that their close international neighbours offer countries with multiple simultaneous languages. There is a hint at what a bodge this is in the “Canada (we speak English)” (#17) and “Canada (ici on parle francaise)” (#18) territories, or the fact the using a weird keyboard layout switches you to a different territory (#70 vs #1; ditto US).
I’m actually hoping for the supposedly-smart bits of RISC OS to do what the kernel can do effortlessly.
Fair enough. I bet that looks good on a powerpoint slide. :-P Problem is – I’ve just pointed out above that Canada has multiple territories to get around the sticky language issue (and I notice you have “Canada1” and “Canada2” instead of “Canada-except-Québec” and “Québec” :-) ), Switzerland with its four languages and Belgium with its two count as only one country each. “Middle East” encompasses French, maybe English, Hebrew, Arabic, and probably some languages I’ve never heard of. Sure, there is an “Israel” Territory, but Israel is in the “Middle East” isn’t it? “UK” and “UK-with-a-Dvorak-keyboard” are different territories. How does that tie in with language and geography? Where does one select “Cornish” as a language? Or Basque? Or Breton? What is the language of “Ireland”? In fact, we have “Wales”, where is “Scotland” (the other country of the UK (Wales is not a country…yet)) and “Northern Ireland”? Essentially, Territory is Not Fit For Purpose.
That the kernel gets it right and the Territory system does not certainly shows what the Territory system is failing to achieve.
No good.
I do not want this! Good god – we’re supposed to roll our own Territory modules just to get the time to show up right in C programs? Furthermore, it is – ultimately – useless – as it implies that CLib is pathologically unable to provide the correct time to anybody that is not running a custom-hacked Territory. I build my own OS (’cos I can, mainly) – how many people here live outside the UK, speak English, and do not build their own OS but just download the latest one from this site?
That would work for me for the summer. ;-) Anybody here using a British-setup RISC OS in a timezone further apart? What about them?
I think I’m going to poke around the “UK” territory to see if I can patch in timezone offset bodging. It can reply with the timezone names “WNTR” and “SUMR” (for Winter/Summer time), it will be hardwired to the UK start/end dates, but it will take account of the configured TimeZone offset. This should have no effect whatsoever on UK-in-the-UK systems as your offset will be zero. It will, on the other hand, permit overseas “UK” users to have proper times returned to their programs without the nefarious side effects of using alternative “territories”. |
David Feugey (2125) 2709 posts |
What should I do when I’ll go in mission in Korea? |
Rick Murray (539) 13851 posts |
[edit: it’s also a problem in the C library, but I can still tweak the UK territory for additional timezones in the hope of the C library being fixed] Okay – it looks like the quickest and cleanest way may be to ditch the use of Obviously, CET (UTC +1h00) and CEST (UTC +2h00) will be provided. Any others to add? Write the official name and the offset from UTC in the box below… 1 This is still not the best way, any auto-DST stuff will have UK changeover rules, that may work for Europe, but probably not for other places. 2 If there are only a few commonly desired options, all is okay. If many, I’ll probably need to go the TimeZone offset route. Which looks like it’d be messy botching of tables. Hmmm… |
Rick Murray (539) 13851 posts |
Well, I have modified the UK Territory to understand CET and CEST. Doing …returns BST. Okay. Something is going wrong here. I have built a standalone version of the USA territory module, and no matter which of the (non-DST) timezones I select (EST, CST, MST, PST, or AKST) – a program using Curiouser and curiouser…
works as expected, for CEST and PDT. Time to look at what CLib is doing. |
Rick Murray (539) 13851 posts |
Found it. The This can never work for territories with multiple time zones. Proof:
The code could be a lot simpler by just asking Territory for the current timezone… Actually – this would be an all-round win with no modifications necessary to the UK Territory (for now!). |
Rick Murray (539) 13851 posts |
Finally… There’s (still) a lot wrong with Territory, however:
and:
It looks like Territory can actually handle this situation and it’s a library problem1. So on the point of custom timezones with the UK territory, I will apologise to TerritoryManager. Sorry. Sumimasen. Désolé. 1 Just looked at the earliest source available, some 17 years ago, and it did it like that back then, too. Perhaps, one could wonder, it did it this way since forever. It’s late, hot, hard day at the office, blah blah. I’m going to microwave a quiche, eat it, then go to bed. Night guys! |
Chris Mahoney (1684) 2165 posts |
I’m still getting my head around all this, but it looks like multiple territories for Canada, Belgium, etc. is the “correct” way. As per PRM 3-797:
But as you noted, that doesn’t make localtime() work correctly! |
David Feugey (2125) 2709 posts |
Damned. So you should add french (keyboard, language, etc.) to USA, all Europe and part of Asia, since I go very often in these parts of the world… IMHO, it would be more simple to separate timezones from territories. |
Rick Murray (539) 13851 posts |
Or, when the C library is fixed, just set the timezone manually… |
Rick Murray (539) 13851 posts |
Will be submitted “officially”, however to those who want to try it/play with it now: 1. How to add extra timezones to the UK territory This is not necessary, Territory will work with custom timezones. This is so you can do Open the UK Territory source – …castle.RiscOS.Sources.Internat.Territory.Module.s.UK – near to the top you will see:
Alter this to suit the timezones you are adding – “CEST” would be ‘4’. Just below this, you will see:
Alter this according to the number of timezones you are adding. DST/NoDST counts as one timezone, so to add CET/CEST, we’d set this to be two. You will then see this:
What we need to do is basically copy this, change all the zeroes to ones, and then fill in the correct information. To add CET/CEST, we would add this following the block shown above:
A subsequent timezone would be NODST2, DST2, NODSTOffset2, and DSTOffset2; and so on. Save, rebuild your ROM, you’re all set. Tip! If you want to build a non-ROM version of the module for testing that it works, go to the parent directly and you’ll see a load of taskobey files called !Mk<something>. These are the commands to make the relevant territory modules.
Change this to say:
and then you just need to load the !Builder application (sets paths and stuff) and then double-click on “!MkUKSA” to build a standalone version of the UK territory. [same applies for the other territories] |
Rick Murray (539) 13851 posts |
And now for the important modification: 2. Fixing localtime() to work correctly The source for the localtime function is at the end of this file – castle.RiscOS.Sources.Lib.RISC_OSLib.c.time – and rather than provide bits to change, I’m just going to list the entire function. Copy-paste this in instead of the existent version…
The previous behaviour has been retained for specific locales. This has not been tested, but I’m assuming it works and was tested as part of the development of the specific-locales code, for stuff like passing “USA/PST” to For our needs, if we are running in the default locale (which you will be unless you specify otherwise), we ignore the DST flag (it is passed to Tested with UK: GMT, BST, CET, CEST. In all cases, the time returned by Thunderstorm approaching, time to unplug the internet. Don’t want to lose another Livebox… |
Chris Mahoney (1684) 2165 posts |
A couple of questions:
|
Rick Murray (539) 13851 posts |
Documentation – PRMs? If not, yeah, I’d just clone one of the existing modules. But note that there is a massive lurking “gotcha!”. Unless you are adding a new language, it would be bad to clone a module to try to add different behaviour to an English language system; because just about everything assumes that the territory name indicates the language. |
Rick Murray (539) 13851 posts |
Time for me to duck out for today. Walking around the field in between the rain and thunder clouds to try to get a good mobile signal is kind of silly. |
Chris Mahoney (1684) 2165 posts |
I couldn’t find anything in the PRMs but admittedly I didn’t have a thorough look.
Time for me to play around with the USA and Australia territories and see what happens. Presumably the system still works when they’re selected! |
Rick Murray (539) 13851 posts |
I have put together some ideas for ways we could enhance the territory/language system. You have “comment” access, so don’t be afraid to annotate. ;-) https://docs.google.com/document/d/15RwrmIzUF8r9iJOXX2BEXeMvJkYYSut88yJNaE1cozQ/edit?usp=sharing Doesn’t need you to sign in to annotate (just tried with iOS/Dolphin), so please append your forum name if you aren’t signed in, m’kay? Thanks. |
Steve Pampling (1551) 8172 posts |
Following my earlier comment you will appreciate that I totally agree that language and territory or country are not synonymous. One small amendment: Belgium has three official languages (I looked it up following the response from Frank) Note: English is widely spoken but not official. |