Integer minutes/hours to Ruby Time [closed] - ruby-on-rails

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have the following duration:
1456
14 minutes and 56 seconds
And I need to convert to time and format like this:
14:56
In case of hours:
6500
01:05
How can I do that with ruby/rails ?
I'm looking for a solution but I can't.

Updated (considering #eiko's comment), this code will handle any (positive) integer (inferior to 60*24*100):
def custom_duration_to_hours_mn_sec_str(duration)
min, sec = duration.divmod(100)
total_in_seconds = min.minutes + sec.seconds
Time.at(total_in_seconds).utc.strftime("%H:%M:%S")
end
custom_duration_to_hours_mn_sec_str(56) # 56 seconds
# => "00:00:56"
custom_duration_to_hours_mn_sec_str(1456) # 14 mn 56 seconds
# => "00:14:56"
custom_duration_to_hours_mn_sec_str(12056) # 120 mn 56 seconds
# => "02:00:56"
Limitation: if the duration is over 60*24 minutes (quantity of minutes in a 24h day), it will start again to '00:00:00' and therefore ignore the quantity of days.
custom_duration_to_hours_mn_sec_str(60*24*100) # *100 to add the zeros for seconds
# => "00:00:00"

Related

Vacation Entitlement

Tenure
Vacation entitlement
1 year but less then 4 years
4%, 14 days
3 or more years but less then 10
6%, 21 days
10 or more years
8%, 28 days
15 or more years
10%, 35 days
I have the file as follows
NAME | HIRE DATE | TODAY | YEARS | % | VACATION Entitlement
I am unsure how to calculate all this information into one sheet. I'm still learning the formulas, but I am not sure where to begin when it gets into multiple details.

How to specify start time of continuous query in InfluxDB?

The following data is from the InfluxDB documentation about continuous queries:
name: bus_data
--------------
time passengers complaints
2016-08-28T07:00:00Z 5 9
2016-08-28T07:15:00Z 8 9
2016-08-28T07:30:00Z 8 9
2016-08-28T07:45:00Z 7 9
2016-08-28T08:00:00Z 8 9
2016-08-28T08:15:00Z 15 7
2016-08-28T08:30:00Z 15 7
2016-08-28T08:45:00Z 17 7
2016-08-28T09:00:00Z 20 7
In this example the data is grouped by 15 minutes and starts from 2016-08-28T07:00:00Z. How do I specify when a continuous query should start running? I do not want to end up with time stamps like 2016-08-28T07:03:17Z but I'd rather have a 'clean' start time like in the example.

grep greater than an integer in file1.txt file2.txt file3.txt and file4.txt [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to find anomalies in some text files that have a list of integers. is there a way to search inside multiple files for a number greater than x?
alex#porcellino:/tmp/test$ ls
a b
alex#porcellino:/tmp/test$ cat a
1
2
16
4
7
alex#porcellino:/tmp/test$ cat b
5
6
89
11
alex#porcellino:/tmp/test$ for n in `cat * | sort | uniq`;do if [ $n -gt 12 ];then grep $n *;fi;done
a:16
b:89
I am trying to find ping anomalies over 1000ms, the only files I wish to search are in the folder I am working in. This is how I was able to get my desired results:
cat * | egrep 'time=........ ms|time=......... ms'
This is pretty basic but works for my needs.

Solving recurrence relations [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I honestly don't even know if i am doing this right. The questions asks to solve:
C0 = 1
CN = CN/2 + N
when N is a power of 2.
Here is what I have so far but it is a complete guess
CN = CN/2 + N
CN/2 = CN/4 + N/2
CN = CN/4 + N/2 + N
stuck here
Observe that for each term, you are adding something to the next term. Thus, you'll get a sum. For example, for 1024, you'll get:
1024 + 512 + 256 + 128 + 64 + 32 + 16 + ...
As dividing a nonzero number by two will never yield zero, the base case, C0, is never reached, and you'll end up with an infinite series. Luckily, it is geometric. The initial term is N and each time we multiply by 1/2, so the the sum will be N/(1-1/2) = N/(1/2) = 2N.

Calculate number of business days between two days

I need to calculate the number of business days between two dates. How can I pull that off using Ruby (or Rails...if there are Rails-specific helpers).
Likewise, I'd like to be able to add business days to a given date.
So if a date fell on a Thursday and I added 3 business days, it would return the next Tuesday.
Take a look at business_time. It can be used for both the things you're asking.
Calculating business days between two dates:
wednesday = Date.parse("October 17, 2018")
monday = Date.parse("October 22, 2018")
wednesday.business_days_until(monday) # => 3
Adding business days to a given date:
4.business_days.from_now
8.business_days.after(some_date)
Historical answer
When this question was originally asked, business_time didn't provide the business_days_until method so the method below was provided to answer the first part of the question.
This could still be useful to someone who didn't need any of the other functionality from business_time and wanted to avoid adding an additional dependency.
def business_days_between(date1, date2)
business_days = 0
date = date2
while date > date1
business_days = business_days + 1 unless date.saturday? or date.sunday?
date = date - 1.day
end
business_days
end
This can also be fine tuned to handle the cases that Tipx mentions in the way that you would like.
We used to use the algorithm suggested in the mikej's answer and discovered that calculating 25,000 ranges of several years each takes 340 seconds.
Here's another algorithm with asymptotic complexity O(1). It does the same calculations in 0.41 seconds.
# Calculates the number of business days in range (start_date, end_date]
#
# #param start_date [Date]
# #param end_date [Date]
#
# #return [Fixnum]
def business_days_between(start_date, end_date)
days_between = (end_date - start_date).to_i
return 0 unless days_between > 0
# Assuming we need to calculate days from 9th to 25th, 10-23 are covered
# by whole weeks, and 24-25 are extra days.
#
# Su Mo Tu We Th Fr Sa # Su Mo Tu We Th Fr Sa
# 1 2 3 4 5 # 1 2 3 4 5
# 6 7 8 9 10 11 12 # 6 7 8 9 ww ww ww
# 13 14 15 16 17 18 19 # ww ww ww ww ww ww ww
# 20 21 22 23 24 25 26 # ww ww ww ww ed ed 26
# 27 28 29 30 31 # 27 28 29 30 31
whole_weeks, extra_days = days_between.divmod(7)
unless extra_days.zero?
# Extra days start from the week day next to start_day,
# and end on end_date's week date. The position of the
# start date in a week can be either before (the left calendar)
# or after (the right one) the end date.
#
# Su Mo Tu We Th Fr Sa # Su Mo Tu We Th Fr Sa
# 1 2 3 4 5 # 1 2 3 4 5
# 6 7 8 9 10 11 12 # 6 7 8 9 10 11 12
# ## ## ## ## 17 18 19 # 13 14 15 16 ## ## ##
# 20 21 22 23 24 25 26 # ## 21 22 23 24 25 26
# 27 28 29 30 31 # 27 28 29 30 31
#
# If some of the extra_days fall on a weekend, they need to be subtracted.
# In the first case only corner days can be days off,
# and in the second case there are indeed two such days.
extra_days -= if start_date.tomorrow.wday <= end_date.wday
[start_date.tomorrow.sunday?, end_date.saturday?].count(true)
else
2
end
end
(whole_weeks * 5) + extra_days
end
business_time has all the functionallity you want.
From the readme:
#you can also calculate business duration between two dates
friday = Date.parse("December 24, 2010")
monday = Date.parse("December 27, 2010")
friday.business_days_until(monday) #=> 1
Adding business days to a given date:
some_date = Date.parse("August 4th, 1969")
8.business_days.after(some_date) #=> 14 Aug 1969
Here is my (non gem and non holiday) weekday count example:
first_date = Date.new(2016,1,5)
second_date = Date.new(2016,1,12)
count = 0
(first_date...second_date).each{|d| count+=1 if (1..5).include?(d.wday)}
count
Take a look at Workpattern. It alows you to specify working and resting periods and can add/subtract durations to/from a date as well as calculate the minutes between two dates.
You can set up workpatterns for different scenarios such as mon-fri working or sun-thu and you can have holidays and whole or part days.
I wrote this as away to learn Ruby. Still need to make it more Ruby-ish.
Based on #mikej's answer. But this also takes into account holidays, and returns a fraction of a day (up to the hour accurancy):
def num_days hi, lo
num_hours = 0
while hi > lo
num_hours += 1 if hi.workday? and !hi.holiday?
hi -= 1.hour
end
num_hours.to_f / 24
end
This uses the holidays and business_time gems.
Simple script to calculate total number of working days
require 'date'
(DateTime.parse('2016-01-01')...DateTime.parse('2017-01-01')).
inject({}) do |s,e|
s[e.month]||=0
if((1..5).include?(e.wday))
s[e.month]+=1
end
s
end
# => {1=>21, 2=>21, 3=>23, 4=>21, 5=>22, 6=>22, 7=>21, 8=>23, 9=>22, 10=>21, 11=>22, 12=>22}
There are two problems with the most popular solutions listed above:
They involve loops to count every single day between each date (meaning that performance gets worse the further apart the dates are.
They are unclear about whether they count from the beginning of the day or the end. If you count from the morning, there is one weekday between Friday and Saturday. If you count from the night, there are zero weekdays between Friday and Saturday.
After stewing over it, I propose this solution that addresses both problems. The below takes a reference date and an other date and calculates the number of weekdays between them (returning a negative number if other is before the reference date). The argument eod_base controls whether counting is done from end of day (eod) or start of day. It could be written more compactly but hopefully it's relatively easy to understand and it doesn't require gems or rails.
require 'date'
def weekdays_between(ref,otr,eod_base=true)
dates = [ref,otr].sort
return 0 if dates[0] == dates[1]
full_weeks = ((dates[1]-dates[0])/7).floor
dates[eod_base ? 0 : 1] += (eod_base ? 1 : -1)
part_week = Range.new(dates[0],dates[1])
.inject(0){|m,v| (v.wday >=1 && v.wday <= 5) ? (m+1) : m }
return (otr <=> ref) * (full_weeks*5 + part_week)
end

Resources