Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'date'
- def days_diff(date1, date2)
- date1 = Time.new(date1.year, date1.month, date1.day)
- date2 = Time.new(date2.year, date2.month, date2.day)
- return (date1.to_i - date2.to_i)/(3600*24)
- end
- def count_sessions(array)
- count_session = 1
- last_session = DateTime.strptime(array[array.length-1], "%Y-%m-%d %H:%M:%S")
- consecutive_days = 1
- for i in 2..array.length
- comparing = DateTime.strptime(array[array.length-i], "%Y-%m-%d %H:%M:%S")
- days_between_sessions = days_diff(last_session, comparing)
- if days_between_sessions > 1
- break
- elsif last_session.to_time.to_i - comparing.to_time.to_i > 60*30
- consecutive_days += days_between_sessions
- count_session += 1
- last_session = comparing
- end
- end
- if count_session >= 6 && consecutive_days == 3
- return true
- else
- return false
- end
- end
- puts count_sessions(['2017-03-10 08:13:11', '2017-03-10 19:01:27', '2017-03-11 07:35:55', '2017-03-11 16:15:11', '2017-03-12 08:01:41', '2017-03-12 17:19:08'])
- puts count_sessions(['2017-03-10 18:58:11', '2017-03-10 19:01:27', '2017-03-11 07:35:55', '2017-03-11 16:15:11', '2017-03-12 08:01:41', '2017-03-12 17:19:08'])
- puts count_sessions(['2017-03-08 17:11:13', '2017-03-11 17:22:47', '2017-03-11 19:23:51', '2017-03-11 22:03:12', '2017-03-12 08:32:04', '2017-03-12 13:19:08', '2017-03-12 17:19:08'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement