Class: QAT::Time

Inherits:
Object
  • Object
show all
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

Since:

  • 0.1.0

Defined Under Namespace

Modules: Zone

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Zone

get_local_tz

Class Attribute Details

.default_sync_methodString

Returns default method to be used in synchronize. Default: NTP.

Returns:

  • (String)

    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

.nowActiveSupport::TimeWithZone

Returns the current time in the current time zone

Returns:

  • (ActiveSupport::TimeWithZone)

    Current time

Since:

  • 0.1.0



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.

Parameters:

  • timestamp (String)

    text to parse

Returns:

  • (Time)

    Result of the expression

See Also:

Since:

  • 0.1.0



77
78
79
80
# File 'lib/qat/time.rb', line 77

def parse_expression timestamp
  Chronic.time_class = zone
  Chronic.parse timestamp
end

.sync_for_method(method) {|logger, host, options| ... } ⇒ Object

Defines synchronization instructions for a given method

Parameters:

  • method (String)

    the method of synchronization (eg. HTTP, SSH, NTP)

Yields:

  • (logger, host, options)

    Block to execute the synchronization

Yield Parameters:

  • logger (Log4r::Logger)

    Logging object with the Time channel

  • host (String)

    Logging object with the Time channel

  • options (Hash)

    Options passed through the synchronize method

Yield Returns:

  • (ActiveSupport::TimeWithZone)

    Object with the value of the current time

Since:

  • 0.1.0



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

Parameters:

  • host (String)

    host ip address/ hostname

  • method (String) (defaults to: default_sync_method)

    method used for synchronization: NTP or SSH.

  • opts (Hash) (defaults to: {})

    synchronization options

Returns:

  • (ActiveSupport::TimeWithZone)

    Current time

Since:

  • 0.1.0



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

.zoneActiveSupport::TimeWithZone

Returns the current time zone. If none is set, the machine’s time zone will be set.

Returns:

  • (ActiveSupport::TimeWithZone)

    Current time zone.

Since:

  • 0.1.0



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

Parameters:

  • zone (String)

    time zone to use

Returns:

  • (ActiveSupport::TimeWithZone)

See Also:

Since:

  • 0.1.0



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