Skip to content

Instantly share code, notes, and snippets.

@btc
Created January 13, 2014 03:39
Show Gist options
  • Save btc/8394359 to your computer and use it in GitHub Desktop.
Save btc/8394359 to your computer and use it in GitHub Desktop.
require 'chronic'
class RhythmClock
def initialize wake, sleep
@begin = wake
@end = sleep
end
# returns integer in range (0, 100]
def now(time=DateTime.now)
elapsed = daysecs(time) - daysecs(@begin)
awake_period = daysecs(@end) - daysecs(@begin)
(100 * elapsed / awake_period.to_f).to_i
end
private
def daysecs time
seconds = 0
seconds += time.hour * 60 * 60
seconds += time.min * 60
seconds += time.sec
seconds
end
end
def main
wake_up_time = Chronic.parse('6 am')
sleep_time = Chronic.parse('10 pm')
rc = RhythmClock.new wake_up_time, sleep_time
while true
puts rc.now
sleep 1
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment