Having some issues with "convert" (RESOLVED)

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
NigelS
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 83
Joined: Sat Oct 22, 2011 2:37 pm

Having some issues with "convert" (RESOLVED)

Post by NigelS » Tue Oct 08, 2013 1:21 pm

Greetings

I'm not sure if this is a bug or not but I'm getting an error when I try the following.

I have two dates that I want to calculate the no of days elapsed. The dates are as follows

Date 1 = 31/08/2012
Date 2 = 10/08/2013

When I try and use 'convert" I get an error on date 1 as been incorrect. The difference is in the year, if I use the current year then no error.

I've tried the various 'convert' syntax's but still no joy. :cry:

I'm using Community 6.1 for Windows. :roll:

Any ides?
Last edited by NigelS on Wed Oct 09, 2013 8:05 am, edited 1 time in total.

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Having some issues with "convert"

Post by bangkok » Tue Oct 08, 2013 1:40 pm

The very good "Date & Time Library"

here :

http://www.troz.net/rev/index.irev?cate ... ary#stacks

... will do the trick (and will teach you how to deal with convert)

look for "daysBetween" function

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Having some issues with "convert"

Post by dunbarx » Tue Oct 08, 2013 1:45 pm

Hi.

The dateformat may be the culprit. In the civilized world, the date "31/08/2013" is usually written "08/31/2013" and this gives a valid conversion. You can modify this format, though into Martian if you wish.

Code: Select all

on mouseUp
   --get "31/08/2012" --works on Mars
    get "3/08/2012" --works on Earth
   convert it to seconds
   answer it
end mouseUp
Craig Newman

NigelS
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 83
Joined: Sat Oct 22, 2011 2:37 pm

Re: Having some issues with "convert"

Post by NigelS » Tue Oct 08, 2013 2:21 pm

Greetings

I have had a look at dayBetween and extrabulted this function and this is where I picked up that the year caused a problem.
This is the script I wrote to calculate the time difference

Code: Select all


# -------

function CalcTimeElapsed pStartTime pEndTime
   
   local tCalculatedElapsedTime
   local tHour,tMin
   local tHourInSec, tMinInSec
   
   convert pStartTime to dateItems    -- LastMsgUpDateTime 
   convert pEndTime to dateItems     -- CurrentTime
   
   -- this'll calculate the elapsed time
   subtract item 4 of pStartTime from item 4 of pEndTime
   subtract item 5 of pStartTime from item 5 of pEndTime
   
   put item 4 of pEndTime into tHour
   put item 5 of pEndTime into tMin
   
   -- convert into sec's
   put tHour*60*60 into tHourInSec
   put tMin*60 into tMinInSec
   put tHourInSec+tMinInSec into tCalculatedElapsedTime
   
   return tCalculatedElapsedTime
   
end CalcTimeElapsed

# -------


This works fine if I ignore the year.

Craig - are you saying that I can't calculate for any day that exist with 2 digits? The format I'm using is mm/dd/yy, should I use a different format?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Having some issues with "convert"

Post by bn » Tue Oct 08, 2013 2:36 pm

Hi Craig,
In the civilized world, the date "31/08/2013" is usually written "08/31/2013" and this gives a valid conversion. You can modify this format, though into Martian if you wish.
greetings from Mars to the civilized world

Kind regards
Bernd

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Having some issues with "convert"

Post by dunbarx » Tue Oct 08, 2013 2:46 pm

No, I just meant that the "dateformat" has to match whatever "date format" you are using. In the U.S., the format is "mm/dd/yy". But if you use a string that goes out of range, that is, the month is greater than 12, for example, the convert command will not do anything at all:

convert "31/08/2012" to dateItems --yields "31/08/2012"
convert "08/31/2012" to dateItems -- yields "2012,8,31,0,0,0,6"

In fact, any unresolvable string will be ignored by the convert command:

convert "08/////31/2012" to dateItems --yields "08/////31/2012"
convert "08/xx/2012" to dateItems --yields "08/xx/2012"

This is in contrast to the "dateItems", which is far more forgiving, and more powerful, in that you modify its items, and it will do the math to make it whole:

Code: Select all

on mouseUp
   get the date
   convert it to dateItems
   add 15 to item 3 of it
   convert it to date
end mouseUp
Craig

NigelS
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 83
Joined: Sat Oct 22, 2011 2:37 pm

Re: Having some issues with "convert"

Post by NigelS » Tue Oct 08, 2013 5:02 pm

Okay... I'm with you now Craig. Here in South Africa our system dates are set to dd/mm/yy but recently a lot of business are switching to mm/dd/yy. This does cause some problems as we always have check first what the system date is set to and convert first. Should have thought of that.

Thx
Nigel

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

Re: Having some issues with "convert"

Post by Klaus » Tue Oct 08, 2013 5:15 pm

Hi Nigel,

can't you use "system date"?
Tried this here with german date format and worked like a charm:
...
## SYSTEM DATE is actually -> 08.10.13, but changing to 2013 also works!
convert "08.10.2013" from system date to dateitems
put it
## As exspected -> 2013,10,8,0,0,0,3
...

Best

Klaus

NigelS
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 83
Joined: Sat Oct 22, 2011 2:37 pm

Re: Having some issues with "convert"

Post by NigelS » Tue Oct 08, 2013 6:34 pm

Klaus

Absolutely, it's something I should have checked first. All I can say is one word "Gotcha". First thing I'll do when I get into the office is try it out. Although I do use yyyy rather than yy. I think my problem lays with with Craig makes mention in his post.

Nigel

NigelS
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 83
Joined: Sat Oct 22, 2011 2:37 pm

Re: Having some issues with "convert" (RESOLVED)

Post by NigelS » Wed Oct 09, 2013 7:59 am

Greetings

:oops: Always check your code. I found my problem and thought I would share what I had done.

The code

Code: Select all


-- format the current date 
      put tCurrentMonth &"/"& tCurrentDay &"/"& tCurrentYear into tTheCurrentDateTime
      put tTheCurrentDateTime && tCurrentTime into tTheCurrentDateTime
      
      put item 1 of tLastMsgUpdate into tTheLastMsgUpdateDate
      put item 2 of tLastMsgUpdate into tTheLastMsgUpdateTime
      
      -- extrabulate the last highsite message date 
      replace "-" with space in tTheLastMsgUpdateDate          
      put item 1 of tTheLastMsgUpdateDate into tMsgYear
      
      put item 2 of tTheLastMsgUpdateDate into tMsgDay
      put PrefixAZero( tMsgDay ) into tMilSeconds
      
      put item 3 of tTheLastMsgUpdateDate into tMsgMonth
      put tMsgMonth &"/"& tMsgDay &"/"& tMsgYear into tTheLastMsgUpdate
      

the problem lay in these line of code

Code: Select all

      put item 2 of tTheLastMsgUpdateDate into tMsgDay
      put PrefixAZero( tMsgDay ) into tMilSeconds
      
      put item 3 of tTheLastMsgUpdateDate into tMsgMonth
I swapped item 2 and item 3 around where Item 2 should be tMsgMonth and Item 3 should be tMsgDay.

So, the moral of the story - Check you code :wink:

Thanks to all who helped with tips and hints.

Post Reply