Time comparison

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
kenzo
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4
Joined: Thu Feb 28, 2013 3:36 pm
Location: Cedar Falls, IA

Time comparison

Post by kenzo » Thu Feb 28, 2013 4:03 pm

Hi, I am trying to make an app like website "opening hours":

In order to determine if the building is currently open, I need to compare times.

for example:

restaurantA is open from 11:00AM to 10:00PM on monday through Friday
if the current time is 12:00PM on monday, then put "Open" into field.
if the current time is 09:00AM on tuesday, then put "Closed" into field.

I tried to find a suitable function, but I could not. So I decided to make it.
Following code compares 2 times. One of them will be current time but I simplify it.
---------------------------------
on mouseUp
local myTime, myTime2
put 2013,2,17,09,13,21,5 into myTime --I want to compare only "time", so I convert date and time to short time.
put 2013,2,17,11,13,21,5 into myTime2 --But I could not store like 10:00AM as integer. So first I declare dateItems

convert myTime to short time --At this point, myTime = 09:13AM, and myTime2 = 11:13AM
convert myTime2 to short time

if myTime > myTime2 is true then 9:13AM is less than 11:13AM. So the condition should be false.
put "yes" into field 1 but it puts yes.
else
put "no" into field 1
end if
end mouseUp
----------------------

Sometimes it works, but sometimes it does not.

01:00AM and 09:00AM works
01:00AM and 11:00AM does not work.

I do not know why.

I am sorry, this is a bad explanation. I hope you see what I am trying to do.

Thanks for your help,

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Re: Time comparison

Post by sturgis » Thu Feb 28, 2013 4:31 pm

To compare 2 times you can't easily use the short time format.

You could use dateitems where you check each item in question.

So if the dateitems you want to compare are like: 2013,2,17,09,13,21,5 for open and: 2013,2,17,11,13,21,5 for close


then something like

Code: Select all

if (item 4 of myTime >= item 4 of myTime2) AND (item 5 of myTime > item 5 of myTime2) then
put "yes" into field 1 but it puts yes.
else
put "no" into field 1
end if
However, to make it easier you can probably do something like

Code: Select all

put "9:13 AM" into myTime
put 11:00 AM" into myTime2
convert myTime to seconds
convert myTime2 to seconds

if myTime > myTime2 then
put "yes" into field 1
else
put "no" into field 1
end if
Some care must be taken with both possibilities though. If a building opens at 11:00 PM and closes the next day at 5:00 AM you must account for the day change when comparing dateitems or seconds.

To do this with seconds just use the whole date being checked.

Code: Select all

put "11:00 PM 3/1/2013" into time1
put "05:00 AM 3/2/2013" into time2

convert time1 to seconds
convert time2 to seconds
if time1 > time2 then.....
To do this with dateitems, just make sure you do yoru comparisons for all items in question from big to small.

If you need to worry about year, compare year items AND month items AND day items AND hour items AND minutes items AND seconds items (if needed)

Klaus
Posts: 12008
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Time comparison

Post by Klaus » Thu Feb 28, 2013 4:34 pm

Hi kenzo,

you are not the japanese fashion guru, aren't you?

1. welcome to the forum! :D
2. If I understand you correctly you want to check if NOW is (moday to friday) and is in the opening time: >= 11AM and <10PM
Is that correct?

If yes, then you can use "dateitems" (check the dictionary for "dateitems").

Do something like this:

Code: Select all

...
## dateitems uses the 24 hour format!
put 11 into tOpenFrom
put 22 into tOpenUntil
put the seconds into tSecs
convert tSecs to dateitems

## 1. check for monday till friday first:
put item 7 of tSecs into tCurrentWeekOfDay
if tCurrentWeekOfDay > 5 then

     ## NOW is saturday or sunday!
     answer "Go away!"
     exit to top
end if

## 2. Now check for correct time:
put item 4 of tSecs into tCurrentHour
if tCurrentHour >= tOpenFrom AND tCurrentHour < tOpenUntil then
    answer "We are open!"
 else
    answer "Go away!"
end if
...
Best

Klaus

Klaus
Posts: 12008
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Time comparison

Post by Klaus » Thu Feb 28, 2013 4:35 pm

Ah, sturgis beat me for a couple of minutes!
Need to type FASTER! 8)

OK, kenzo, now you have two options to choose from :D

