Class: QAT::Time
- Inherits:
-
Object
- Object
- QAT::Time
- Extended by:
- Zone
- Includes:
- Logger
- Defined in:
- lib/qat/time.rb,
lib/qat/time/zone/unix.rb,
lib/qat/time/zone/windows.rb
Overview
This class represents QAT’s Time extensions/enhancements.
This class should be used globally to obtain Time object, allowing centralized modification of time generation. There are three main featured united in this class:
-
Clock Synchronization
-
Time zone change
-
Natural time expression parsing
Defined Under Namespace
Modules: Zone
Class Attribute Summary collapse
-
.default_sync_method ⇒ String
Default method to be used in Time.synchronize.
Class Method Summary collapse
-
.now ⇒ ActiveSupport::TimeWithZone
Returns the current time in the current time zone.
-
.parse_expression(timestamp) ⇒ Time
Parses a string containing a natural language date or time.
-
.sync_for_method(method) {|logger, host, options| ... } ⇒ Object
Defines synchronization instructions for a given method.
-
.synchronize(host, method = default_sync_method, opts = {}) ⇒ ActiveSupport::TimeWithZone
Synchronizes the host’s time/date with a remote server time/date.
-
.zone ⇒ ActiveSupport::TimeWithZone
Returns the current time zone.
-
.zone=(zone) ⇒ ActiveSupport::TimeWithZone
Sets the timezone.
Methods included from Zone
Class Attribute Details
.default_sync_method ⇒ String
Returns default method to be used in synchronize. Default: NTP.
35 36 37 |
# File 'lib/qat/time.rb', line 35 def default_sync_method @default_sync_method end |
Class Method Details
.now ⇒ ActiveSupport::TimeWithZone
Returns the current time in the current time zone
114 115 116 |
# File 'lib/qat/time.rb', line 114 def now self.zone.now end |
.parse_expression(timestamp) ⇒ Time
Parses a string containing a natural language date or time.
77 78 79 80 |
# File 'lib/qat/time.rb', line 77 def parse_expression Chronic.time_class = zone Chronic.parse end |
.sync_for_method(method) {|logger, host, options| ... } ⇒ Object
Defines synchronization instructions for a given method
126 127 128 129 130 |
# File 'lib/qat/time.rb', line 126 def sync_for_method(method, &block) raise ArgumentError.new 'No method name defined!' unless method raise ArgumentError.new "Block parameters should be 3: |logger, host, options|, but block only has #{block.arity} parameters" unless block.arity == 3 define_singleton_method to_method_name(method), &block end |
.synchronize(host, method = default_sync_method, opts = {}) ⇒ ActiveSupport::TimeWithZone
Synchronizes the host’s time/date with a remote server time/date
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/qat/time.rb', line 43 def synchronize(host, method = default_sync_method, opts = {}) opts ||= {} # opts can be a *nil* when it comes from qat/cucumber log.info {"Synchronizing with host #{host} using #{method} method"} sync_meth = to_method_name method unless respond_to? sync_meth log.error {"No synchronize method #{method} defined!"} raise NotImplementedError.new "No implementation of syncronization using the '#{method}' method" end host = nil if host.to_s.empty? local_ips = Socket.ip_address_list.map &:ip_address local_ips << '::1' unless local_ips.include? '::1' dns_timeout = (opts and (opts[:dns_timeout] || opts[:timeout])) || 15 Timeout.timeout dns_timeout do if local_ips.include? Resolv.getaddress(host) log.info {'Target host is localhost, returning'} return now end end time_point = self.method(sync_meth).call(log, host, opts) raise ArgumentError.new "Expected the result from #{sync_meth} to be a Time object" unless time_point.is_a? ::Time log.info {"Synchronizing to #{time_point.strftime '%F %T,%L %z'}"} Timecop.travel time_point log.info {"Done!"} now end |
.zone ⇒ ActiveSupport::TimeWithZone
Returns the current time zone. If none is set, the machine’s time zone will be set.
85 86 87 88 89 90 91 92 93 |
# File 'lib/qat/time.rb', line 85 def zone unless ::Time.zone ::Time.zone = get_local_tz unless ::Time.zone ::Time.zone = ActiveSupport::TimeZone['UTC'] end end ::Time.zone end |
.zone=(zone) ⇒ ActiveSupport::TimeWithZone
Sets the timezone
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/qat/time.rb', line 100 def zone=(zone) if zone.nil? zone = ActiveSupport::TimeZone['UTC'] log.warn "Zone was nil change to UTC" end ::Time.zone = zone rescue ArgumentError log.warn "Zone was nil change to UTC" ::Time.zone = ActiveSupport::TimeZone['UTC'] end |