Problem with Convert and Danish

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
rrBUSS9EE
Posts: 98
Joined: Wed May 02, 2012 3:46 pm

Problem with Convert and Danish

Post by rrBUSS9EE » Mon Jul 02, 2012 1:41 pm

Hi All…

I am parsing XMLfiles from a device, and the Danish version of this device formats "March 5, 2012" as "Marts 5, 2012". Problem is, LC's convert command does not recognize this as a valid date even on systems set to Danish date formatting. I have tried setting the useSystemDate just before converting without success.

Both HyperCard and SuperCard handle this without issue. How can I get LiveCode to handle this correctly?


Thanks.

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problem with Convert and Danish

Post by Klaus » Mon Jul 02, 2012 1:57 pm

Hi,

what did you script so far?
Did you use the form "convert xxx FROM zzz to yyy"?


Best

Klaus

rrBUSS9EE
Posts: 98
Joined: Wed May 02, 2012 3:46 pm

Re: Problem with Convert and Danish

Post by rrBUSS9EE » Mon Jul 02, 2012 2:14 pm

Hi Klaus,

Code: Select all

   
set the usesystemdate to true
 --   answer the usesystemdate
get fld "date"
convert it from long date to dateitems --also tried just 'convert it to dateitems'
put it into fld "dateitems"
put the monthnames --odd that this actually puts the correct Danish month names

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problem with Convert and Danish

Post by Klaus » Mon Jul 02, 2012 2:26 pm

Hi,

please re-read the part about "convert" in the docs :)
You need to tell LiveCode what the SOURCE format is, as I showed in my first reply!
I you don't then LC will think the SOURCE format is an ENGLISH date!

Try this:
...
get fld "date"

## !!!
convert it from long SYSTEM date to dateitems
## !!!

put it into fld "dateitems"
...

Best

Klaus

rrBUSS9EE
Posts: 98
Joined: Wed May 02, 2012 3:46 pm

Re: Problem with Convert and Danish

Post by rrBUSS9EE » Mon Jul 02, 2012 2:50 pm

Thanks Klaus… but I am afraid it still returns "marts 5, 2012"

Code: Select all

   set the usesystemdate to true
   get "marts 5, 2012"
   convert it from long system date to dateitems
   answer it
BTW… the docs say

"You can optionally use the english or system keyword before the short, abbreviated, or long date or time. If the useSystemDate is true, or if you use the system keyword, the user's current system preferences are used to format the date or time. Otherwise, the standard US date and time formats are used."

So I took this to mean setting the usesystemdate was enough.

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problem with Convert and Danish

Post by Klaus » Mon Jul 02, 2012 3:30 pm

Oh, sorry, overlooked that one, sure that should be enough for "convert".

But I am afraid this is no a convertible date format at all!?
"the long system date" and "the abbr system date" ALWAYS show the weekdayname here, which is missing in your example,
so "convert" obviously cannot convert from this "unkown" format to anything else!

Abbreviated system date: Mon, Jul 2, 2012
Long system date: Montag, 2. Juli 2012

What do you get with:"the long system date" and "the abbr system date"?

I bet the leading week day name, right?
Then you are out of luck 8)


Best

Klaus

rrBUSS9EE
Posts: 98
Joined: Wed May 02, 2012 3:46 pm

Re: Problem with Convert and Danish

Post by rrBUSS9EE » Mon Jul 02, 2012 5:00 pm

"The long system date" returns "mandag 2. juli 2012".

"The abbr system date" returns "man 2. jul. 2012".

But if it is aware of the proper month names (the monthNames), why does it not see "marts" as "March" in the same way that HyperCard and SuperCard do?

Oh well… I'll chalk this one up to just another one of LC's inexplicable behaviors. I suppose I can add a routine to walk the list of monthNames to determine just what month it is.

Thanks!

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problem with Convert and Danish

Post by Klaus » Mon Jul 02, 2012 6:23 pm

Well, get used to it and don't shoot the messenger, thanks 8)

rrBUSS9EE
Posts: 98
Joined: Wed May 02, 2012 3:46 pm

Re: Problem with Convert and Danish

Post by rrBUSS9EE » Mon Jul 02, 2012 6:34 pm

Here's my little fix:

Code: Select all

function americanize thedate
   set the useSystemDate to true
   put the monthnames into theMonths
   put word 1 of the date into theMonth
   get lineoffset(theMonth,theMonths)
   switch it
      case 1
         replace themonth with "January" in thedate
         break
      case 2
         replace themonth with "February" in thedate
         break
      case 3
         replace themonth with "March" in thedate
         break
      case 4
         replace themonth with "April" in thedate
         break
      case 5
         replace themonth with "May" in thedate
         break
      case 6
         replace themonth with "June" in thedate
         break
      case 7
         replace themonth with "July" in thedate
         break
      case 8
         replace themonth with "August" in thedate
         break
      case 9
         replace themonth with "September" in thedate
         break
      case 10
         replace themonth with "October" in thedate
         break
      case 11
         replace themonth with "November" in thedate
         break
      case 12
         replace themonth with "December" in thedate
         break
      default
   end switch
   return theDate
end americanize

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problem with Convert and Danish

Post by Klaus » Mon Jul 02, 2012 6:45 pm

Hi,

fine, but you can make it a tad shorter:

Code: Select all

function americanize thedate
   set the useSystemDate to true
   put the monthnames into theMonths
   put word 1 of the date into theMonth
   put lineoffset(theMonth,theMonths) into tTargetLine

   ## Now get the list of ENGLISH monthnames:
   set the useSystemDate to FALSE
   put the monthnames into theEnglishMonths
  
   ## We already have the list of english monthnames, we only need to get the correct line:
   put line tTargetLine of tEnglishMonths into tTargetMonth

   ## Now replace
   replace themonth with tTargetMonth in thedate
   return theDate
end americanize
:D

Best

Klaus

rrBUSS9EE
Posts: 98
Joined: Wed May 02, 2012 3:46 pm

Re: Problem with Convert and Danish

Post by rrBUSS9EE » Mon Jul 02, 2012 6:51 pm

Nice. But you did not account for lineOffset returning 0.

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problem with Convert and Danish

Post by Klaus » Mon Jul 02, 2012 6:56 pm

You are correct! :D

Code: Select all

function americanize thedate
   set the useSystemDate to true
   put the monthnames into theMonths
   put word 1 of the date into theMonth
   put lineoffset(theMonth,theMonths) into tTargetLine

   ## Here we are :-)
   if tTargetLine = 0 then
    return thedate
  end if

   ## Now get the list of ENGLISH monthnames:
   set the useSystemDate to FALSE
   put the monthnames into theEnglishMonths
  
   ## We already have the list of english monthnames, we only need to get the correct line:
   put line tTargetLine of tEnglishMonths into tTargetMonth

   ## Now replace
   replace themonth with tTargetMonth in thedate
   return theDate
end americanize
Best

Klaus

Post Reply