Class: Ruy::TimePattern
- Inherits:
-
Object
- Object
- Ruy::TimePattern
- Includes:
- Comparable
- Defined in:
- lib/ruy/time_pattern.rb
Constant Summary collapse
- WELL_FORMED_TS_EXP =
/^(?<year>\d{4}|\*)-(?<month>\d{2}|\*)-(?<day>\d{2}|\*)T(?<hour>\d{2}|\*):(?<minute>\d{2}|\*):(?<second>\d{2}|\*)(z(?<time_zone>\S+))?$/
Instance Attribute Summary collapse
-
#day ⇒ Object
readonly
Returns the value of attribute day.
-
#hour ⇒ Object
readonly
Returns the value of attribute hour.
-
#local ⇒ Object
readonly
Returns the value of attribute local.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#sec ⇒ Object
readonly
Returns the value of attribute sec.
-
#time_zone ⇒ Object
readonly
Returns the value of attribute time_zone.
-
#tz ⇒ Object
readonly
Returns the value of attribute tz.
-
#utc ⇒ Object
readonly
Returns the value of attribute utc.
-
#utc_offset ⇒ Object
readonly
Returns the value of attribute utc_offset.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Instance Method Summary collapse
- #<=>(o) ⇒ Object
- #==(o) ⇒ Object
-
#initialize(pattern, tz_identifier = 'UTC') ⇒ TimePattern
constructor
A new instance of TimePattern.
- #inspect ⇒ Object
-
#to_s ⇒ Object
Returns a well-formed Ruy timestamp with IANA time zone identifier representing the current TimePattern object.
Constructor Details
#initialize(pattern, tz_identifier = 'UTC') ⇒ TimePattern
Returns a new instance of TimePattern.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ruy/time_pattern.rb', line 16 def initialize(pattern, tz_identifier = 'UTC') unless match_data = pattern.match(WELL_FORMED_TS_EXP) raise ArgumentError, "#{pattern.inspect} is malformed" end @pattern = pattern @year = match_data[:year] == '*' ? nil : match_data[:year].to_i @month = match_data[:month] == '*' ? nil : match_data[:month].to_i @day = match_data[:day] == '*' ? nil : match_data[:day].to_i @hour = match_data[:hour] == '*' ? nil : match_data[:hour].to_i @min = match_data[:minute] == '*' ? nil : match_data[:minute].to_i @sec = match_data[:second] == '*' ? nil : match_data[:second].to_i @time_zone = match_data[:time_zone] # Store the TZInfo::Timezone object corresponding to the specified time zone @tz = TZInfo::Timezone.get(@time_zone || tz_identifier) # Store a Time object with values based on the specified time zone @local = Time.new(year || 0, month, day, hour, min, sec, 0) # Store a Time object with values based on UTC @utc = @tz.local_to_utc(@local) @utc_offset = @tz.current_period.utc_total_offset end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (protected)
Redirects missing methods to the UTC Time object stored in the instance
95 96 97 |
# File 'lib/ruy/time_pattern.rb', line 95 def method_missing(method, *args) @utc.respond_to?(method) ? @utc.send(method, *args) : super end |
Instance Attribute Details
#day ⇒ Object (readonly)
Returns the value of attribute day.
9 10 11 |
# File 'lib/ruy/time_pattern.rb', line 9 def day @day end |
#hour ⇒ Object (readonly)
Returns the value of attribute hour.
9 10 11 |
# File 'lib/ruy/time_pattern.rb', line 9 def hour @hour end |
#local ⇒ Object (readonly)
Returns the value of attribute local.
9 10 11 |
# File 'lib/ruy/time_pattern.rb', line 9 def local @local end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
9 10 11 |
# File 'lib/ruy/time_pattern.rb', line 9 def min @min end |
#month ⇒ Object (readonly)
Returns the value of attribute month.
9 10 11 |
# File 'lib/ruy/time_pattern.rb', line 9 def month @month end |
#sec ⇒ Object (readonly)
Returns the value of attribute sec.
9 10 11 |
# File 'lib/ruy/time_pattern.rb', line 9 def sec @sec end |
#time_zone ⇒ Object (readonly)
Returns the value of attribute time_zone.
9 10 11 |
# File 'lib/ruy/time_pattern.rb', line 9 def time_zone @time_zone end |
#tz ⇒ Object (readonly)
Returns the value of attribute tz.
9 10 11 |
# File 'lib/ruy/time_pattern.rb', line 9 def tz @tz end |
#utc ⇒ Object (readonly)
Returns the value of attribute utc.
9 10 11 |
# File 'lib/ruy/time_pattern.rb', line 9 def utc @utc end |
#utc_offset ⇒ Object (readonly)
Returns the value of attribute utc_offset.
9 10 11 |
# File 'lib/ruy/time_pattern.rb', line 9 def utc_offset @utc_offset end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
9 10 11 |
# File 'lib/ruy/time_pattern.rb', line 9 def year @year end |
Instance Method Details
#<=>(o) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ruy/time_pattern.rb', line 42 def <=>(o) if o.respond_to?(:to_time) time = o.to_time time_to_local = @tz.utc_to_local(time.utc) self_time = Time.gm( self.year || time_to_local.year, self.month || time_to_local.month, self.day || time_to_local.day, self.hour || time_to_local.hour, self.min || time_to_local.min, self.sec || time_to_local.sec, Rational(time_to_local.nsec, 1000) ) @tz.local_to_utc(self_time) <=> time else super end end |
#==(o) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ruy/time_pattern.rb', line 64 def ==(o) if o.is_a?(self.class) return year == o.year && month == o.month && day == o.day && hour == o.hour && min == o.min && sec == o.sec && time_zone == o.time_zone elsif o.respond_to?(:to_time) super else equal?(o) end end |
#inspect ⇒ Object
87 88 89 |
# File 'lib/ruy/time_pattern.rb', line 87 def inspect @pattern.inspect end |
#to_s ⇒ Object
Returns a well-formed Ruy timestamp with IANA time zone identifier representing the current TimePattern object.
83 84 85 |
# File 'lib/ruy/time_pattern.rb', line 83 def to_s @pattern end |