Validate given date is yesterday date - lua

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

Related

Requirement is not a valid member of Part "Workspace.Door"

I don't know what's wrong with this. I'm just trying to make an opening zone system and it says
Requirement is not a valid member of Part "Workspace.Door"
The script:
script.Parent.Touched:Connect(function(hit)
if hit then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player.leaderstats.Coin.Value >= script.Parent.Requirement.Value then
player.leaderstats.Coin.Value -= script.Parent.Requirement.Value
script.Parent.Transparency = 10
script.Parent.CanCollide = false
end
end
end)

Trying to build a table of unique values in LUA

I am trying to build a table and add to it at the end each time I get a returned value that is not already in the table. So basically what I have so far is not working at all. I'm new to LUA but not to programming in general.
local DB = {}
local DBsize = 0
function test()
local classIndex = select(3, UnitClass("player")) -- This isn't the real function, just a sample
local cifound = False
if classIndex then
if DBsize > 0 then
for y = 1, DBsize do
if DB[y] == classIndex then
cifound = True
end
end
end
if not cifound then
DBsize = DBsize + 1
DB[DBsize] = classIndex
end
end
end
Then later I'm trying to use another function to print the contents of the table:
local x = 0
print(DBsize)
for x = 1, DBsize do
print(DB[x])
end
Any help would be much appreciated
Just store a value in the table using your unique value as a key. That way you don't have to check wether a value already exists. You simply overwrite any existing keys if you have it a second time.
Simple example that stores unique values from 100 random values.
local unique_values = {}
for i = 1, 100 do
local random_value = math.random(10)
unique_values[random_value] = true
end
for k,v in pairs(unique_values) do print(k) end

Find out if current time is between two times

I have two time columns stored in a Postgresql database: open_time and close_time. I'm trying to find out if the current time, ignoring the date, is between the two times, ignoring the dates.
This code compares the dates as well as the time:
current_time = Time.now
if current_time.between?(store.open_time, store.close_time)
puts "IN BETWEEN"
end
It doesn't work, for example, when current_time # => 2018-06-06 23:59:49 -0600 and open_time # => 2000-01-01 22:59:00 UTC.
How do I get it to not include the dates, and just compare the times?
require 'time'
TIME_FMT = "%H%M%S"
def store_open_now?(open_time, close_time)
nt = Time.now.strftime(TIME_FMT)
ot = open_time.strftime(TIME_FMT)
ct = close_time.strftime(TIME_FMT)
ot <= ct ? (nt >= ot && nt <= ct) : (nt >= ot || nt <= ct)
end
As I write, the time is now about 32 minutes past midnight.
Time.now.strftime(TIME_FMT)
#=> "003252"
Suppose
open_time = DateTime.parse("09:00")
#=> #<DateTime: 2018-06-07T09:00:00+00:00 ((2458277j,32400s,0n),
# +0s,2299161j)>
close_time = DateTime.parse("17:00")
#=> #<DateTime: 2018-06-07T17:00:00+00:00 ((2458277j,61200s,0n),
# +0s,2299161j)>
Then
open_time.strftime(TIME_FMT)
#=> "090000"
close_time.strftime(TIME_FMT)
#=> "170000"
store_open_now?(open_time, close_time)
#=> false
Now suppose the open time is the same, but the close time is later.
close_time = DateTime.parse("01:00")
#=> #<DateTime: 2018-06-07T01:00:00+00:00 ((2458277j,3600s,0n),
# +0s,2299161j)>
Then
close_time.strftime(TIME_FMT)
#=> "010000"
store_open_now?(open_time, close_time)
#=> true
Perhaps you want something like this:
current_time = Time.now
open_time = store.open_time
close_time = store.close_time
current_time -= current_time.beginning_of_day
open_time -= open_time.beginning_of_day
close_time -= close_time.beginning_of_day
if current_time.between?(open_time, close_time)
puts "IN BETWEEN"
end
or
current_time = Time.now
open_time = store.open_time
close_time = store.close_time
current_time = [current_time.hour, current_time.min, current_time.sec]
open_time = [open_time.hour, open_time.min, open_time.sec]
close_time = [close_time.hour, close_time.min, close_time.sec]
if open_time <=> current_time == -1 and current_time <=> close_time == -1
puts "IN BETWEEN"
end
You could CAST() your datetime to time by using,
cast(tbl_store.open_time as time) as SomeVariable
cast(tbl_store.close_time as time) as SomeOtherVariable
That would give you the time only instead of the full datetime value that you had to begin with, which is what you wanted.
You can then use the same logic with your curtime() between to the get value that you were looking for.
Example:
SELECT
CAST(tbl_store.open_time as TIME) as open_time,
CAST(tbl_store.close_time as TIME) as close_time,
CURTIME() BETWEEN (cast(tbl_store.open_time as TIME)) AND (cast(tbl_store.close_time as TIME)) as time_between
FROM
tbl_store
Working SQL Fiddle
You can change the schema build in the fiddle to test the datetime values you desire.
Note that if you ever have a logic that will include midnight time, you will have to make a CASE WHEN logic against that, else it will fail and return 0, whereas it should return 1.
You can take advantage of ranges and how numeric strings are compared
r = Range.new('09:00', '18:00')
r.include?('08:59') # => false
r.include?('09:01') # => true
r.include?('18:01') # => false
Then we could use
open_hours_range = Range.new(open_time.strftime('%R'), close_time.strftime('%R'))
shop_open? = open_hours_range.include?(Time.now.strftime('%R'))

