Round millseconds timestamp to nearest 30 seconds - ruby

I have following ruby code to get current time in "milliseconds", for example: 1648059542287 which is equivalent to Wed Mar 23 2022 18:19:02
What I need is timestamp rounded to nearest 30 seconds, for above example, I need it rounded to 1648059540000. Any suggestions?
#ctime = Time.now.to_f
(#ctime * 1000).to_i

Using the time you provided, I think this will do what you want:
Take your time, convert it to an integer
divide by floating point value 30 the number of half minutes
round that value and multiple by 30 to get back to the number of seconds
Create a new time object based on the rounded value.
input = Time.at(1648059542,287000)
Time.at((input.to_i / 30.0).round * 30)

Related

Approximate comparison of 2 Ruby DateTimes

I have 2 DateTimes and I want to check if they're roughly 24 hours apart, plus or minus a small amount, say 5 minutes. Is there a built in way to do this?
There is not, but it is easy enough:
(-5 * 60 .. 5 * 60).include?((t2 - t1).abs - 24 * 3600)
"is the absolute difference between the two dates, when you subtract a full day, within plus or minus five minutes?"
Version 1: Works for both DateTime and Time. Converts everything to seconds.
def time_apart_within_drift?(t1, t2, diff: 24*60*60, drift: 5*60)
(t1.to_i - t2.to_i).abs <= diff + drift
end
As per the Ruby Style Guide, you should almost always use Time instead of DateTime. A Time object can still include the date, and it will make your calculations much cleaner.
This is because subtracting two DateTime objects gives you the difference in days as a Rational object, whereas subtracting two Time objects gives you the difference in seconds as a Float.
This allows you to write your function like so:
def time_apart_within_drift?(t1, t2, diff: 24*60*60, drift: 5*60)
(t1 - t2).abs <= diff + drift
end
# true
time_apart_within_drift?(Time.new(2020,3,5), Time.new(2020,3,4))
# true
time_apart_within_drift?(Time.new(2020,3,5), Time.new(2020,3,3,23,57))
# false
time_apart_within_drift?(Time.new(2020,3,5), Time.new(2020,3,3,23,47))
If using Time as well as rails and/or activesupport, the args can be made more readable using Duration objects:
def time_apart_within_drift?(t1, t2, diff: 24.hours, drift: 5.minutes)
(t1 - t2).abs <= diff + drift
end

Decimal minutes into time format (Tabular Cube)

I would like to change decimal minutes into HH:mm:ss (DAX, Tabular Cube).
When I use this :
measure = FORMAT(TIME(0;minutes;0);"HH:mm:ss")
I get hours and minutes but without seconds. Minutes are rounded.
When I use this :
measure = FORMAT(TIME(0;0;minutes*60);"HH:mm:ss")
I have an error that value is too big or too small.
Thanks in advance.
DAX understands decimal time in units of days, so all that you need to do is convert decimal minutes to decimal days and format:
FORMAT ( [Minutes] / 60 / 24, "HH:mm:ss" )

Lua convert format "X h X min" to seconds in WoW

So in the game World of Warcraft I have managed to get the remaining time of a mission via their API. The problem for me is that I want to convert this to seconds to then be able to check at which time the mission will finish. If I call the function time() in the game I get a response similar to this 1418569973 which to me, makes no sense. But this is why I need to convert it to seconds, because then I can simply add the amount of seconds I get to the current time and get the end time of the mission.
But my problem is that the when I look into the table that gives me the current time left of a mission it returns a string with the format "X h X min" for example "4 h 34 min". I need to convert that to seconds but I literally have no idea on where to start. I'm thinking of something like removing the "h" and the "min" in a function. But from there I'm not really sure of where to go.
os.time() returns the seconds since epoch. Thus, "1418569973" gives you "12/14/14 03:12:53 PM" UTC. Now, to convert your string to seconds:
local iH, iM = sInput:match "(%d+) h (%d+) min"
iH, iM = tonumber( iH ), tonumber( iM )
local iSec = iH * 3600 + iM * 60

SSRS sum based on not-null condition

I have a report I am building that I am summing a column with the total minutes listed in it then dividing it by 60 to get number of hours like so.
=Sum(Fields!designtimeValue.Value) / 60
But I have a condition where I want to weed out rows that contain other data in another column
Its laid out like so
Submittal_date Submittal_returned Design_Hours
12/01/2014 180
12/01/2014 12/02/2014 240
12/01/2014 60
So I want to do something like =Sum(IIF(Fields!sumbittal_returned.Value="NULL",Fields!design_hours.Value)) / 60
But it is throwing an error the sum iff does not allow that number of exceptions
I then need to take it a step further And be able to take the sum of design total if submittal_returned = NULL total then for any instance submittal_returned = Not-NULL add 120 minutes
Thanks
Edit : format code
IIF takes 3 arguments. A condition, a true result, and a false result. You have to supply all three.
This would be the correct syntax:
=Sum(IIF(IsNothing(Fields!submittal_returned.Value),Fields!design_hours.Value,0)) / 60
Although that condition will only be true if submittal_returned actually contained the string "NULL". If it's actually NULL (not a string), you would need test with the IsNothing() function.
Not sure I understand your last sentence, maybe you mean this?:
=Sum(IIF(IsNothing(Fields!submittal_returned.Value),Fields!design_hours.Value,Fields!design_hours.Value + 120)) / 60

Converting numbers/string to time - PROLOG

I am a beginner in prolog and was wondering if there was an easy way to convert numbers to time, for comparison.
For example:
The below two lists show bus name, capacity, time it arrives at city, time it departs city.
bus_info(bus1,150, 12:30, 14:30).
bus_info(bus2, 200, 16:00, 18:00).
passenger_info(mike, 21, 17:30). -shows name, age, and time available
I want to check which bus Mike can catch. The answer is bus 2, but how do I calculate this in prolog?
You're just comparing times for a given day so you don't need to convert the numbers to any kind of system time encoding. You only need, say "minutes past midnight" or something like that. For example, 12:30 would be (12*60)+30 minutes past midnight. And you can use that as your comparison units for a daily schedule.
To capture your hours and minutes to do this calculation, if you were to "ask" in Prolog:
bus_info(Bus, Num, StartHH:StartMM, EndHH:EndMM).
You would get two results:
Bus = bus1
Num = 150
StartHH = 12
StartMM = 30
EndHH = 14
EndHH = 30
And
Bus = bus2
Num = 200
StartHH = 16
StartMM = 0
EndHH = 18
EndMM = 0
To assign a numeric value of an expression in Prolog, you need the is predicate. For example:
StartTime is (StartHH * 60) + StartMM.
That basic information should get you started if you've learned how Prolog predicates basically work.

Resources