timerizer 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/timerizer.rb +43 -10
  2. metadata +1 -1
data/lib/timerizer.rb CHANGED
@@ -457,6 +457,21 @@ class WallClock
457
457
  end
458
458
  end
459
459
 
460
+ # Takes a string and turns it into a WallClock time
461
+ # @param [String] string The string to convert
462
+ # @return [WallClock] The time as a WallClock
463
+ # @example
464
+ # WallClock.from_string("10:30 PM")
465
+ # # => 10:30:00 PM
466
+ # WallClock.from_string("13:01:23")
467
+ # # => 1:01:23 PM
468
+ # @see #to_s
469
+ def self.from_string(string)
470
+ time, meridiem = string.split(' ', 2)
471
+ hour, minute, second = time.split(':', 3)
472
+ WallClock.new(hour.to_i, minute.to_i, second.to_i || 0, meridiem || :am)
473
+ end
474
+
460
475
  # Returns the time of the WallClock on a date
461
476
  # @param [Date] date The date to apply the time on
462
477
  # @return [Time] The time after the given date
@@ -562,20 +577,38 @@ class WallClock
562
577
 
563
578
  # Convert {WallClock} to a human-readable format.
564
579
  # @param [Symbol] system The hour system to use (`:twelve_hour` or `:twenty_four_hour`; default `:twelve_hour`)
580
+ # @param [Hash] options Extra options for the string to use
581
+ # @option options [Boolean] :use_seconds Whether or not to include seconds in the conversion to a string
582
+ # @option options [Boolean] :include_meridian Whether or not to include the meridian for a twelve-hour time
565
583
  # @example
566
- # time = WallClock.new(5, 37, :pm)
584
+ # time = WallClock.new(5, 37, 41, :pm)
567
585
  # time.to_s
568
- # => "5:37:00 PM"
569
- # time.to_s(:twenty_four_hour)
570
- # => "17:37:00"
586
+ # => "5:37:41 PM"
587
+ # time.to_s(:twenty_four_hour, :use_seconds => true)
588
+ # => "17:37:41"
589
+ # time.to_s(:twelve_hour, :use_seconds => false, :include_meridiem => false)
590
+ # => "5:37"
591
+ # time.to_s(:twenty_four_hour, :use_seconds =>false)
592
+ # => "17:37"
571
593
  # @raise ArgumentError Argument isn't a proper system
572
- def to_s(system = :twelve_hour)
594
+ def to_s(system = :twelve_hour, options = {})
595
+ options = {:use_seconds => true, :include_meridiem => true}.merge(options)
573
596
  pad = "%02d"
574
- if(system == :twelve_hour)
575
- meridiem = self.meridiem.to_s.upcase
576
- "#{self.hour(system)}:#{pad % self.minute}:#{pad % self.second} #{meridiem}"
577
- elsif(system == :twenty_four_hour)
578
- "#{self.hour(system)}:#{pad % self.minute}:#{pad % self.second}"
597
+ meridiem = self.meridiem.to_s.upcase
598
+ hour = self.hour(system)
599
+ minute = pad % self.minute
600
+ second = pad % self.second
601
+
602
+ string = [hour, minute].join(':')
603
+ if options[:use_seconds]
604
+ string = [string, second].join(':')
605
+ end
606
+
607
+ case system
608
+ when :twelve_hour
609
+ options[:include_meridiem] ? [string, meridiem].join(' ') : string
610
+ when :twenty_four_hour
611
+ string
579
612
  else
580
613
  raise ArgumentError, "system should be :twelve_hour or :twenty_four_hour"
581
614
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timerizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: