Firestore security rules for birthdate - google-cloud-firestore

I would like to have a Firestore security rule for bithdate field in my users collections where user age should be >=18 and <=80. I tried the following rule but I know it is not going to work especially for ages that are close to 18 or 80. Any idea how to make this work.
let now = request.time;
let thisYear = now.year();
let thisMonth = now.month();
let thisDay = now.day();
request.resource.data.birthdate >= timestamp.date(thisYear-80,thisMonth,thisDay) &&
request.resource.data.birthdate <= timestamp.date(thisYear-18,thisMonth,thisDay)

You can achieve the same result by comparing it with the request time but in seconds. here’s one example on how this will work:
function isAbove18(date) {
return date.seconds <= request.time.seconds - 18 * 365.25 * 24 * 60 * 60; //considering leap year with 0.25 🙃
}
function isBelow80(date) {
return date.seconds >= request.time.seconds - 80 * 365.25 * 24 * 60 * 60;
}
allow read, write: if isAbove18(resource.data.birthdate) && isBelow80(resource.data.birthdate);
For more about this you can go through this docs which takes some different approaches.

Related

How do i format time into seconds in lua?

So basically I'm confused on how I'd make it so that I can convert DD:HH:MM:SS to only seconds while taking into account the amount of numbers there are. (Sorry if I make 0 sense, you should definitely know what I mean by the example below.)
print("05:00":FormatToSeconds()) -- 5 minutes and 0 seconds
-- 300
print("10:30:15":FormatToSeconds()) -- 10 hours, 30 minutes and 15 seconds
-- 37815
print("1:00:00:00":FormatToSeconds()) -- 1 day
-- 86400
print("10:00:00:30":FormatToSeconds()) -- 10 days, 30 seconds
-- 864030
So on and so forth. I think that maybe using gmatch would work but still idk. Help would be greatly appreciated.
Edit:
So I've tried doing it with gmatch, but I don't know if this is the most fastest way of doing this (which it probably isn't), so any help would still be appreciated.
(My code)
function ConvertTimeToSeconds(Time)
local Thingy = {}
local TimeInSeconds = 0
for v in string.gmatch(Time, "%d+") do
if tonumber(string.sub(v, 1, 1)) == 0 then
table.insert(Thingy, tonumber(string.sub(v, 2, 2)))
else
table.insert(Thingy, tonumber(v))
end
end
if #Thingy == 1 then
TimeInSeconds = TimeInSeconds + Thingy[1]
elseif #Thingy == 2 then
TimeInSeconds = TimeInSeconds + (Thingy[1] * 60) + Thingy[2]
elseif #Thingy == 3 then
TimeInSeconds = TimeInSeconds + (Thingy[1] * 60 * 60) + (Thingy[2] * 60) + Thingy[3]
elseif #Thingy == 4 then
TimeInSeconds = TimeInSeconds + (Thingy[1] * 24 * 60 * 60) + (Thingy[2] * 60 * 60) + (Thingy[3] * 60) + Thingy[4]
end
return TimeInSeconds
end
print(ConvertTimeToSeconds("1:00:00:00"))
Don't worry about execution speed before doing any actual measurements unless you're designing a time-critical program. In any extreme situation you'd probably want to offload risky parts to a C module.
Your approach is just fine. There are parts you can clean up: you can just return the results of calculations as TimeInSeconds doesn't actually act as accumulator in your case; tonumber handles '00' just fine and it can ensure decimal integers with an argument (since 5.3).
I'd go the other way and describe factors in a table:
local Factors = {1, 60, 60 * 60, 60 * 60 * 24}
local
function ConvertTimeToSeconds(Time)
local Components = {}
for v in string.gmatch(Time, "%d+") do
table.insert(Components, 1, tonumber(v, 10))
end
if #Components > #Factors then
error("unexpected time component")
end
local TimeInSeconds = 0
for i, v in ipairs(Components) do
TimeInSeconds = TimeInSeconds + v * Factors[i]
end
return TimeInSeconds
end
Of course, both implementations have problem with pattern being naïve as it would match e.g., '00 what 10 ever 10'. To fix that, you could go another route of using string.match with e.g., '(%d+):(%d+):(%d+):(%d+)' and enforcing strict format, or matching each possible variant.
Otherwise you can go all in and use LPeg to parse the duration.
Another way would be to not use strings internally, but instead convert them into a table like {secs=10, mins=1, hours=10, days=1} and then use these tables instead - getting seconds from that representation would be straight-forward.

calculate hours/minutes between two times in dart

I want to get/calculate hours and minutes between two times in dart.
Example: if start_time is 17:30 and end_time is 09:00 (day after) it should return total: 15 hours 30 minutes
My code:
check_two_times_is_before(String start_time, String end_time){
var format = DateFormat("HH:mm");
var start = format.parse(start_time);
var end = format.parse(end_time);
if(start.isAfter(end)) {
// do something here
}
}
You can use difference method on DateTime class.
check_two_times_is_before(String start_time, String end_time){
var format = DateFormat("HH:mm");
var start = format.parse(start_time);
var end = format.parse(end_time);
if(start.isAfter(end)) {
end = end.add(Duration(days: 1));
Duration diff = end.difference(start);
final hours = diff.inHours;
final minutes = diff.inMinutes % 60;
print('$hours hours $minutes minutes');
}
}
You can try it with Duration class
Duration duration = end.difference(start).abs();
final hours = duration.inHours;
final minutes = duration.inMinutes % 60;
print('$hours hours $minutes minutes');
The question is pretty old but here is another solution which might help someone.
Duration duration = DateTime.fromMillisecondsSinceEpoch(timestmap).difference(DateTime.now()).abs();
final days = duration.inDays;
final hours = duration.inHours - (days * 24);
final minutes = duration.inMinutes - (days * 24 * 60) - (hours * 60);
final seconds = duration.inSeconds - (days * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60);
Nothing to explain here really. We're just using basic algebra. We're getting the total number of milliseconds between the two timestamps and working that down to days, hours, minutes and seconds.

Best way to find time segments in a time frame

I am currently making an app where I have to retrieve allowances for certain hours worked.
For example, I work from 3:00 pm to 10:00 pm.
Between this time an allowance would be given between 8:00 pm - 10:00 pm 20% and from 10:00 pm to 11:00 pm 35%.
Until now I did not get much further than a rather complicated if construction.
if startUur <= 20 && eindUur >= 22 || 20..<22 ~= startUur || 20..<22 ~= eindUur || startUur > 20 && eindUur <= 22 && startMinuut > 0 || startUur < 20 {
//code to calculate allowance
}
I have been looking for a good and better way to do this, but I cannot find it.
Is there a better way or am I bound to such a way with an if construction?
struct TimeAndMoneyFrame {
var timeHourStart:Int = 0
var timeHourEnd:Int = 0
var percentage: Double = 0
}
extension TimeAndMoneyFrame {
func timeLength() -> Int {
return timeHourEnd - timeHourStart
}
}
let 2000to2200 = TimeAndMoneyFrame(timeHourStart = 20, timeHourEnd = 22, percentage = 0.2)
let 2200to2300 = TimeAndMoneyFrame(timeHourStart = 22, timeHourEnd = 23, percentage = 0.35)
let timeArray:[TimeAndMoneyFrame] = [2000to2200]
//Assuming 'startUur' means startingHour
//Assuming 'eindUur' means endingHour
let startUur:Int = someHour // You define some hour
let eindUure:Int = someHour // You define some hour
let hourlyRateOfPay: Double = somePay // You define some pay
var totalPay: Double = 0
for time in timeArray {
let start = time.timeHourStart
let end = time.timeHourEnd
let percent = time.percentage
//Encompasses entirely
if(start > startUur && end < eindUur) {
totalPay += (hourlyRateOfPay * (1 + percent) * time.timeLength())
}
//Only encompassed the left side - i.e., their time worked ends within this time frame
else if(start > startUur) {
let timeWithinThisFrame = eindUur - start
totalPay += (hourlyRateOfPay * (1 + percent) * (timeWithinThisFrame/time.timeLength())
}
//Only encompassed right side - i.e., the beginning of the starts within this time frame
else if(eindUur < end) {
let timeWithinThisFrame = end - startUur
totalPay += (hourlyRateOfPay * (1 + percent) * (timeWithinThisFrame/time.timeLength())
}
}
So, we can break this problemn up into a couple problems.
1) Defining a struct/class that encompasses a time frame with an associated percentage
2) The algorithm necessary to calculate the total pay
I have a base assumption.
1) You say allowances - a person gets 35% if they work from 2200-2300 - I took this as a bonus so say 1.35% of the normal hourly rate. If this is not the case, the idea of consuming time frames one at a time would be the same. The only difference might be the totalPay calculation.
My algorithm goes through each time frame and determines if the current time intercepts any of the time frames. If it does, I calculate how much it intercepts and calculate the rate of pay.
Note: I coded all of this on SO - there might be some syntax issues.

