Class: WallClock
- Inherits:
-
Object
- Object
- WallClock
- Defined in:
- lib/timerizer.rb
Overview
Represents a time, but not a date. ‘`7:00 PM`’ would be an example of a WallClock object
Defined Under Namespace
Classes: InvalidMeridiemError, TimeOutOfBoundsError
Class Method Summary collapse
-
.from_string(string) ⇒ WallClock
Takes a string and turns it into a WallClock time.
Instance Method Summary collapse
-
#==(time) ⇒ Boolean
Comparse two WallClocks.
-
#hour(system = :twenty_four_hour) ⇒ Integer
Get the hour of the WallClock.
-
#in_hours ⇒ Integer
Get the time of the WallClock, in hours.
-
#in_minutes ⇒ Integer
Get the time of the WallClock, in minutes.
-
#in_seconds ⇒ Integer
Get the time of the WallClock, in seconds.
-
#initialize(hour = nil, minute = nil, second = 0, meridiem = :am) ⇒ WallClock
constructor
Initialize a new instance of WallClock.
-
#meridiem ⇒ Symbol
Get the meridiem of the WallClock.
-
#minute ⇒ Integer
Get the minute of the WallClock.
-
#on(date) ⇒ Time
Returns the time of the WallClock on a date.
-
#second ⇒ Integer
Get the second of the WallClock.
-
#to_i ⇒ Object
Get the time of the WallClock in a more portable format (for a database, for example).
-
#to_relative ⇒ RelativeTime
Converts WallClock to RelativeTime.
-
#to_s(system = :twelve_hour, options = {}) ⇒ Object
Convert WallClock to a human-readable format.
-
#to_wall ⇒ Object
Converts self to WallClock.
Constructor Details
#new(hash) ⇒ WallClock #new(hour, minute, meridiem) ⇒ WallClock #new(hour, minute, second, meridiem) ⇒ WallClock #new(seconds) ⇒ WallClock
Initialize a new instance of WallClock
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/timerizer.rb', line 420 def initialize(hour = nil, minute = nil, second = 0, meridiem = :am) units = nil if hour.is_a?(Integer) && minute.nil? units = {:second => hour} elsif hour.is_a?(Hash) units = hour end if !units.nil? second = units[:second] || 0 minute = units[:minute] || 0 hour = units[:hour] || 0 else if second.is_a?(String) || second.is_a?(Symbol) meridiem = second second = 0 end meridiem = meridiem.downcase.to_sym if !(meridiem == :am || meridiem == :pm) raise InvalidMeridiemError elsif meridiem == :pm && hour > 12 raise TimeOutOfBoundsError, "hour must be <= 12 for PM" elsif hour >= 24 || minute >= 60 || second >= 60 raise TimeOutOfBoundsError end hour += 12 if (meridiem == :pm and !(hour == 12)) end @seconds = RelativeTime.units_in_seconds.fetch(:hour) * hour + RelativeTime.units_in_seconds.fetch(:minute) * minute + second if @seconds >= RelativeTime.units_in_seconds.fetch(:day) raise TimeOutOfBoundsError end end |
Class Method Details
.from_string(string) ⇒ WallClock
Takes a string and turns it into a WallClock time
469 470 471 472 473 |
# File 'lib/timerizer.rb', line 469 def self.from_string(string) time, meridiem = string.split(' ', 2) hour, minute, second = time.split(':', 3) WallClock.new(hour.to_i, minute.to_i, second.to_i || 0, meridiem || :am) end |
Instance Method Details
#==(time) ⇒ Boolean
Comparse two WallClocks.
488 489 490 491 492 493 494 |
# File 'lib/timerizer.rb', line 488 def ==(time) if time.is_a? WallClock self.in_seconds == time.in_seconds else false end end |
#hour(system = :twenty_four_hour) ⇒ Integer
Get the hour of the WallClock.
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/timerizer.rb', line 529 def hour(system = :twenty_four_hour) hour = self.to_relative.hours if system == :twelve_hour if hour == 0 12 elsif hour > 12 hour - 12 else hour end elsif (system == :twenty_four_hour) hour else raise ArgumentError, "system should be :twelve_hour or :twenty_four_hour" end end |
#in_hours ⇒ Integer
Get the time of the WallClock, in hours
510 511 512 |
# File 'lib/timerizer.rb', line 510 def in_hours @seconds / RelativeTime.units_in_seconds[:hour] end |
#in_minutes ⇒ Integer
Get the time of the WallClock, in minutes
504 505 506 |
# File 'lib/timerizer.rb', line 504 def in_minutes @seconds / RelativeTime.units_in_seconds[:minute] end |
#in_seconds ⇒ Integer
Get the time of the WallClock, in seconds
498 499 500 |
# File 'lib/timerizer.rb', line 498 def in_seconds @seconds end |
#meridiem ⇒ Symbol
Get the meridiem of the WallClock.
548 549 550 551 552 553 554 |
# File 'lib/timerizer.rb', line 548 def meridiem if self.hour > 12 || self.hour == 0 :pm else :am end end |
#minute ⇒ Integer
Get the minute of the WallClock.
522 523 524 |
# File 'lib/timerizer.rb', line 522 def minute self.to_relative.minutes end |
#on(date) ⇒ Time
Returns the time of the WallClock on a date
482 483 484 |
# File 'lib/timerizer.rb', line 482 def on(date) date.to_date.to_time + @seconds end |
#second ⇒ Integer
Get the second of the WallClock.
516 517 518 |
# File 'lib/timerizer.rb', line 516 def second self.to_relative.seconds end |
#to_i ⇒ Object
Get the time of the WallClock in a more portable format (for a database, for example)
574 575 576 |
# File 'lib/timerizer.rb', line 574 def to_i self.in_seconds end |
#to_relative ⇒ RelativeTime
Converts WallClock to RelativeTime
568 569 570 |
# File 'lib/timerizer.rb', line 568 def to_relative @seconds.seconds end |
#to_s(system = :twelve_hour, options = {}) ⇒ Object
Convert WallClock to a human-readable format.
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/timerizer.rb', line 594 def to_s(system = :twelve_hour, = {}) = {:use_seconds => true, :include_meridiem => true}.merge() pad = "%02d" meridiem = self.meridiem.to_s.upcase hour = self.hour(system) minute = pad % self.minute second = pad % self.second string = [hour, minute].join(':') if [:use_seconds] string = [string, second].join(':') end case system when :twelve_hour [:include_meridiem] ? [string, meridiem].join(' ') : string when :twenty_four_hour string else raise ArgumentError, "system should be :twelve_hour or :twenty_four_hour" end end |
#to_wall ⇒ Object
Converts self to WallClock
558 559 560 |
# File 'lib/timerizer.rb', line 558 def to_wall self end |