Timed script/function in Lua - lua

good morning friends... I am working on with Lua scripting language for a mobile app and have a requirement as follows -
The application's main aim is to schedule appointments for an individual with a Doctor.
So once a user's appointment is scheduled, for e.g. 8th May # 4:30 PM, the user should receive a "reminder alert" before an hour i.e. # 3:30 PM.
am absolutely having a blank mind on how to get this done.
I can get the user's date-time value and use the logic that a function should invoke just before 60 mins of that date-time. And that function contains my "Alert message".
But how to do this?
Can anyone guide me with a clue?
Please let me know if any other inputs are required...
Thanks in advance.

I would take an approach like this:
1.
Store each appointment's details as a .txt file containing JSON or Lua tabular data something like this:
{
date = "14:30 01/07/2013";
dateBooked = "09:30 23/06/2013";
venue = "31 Dentist Street";
appointmentType = "Routine Teeth Cleaning";
}
2.
You can have a timer class like so
Timer = {}
Timer_mt = { __index = Timer; __add = function(a,b) a:tickBy(b) end ; }
function Timer:new(delayTime,callBack)
local timer = {callBack=callBack}
timer.initTime = os.date() --MM/DD/YY HH:MM:SS
--delayTime = HH:MM:SS
_,_,hour,minute,second = string.find(delayTime,"(%d%d):(%d%d):(%d%d)")
timer.delay = {hour=hour,minute=minute,second=second}
--time the timer started
_,_,hour,minute,second = string.find(timer.initTime,"(%d%d):(%d%d):(%d%d)")
timer.startTime = {hour=hour,minute=minute,second=second}
--time the timer started
timer.initTime = os.date() --MM/DD/YY HH:MM:SS
print(timer.initTime)
_,_,hour,minute,second = string.find(timer.initTime,"(%d%d):(%d%d):(%d%d)")
timer.currentTime = {hour=hour,minute=minute,second=second}
return setmetatable(timer,Timer_mt)
end
function Timer:tick() --returns true if time expired
currTime = os.date() --MM/DD/YY HH:MM:SS
_,_,chour,cminute,csecond = string.find(currTime,"(%d%d):(%d%d):(%d%d)")
if chour - self.startTime.hour >= tonumber(self.delay.hour) and cminute - self.startTime.minute >= tonumber(self.delay.minute) and csecond - self.startTime.second > tonumber(self.delay.second) then
self:callBack()
self.startTime.hour,self.startTime.minute, self.startTime.second = chour,cminute,csecond
--return true
end
--return false
end
t = Timer:new("00:00:02",function () print("DONE") end)
print(t.currentTime.hour,t.currentTime.minute,t.currentTime.second)
while t:tick() or true do
io.read()
end
(I just made this up so I advice you test it but it seems to work for me).
3.
On start-up, or when a new appointment is added create a new timer, and then tick() each one at some point during the main execution, you could even have a timer which is the only one you tick() and it's callback ticks() the others... Anyway set the callback for each timer to display an alarm

Related

How to compare dates doing a test rspec?

Why my test comparing times are failing sometimes? how can i improve this test?
data = { init: Time.zone.now, end: Time.zone.tomorrow }
put "/api/...", params: data,
expect(JSON.parse(response.body)['time'].to_time).to be_within(1.seconds).of(Time.zone.now)
I would like to solve this to pass always the test
Your problem is that you call Time.now multiple times and between calls, it may have changed. Better doing something like this:
now = Time.zone.now
interval = 10.seconds
data = { init: now - interval, end: now + interval }
response = put "/api/...", params: data
expect(JSON.parse(response.body)['time'].to_time).to be_within(interval).of(now)
I would also advise to compare date as integer to prevent any timezone difficulties in your test:
response_time = JSON.parse(response.body)['time']
expect(response_time.to_time.to_i).to be_within(interval).of(now.to_i)

Validate given date is yesterday date

I'm new to lua programming and I'm working on license verification in kong.
I want verify the expiry date with current date.
How do I verify in lua script.
-- Returns true if the given time is in the past.
function dateExpired(expirationTime)
return os.difftime(os.time(), expirationTime) < 0
end
Note that expirationTime is a time value as returned by os.time() or os.date(). If you have your date saved as a table, you can simply feed it through os.time() first: dateExpired(os.time{year=2018, month=5, day=22})
I made this function for you
function verifyExpiration(expirationDate)
local expirationTime = os.time(expirationDate)
local currentTime = os.time()
local result = false
if (expirationTime < currentTime) then
result = true
end
return result
end
It will return:
true if expired
,false if have not expired
and here is some example of how it works:
> expT = {year=2018, month=1, day=1}
> verifyExpiration(expT)
> print(verifyExpiration(expT))
true
> expT = {year=2019, month=1, day=1}
> print(verifyExpiration(expT))
false

