Created
January 13, 2014 03:39
-
-
Save btc/8394359 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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