How to check if time is in range using Rails

There is the following times:
now = "2014-01-24T15:58:07.169+04:00",
start = "2000-01-01T10:00:00Z",
end = "2000-01-01T16:00:00Z"
I need to check if now is between start and end. I use the following code:
Range.new(start, end).cover?(now)
Unfortunately, this code returns false for my data. What am I doing wrong? How can I fix it? Thanks.
Well, I would use between? method. Because it's faster than cover? and include? variants. Here's an example:
yesterday = Date.yesterday
today = Date.today
tomorrow = Date.tomorrow
today.between?(yesterday, tomorrow) #=> true
Here's a gist with performance tests Include?, Cover? or Between?
Update
According to your recent comment, you want to compare 'only time' without date. If I get you correctly, there's a way to do it - strftime. But before that, to make comparison correctly, you need to convert all your datetimes to a single timezone (for example, using utc). Here's an example:
start_time_with_date = Time.parse('2000-01-01T16:00:00Z').utc
end_time_with_date = Time.parse('2014-01-24T15:58:07.169+04:00').utc
start_time = start_time_with_date.strftime('%I:%M:%S') #=> '04:00:00'
end_time = end_time_with_date.strftime('%I:%M:%S') #=> '11:58:07'
current_time = Time.now.utc.strftime('%I:%M:%S') #=> '01:45:27' (my current time)
current_time.between?(start_time, end_time) #=> false
And yes. Sadly, it's a string comparison.
You can use Range#cover? with time objects.
start = Time.parse('2000-01-01T10:00:00Z')
end_time = Time.parse('2000-01-01T16:00:00Z')
now = Time.parse('2014-01-24T15:58:07.169+04:00')
(start..end_time).cover?(now)
You're currently using strings, Ruby cannot know you're speaking about time.
I see the only variant, to define additional method to Range:
class Range
def time_cover? now
(b,e,n) = [ self.begin.utc.strftime( "%H%M%S%N" ),
self.end.utc.strftime( "%H%M%S%N" ),
now.utc.strftime( "%H%M%S%N" ) ]
if b < e
b <= n && e >= n
else
e <= n && b >= n
end
end
end
now = Time.parse "2014-01-24T15:58:07.169+04:00"
s = Time.parse "2000-01-01T10:00:00Z"
e = Time.parse "2000-01-01T16:00:00Z"
Range.new(s, e).time_cover?(now)
# => true
your date time(now) is not in between start and end time

Timed script/function in 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

Resources