Formatting Time

This may seem like an extremely basic question but it has been eluding me for quite some time. I am trying to setup, in my controller, a way to display the time for a specific day based on that time. The time value for example I would want to display would be 10:00 AM from the value 10:00:00. I cannot seem to format the time correctly so that it can display it in that form. Here is my current code that the value will pass through:
def dayMap = new JSONArray()
daysofWeek.each{last ->
def testjsonObject = new JSONObject()
c.setTime(last.date)
int test = c.get(Calendar.DAY_OF_WEEK)
testjsonObject.put('dayofweekNumber', test)
testjsonObject.put('start_time', last.start_time)
testjsonObject.put('end_time', last.end_time)
dayMap.add(testjsonObject)
}
def weekStartTimeString = ""
def weekEndTimeString = ""
List finalList = []
dayMap.each{numberDay ->
if(numberDay.dayofweekNumber == Calendar.MONDAY){
if(numberDay.start_time.equals("09:00:00")){
weekStartTimeString += "<option value='09:00:00' selected>9:00 AM</option>"
println(numberDay)
}
else{
weekStartTimeString += "<option value='numberDay.start_time' selected>"
}
}
}
I would like to take the value numberDay.start_time and display it in that format. 10:00:00 to 10:00 AM. What is the best way of doing this as I am currently out of ideas.
Parse the time into a date object, then convert it back to a string in the format you want. For example:
def start_date = '10:00:00'
assert Date.parse('HH:mm:ss', start_date).format('hh:mm aa') == '10:00 AM'
start_date = '23:59:59'
assert Date.parse('HH:mm:ss', start_date).format('hh:mm aa') == '11:59 PM'
The date format strings are the same ones used by the Java SimpleDateFormat class.

how to iterating days

I have a code for looping days.this to make leaves. I want column signin and signout on attendance will filling automatically start at startdate and end at enddate.
exp I input :
startdate: 2012-11-08 01:30:00
enddate: 2012-11-10 01:30:00
i want output like this:
2012-11-08 01:30:00
2012-11-09 01:30:00
2012-11-010 01:30:00
for i in 0..((#leafe.enddate - #leafe.startdate).to_i)
#attendance = Attendance.new
#attendance.signin = '2012-11-08 01:30:00' #value must chang automatically
#attendance.signout = '2012-11-08 10:30:00'#value must chang automatically
#attendance.user_id = #leafe.user_id
#attendance.save
end
thanks before
If startdate is of type Date, why not just do,
#attendance.signin = #leafe.startdate+i
#attendance.signout = #leafe.startdate+i
Why you using for loop? It looks like javacode.
Use instead block with range like:
(Time.now.beginning_of_day.to_i..Time.now.to_i).step(3600) do |n|#step one hour for example
#attendance = Attendance.new
.......
#attendance.signin = #leafe.startdate
#attendance.signout = #leafe.startdate + n # u can change time interval to any what you want
end

Can using Chronic impair your sense of time?

Haha..
I'm using Chronic to parse the time users add in the Calendar. Where the code works and implements the right time, the end result is that, IF a user adds a time, then it has no date, and because it has no date, it will not show in results. Any ideas?
def set_dates
unless self.natural_date.blank? || Chronic.parse(self.natural_date).blank?
# check if we are dealing with a date or a date + time
if time_provided?(self.natural_date)
self.date = nil
self.time = Chronic.parse(self.natural_date)
else
self.date = Chronic.parse(self.natural_date).to_date
self.time = nil
end
end
unless self.natural_end_date.blank? || Chronic.parse(self.natural_end_date).blank?
# check if we are dealing with a date or a date + time
if time_provided?(self.natural_end_date)
self.end_date = nil
self.end_time = Chronic.parse(self.natural_end_date)
else
self.end_date = Chronic.parse(self.natural_end_date).to_date
self.end_time = nil
end
end
end
Edit:
Here is the time_provided? method:
def time_provided?(natural_date_string)
date_span = Chronic.parse(natural_date_string, :guess => false)
(date_span.last - date_span.first).to_i == 1
end
First, I'm not really sure what are you asking about, because it looks like the code intentionally does what you describe... When there's time provided, the date fields are assigned nil. And I don't think that is Chronic is to blame because that's how your code works.
Not knowing your design (why there are separate date & time fields), the types of fields etc., I would suggest starting with a little kludge like this:
if time_provided?(self.natural_date)
self.time = Chronic.parse(self.natural_date)
self.date = self.time.to_date
or:
self.end_date = Chronic.parse(self.natural_date).to_date
if time_provided?(self.natural_date)
self.time = Chronic.parse(self.natural_date)
end
Or maybe the problem is outside the code you provided: in the part that is responsible for the "because it has no date, it will not show in results" behavior? Maybe you should make the conditions more flexible?

Resources