Hello all,
I have a question on reading dates from strings. I need to read dates from strings like '6 December 2001', but, my Windows being set to Norwegian defaults, the word 'December' is not recognized when using Date's #fromString: [1]. I notice that some methods for creating dates exist in a "...locale:" version as well; what I haven't been able to figure out is how to access other locales than the one that is #current [1]. Do I have to create other locales or something myself, or is there an easy way of reading dates using foreign language settings? Sorry if this post seems clueless -- it's probably because right now I haven't a clue. :-) Thanks in advance for any help! [1]: Selectors may be wrong; haven't got my Dolphin around at the moment. -- Best regards, Ole Martin Halck |
Ole,
> Do I have to > create other locales or something myself, or is there an easy way of reading > dates using foreign language settings? > > Sorry if this post seems clueless -- it's probably because right now I > haven't a clue. :-) Thanks in advance for any help! It sort of works - try the following to see if it works on your machine s := Array new: 12. 1 to: 12 do: [:index | s at: index put: (DateToText new locale: (Locale lcid: 1033); format: 'd MMMM yyyy'; convertFromLeftToRight: (Date newDay: index monthIndex: index year: 2002)). Transcript print: (s at: index); cr]. 1033 is the locale id for US English. Once you have evaluated the above (and checked the contents of s are English and not Norwegian) then try converting back to dates. 1 to: 12 do: [:index | [Transcript print: (DateToText new locale: (Locale lcid: 1036); format: 'd MMMM yyyy'; convertFromRightToLeft: (s at: index)); cr] on: Error do: [:e | ]] It works on my machine but, as that is using proper English as it's default, the test might not be completely accurate. I said it "sort of works" because if you change the locale id to 1036 for French French the first block displays all the months in French as expected but the second part only reconverts 5 of them back into Dates, the rest throw conversion errors. I think it's something to do with character sets but I haven't investigated too deeply. BTW. The easiest way to find the locale id numbers is to evaluate Locale supportedSystemLocales I have to admit though that when I needed something very similar (for the NewsArchiveBrowser) I just used a simple parser that knew the English names of the months - much easier :-) Hope this helps Ian |
"Ian Bartholomew" <[hidden email]> wrote in message
news:jQXm9.12084$J47.1078922@stones... Forget all that, it's a complete load of rubbish. I thought I remembered that the Text -> Date conversion ignored locale (which is, I suppose, I why I wrote my own parser for the NewsArchiveBrowser) and was quite surprised when my earlier tests showed that it _did work. I now realise that the Text -> Date conversions were still being performed in the users Locale and the ones that did work (in my French example) were simply the ones where the first three letters of the month name were the same in both languages - nothing to do with character sets. Sorry! It looks like a simple parser to scan the Strings yourself is the best bet. Ian |
In reply to this post by Ole Martin Halck
Ole
You wrote in message news:anh4p9$ks1$[hidden email]... > Hello all, > > I have a question on reading dates from strings. I need to read dates from > strings like '6 December 2001', but, my Windows being set to Norwegian > defaults, the word 'December' is not recognized when using Date's > #fromString: [1]. I notice that some methods for creating dates exist in a > "...locale:" version as well; what I haven't been able to figure out is how > to access other locales than the one that is #current [1]. Do I have to > create other locales or something myself, or is there an easy way of reading > dates using foreign language settings? Oddly does Windows does not provide a standard mechanism for parsing dates from strings as part of its core internationalisation support, but there is one hidden in the bowels of OLE Automation. Try for example: ((VARIANT fromString: Date today printString) changeType: (AXAutomationConstants at: 'VT_DATE')) value asDate Under the covers this is calling the VarDateFromStr() API function in the OLE Automation library, which is wrapped in Dolphin as OLEAutLibrary>>varDateFromStr:lcid:dwFlags:pdateOut:. Calling this directly, as in the following example, is more efficient: d := DATE new. OLEAutLibrary default varDateFromStr: Date today printString asUnicodeString lcid: Locale systemDefault asParameter dwFlags: 0 pdateOut: d. d asDate Using the API directly also makes it easier for you to choose which Locale to use for the conversion. Using this API you are limited to the range of dates representable in the OLEDATE format, which is 2nd Jan 100 A.D. to 31 December 9999. Regards Blair |
Free forum by Nabble | Edit this page |