Guest User

Untitled

a guest
Feb 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. # definition
  2.  
  3. # provide an optional day name (in English)
  4. # as the last parameter to get the number of times that day occurs
  5. # between the two provided dates
  6. #
  7. # dates (t1 and t2) should be valid Time object dates
  8. def list_days_between(t1, t2, day_name=nil)
  9. days = []
  10. (t2.to_i..t1.to_i).step(86400) do |t|
  11. days << Time.at(t).strftime('%A')
  12. end
  13.  
  14. if day_name
  15. days.collect! { |d| d if d == day_name }
  16. days.delete_if {|d| d == nil}
  17. days = days.count
  18. end
  19.  
  20. days
  21. end
  22.  
  23. # use
  24.  
  25. now = Time.now
  26.  
  27. # according to Google: 10 000 000 seconds = 115.740741 days
  28. # note: I make no claim about the efficiency of this algorithm...
  29. # improvement suggestions are welcome!
  30. day_names_count = list_days_between(now, now-10000000, 'Saturday')
  31. day_names = list_days_between(now, now-10000000)
  32.  
  33. puts 'Array of days: ' + day_names.inspect
  34. puts 'Count of Saturdays: ' + day_names_count.inspect
  35.  
  36. puts 'Total days:' + day_names.count.to_s
Add Comment
Please, Sign In to add comment