How to get date range between two date in Google AppMaker?

I have two datebox that capture a start date & end date. I tried do following binding to get the date range between the two date but it return a negative value
#widget.root.children.DateBox1.value - #widget.root.children.DateBox2.value
following is my form example
// Binding (option 1 - no datasource)
getValidity(#widget.root.children.StartDate.value, #widget.root.children.EndDate.value);
// Binding (option 2 - with datasource)
getValidity(#datasource.item.StartDate, #datasource.item.EndDate);
// Client script
function getValidity(start, end) {
if (start && end) {
var milliseconds = end - start;
// days: 1000ms * 60s * 60m * 24h
return milliseconds / (1000 * 60 * 60 * 24);
// years: 1000ms * 60s * 60m * 24h * 365d
// return milliseconds / (1000 * 60 * 60 * 24 * 365);
}
return 'n/a';
}

Comparing two Date values in ActionScript - possible to compare whole day values?

I need to be able to compare the number of whole days between two dates in ActionScript, is this possible?
I'd like to test if one date is 7 days or less after today, and if so is it one day or less (if it's before today this also counts).
The workaround I have in place is using the .time part of the date field:
// Get the diffence between the current date and the due date
var dateDiff:Date = new Date();
dateDiff.setTime (dueDate.time - currentDate.time);
if (dateDiff.time < ( 1 * 24 * 60 * 60 * 1000 ))
return "Date is within 1 day");
else if (dateDiff.time < ( 7 * 24 * 60 * 60 * 1000 ))
return "Date is within 7 days");
As I say - this is only a workaround, I'd like a permanent solution to allow me to check the number of whole days between 2 dates. Is this possible?
Thanks
var daysDifference:Number = Math.floor((dueDate.time-currentDate.time)/(1000*60*60*24));
if (daysDifference < 2)
return "Date is within 1 day";
else if (daysDifference < 8)
return "Date is within 7 days";