kenzo
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4
Joined: Thu Feb 28, 2013 3:36 pm
Location: Cedar Falls, IA

Re: Time comparison

Post by kenzo » Thu Feb 28, 2013 11:14 pm

Thank you guys! I did not expect that I would get help so quickly.
I am still not sure about basic syntax, especially integer, float, and string.
These concepts seem quite different from python, which is what I know a little bit.

>sturgis
Second one looks easier, and really makes sense.
I did kind of similar thing, but I declared 9:13AM as integer, because I thought I could not convert from string to integer.
So I am surprised I can do that way.

>Klaus
I am actually Japanese, studying abroad in the U.S. I do not why you said that. haha.
Yes, that is exactly what I want to do.
Actually I want to make the app for my university's building hours.
The dining halls and the Wellness center open and close multiple times in a day, so it is pretty difficult to remember.
I want to put the all building hours into the app so I can help the students.

I have not tried those fantastic solutions yet, because I just want to thank you guys as soon as possible.
I got the app academy today, so I will work hard and hopefully be better programmer soon.
I am excited that Livecode reached the all stretch goals. Let's celebrate it.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5600
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Time comparison

Post by jacque » Fri Mar 01, 2013 7:04 am

kenzo wrote:I am still not sure about basic syntax, especially integer, float, and string.
LiveCode does not have typed variables, the engine takes care of that for you. You don't have to declare types or convert between them, which is one of its great advantages. Anything will convert to anything else, pretty much.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Gene
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 75
Joined: Wed Mar 09, 2011 6:48 pm
Location: Northern California

Re: Time comparison

Post by Gene » Fri Mar 01, 2013 7:46 am

I just started working my way through some of the time conversion issues being discussed as a part of starting on a time log of hours worked on projects and I ran across your thread.

I am thinking that doing the computations using a 24 hour clock (see "TwelveHourTime" in the dictionary) might be simpler than having to deal with twelve hour AM/PM cycles each day . It's just a thought, since I'm still trying to resolve that question myself.

kenzo
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4
Joined: Thu Feb 28, 2013 3:36 pm
Location: Cedar Falls, IA

Re: Time comparison

Post by kenzo » Sat Mar 02, 2013 3:55 pm

Thank you guys for additional information.

>jacque
That is so cool. I am wondering if there is a disadvantage of "very high level language".
If more complex the program is smaller advantage live code has, that means Java and Objective-C are better for sophisticated apps.
I am just guessing though.

>Gene
I guess it might be simpler to store the data. By the way I though it is officially called military time.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5600
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Time comparison

Post by jacque » Mon Mar 04, 2013 12:05 am

Gene wrote:I just started working my way through some of the time conversion issues being discussed as a part of starting on a time log of hours worked on projects and I ran across your thread.

I am thinking that doing the computations using a 24 hour clock (see "TwelveHourTime" in the dictionary) might be simpler than having to deal with twelve hour AM/PM cycles each day . It's just a thought, since I'm still trying to resolve that question myself.
Use dateitems. Convert the start and end times to dateitems, then subtract each time item in the start time from the same item in the end time. Then convert the end time back to time again.

Code: Select all

on mouseUp
  put "2:15 PM" into tStart
  put "5:00 PM" into tEnd
  put timeElapsed(tStart,tEnd)
end mouseUp

function timeElapsed pStartTime,pEndTime
  convert pStartTime to dateitems
  convert pEndTime to dateitems
  subtract item 4 of pStartTime from item 4 of pEndTime
  subtract item 5 of pStartTime from item 5 of pEndTime
  convert pEndTime to long time
  return word 1 of pEndTime
end timeElapsed
It returns only the first word because the "AM" or "PM" is spurious.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5600
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Time comparison

Post by jacque » Mon Mar 04, 2013 12:10 am

Kenzo, LiveCode compares very well to lower-level languages. It runs almost as fast as C++, and the language itself is less complex. You can do very sophisticated apps with it.

One thing it does not do is allow your scripts to communicate directly with the OS or the hardware. You need to add externals written in a lower-level language to do that. Fortunately LiveCode has so many built-in functions that the need for externals is rare.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

kenzo
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4
Joined: Thu Feb 28, 2013 3:36 pm
Location: Cedar Falls, IA

Re: Time comparison

Post by kenzo » Sat Mar 16, 2013 5:57 am

>jacque

Thank you for explaining that. Since I am new to programming, I had no idea if I need to know objective-C.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”