Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # definition
- # provide an optional day name (in English)
- # as the last parameter to get the number of times that day occurs
- # between the two provided dates
- #
- # dates (t1 and t2) should be valid Time object dates
- def list_days_between(t1, t2, day_name=nil)
- days = []
- (t2.to_i..t1.to_i).step(86400) do |t|
- days << Time.at(t).strftime('%A')
- end
- if day_name
- days.collect! { |d| d if d == day_name }
- days.delete_if {|d| d == nil}
- days = days.count
- end
- days
- end
- # use
- now = Time.now
- # according to Google: 10 000 000 seconds = 115.740741 days
- # note: I make no claim about the efficiency of this algorithm...
- # improvement suggestions are welcome!
- day_names_count = list_days_between(now, now-10000000, 'Saturday')
- day_names = list_days_between(now, now-10000000)
- puts 'Array of days: ' + day_names.inspect
- puts 'Count of Saturdays: ' + day_names_count.inspect
- puts 'Total days:' + day_names.count.to_s
Add Comment
Please, Sign In to add comment