Module: OmniauthOpenidFederation::TimeHelpers

Defined in:
lib/omniauth_openid_federation/time_helpers.rb

Class Method Summary collapse

Class Method Details

.at(timestamp) ⇒ Time

Convert a timestamp to Time, using Time.zone if available, otherwise Time

Parameters:

  • timestamp (Integer, Float)

    Unix timestamp

Returns:

  • (Time)

    Time object representing the timestamp



22
23
24
25
26
27
28
29
30
# File 'lib/omniauth_openid_federation/time_helpers.rb', line 22

def self.at(timestamp)
  if time_zone_available?
    Time.zone.at(timestamp)
  else
    # rubocop:disable Rails/TimeZone
    Time.at(timestamp)
    # rubocop:enable Rails/TimeZone
  end
end

.nowTime

Get current time, using Time.zone if available, otherwise Time

Returns:

  • (Time)

    Current time



8
9
10
11
12
13
14
15
16
# File 'lib/omniauth_openid_federation/time_helpers.rb', line 8

def self.now
  if time_zone_available?
    Time.zone.now
  else
    # rubocop:disable Rails/TimeZone
    Time.now
    # rubocop:enable Rails/TimeZone
  end
end

.parse(time_string) ⇒ Time

Parse a time string, using Time.zone if available, otherwise Time

Parameters:

  • time_string (String)

    Time string to parse

Returns:

  • (Time)

    Parsed time object



36
37
38
39
40
41
42
43
44
# File 'lib/omniauth_openid_federation/time_helpers.rb', line 36

def self.parse(time_string)
  if time_zone_available?
    Time.zone.parse(time_string)
  else
    # rubocop:disable Rails/TimeZone
    Time.parse(time_string)
    # rubocop:enable Rails/TimeZone
  end
end

.time_zone_available?Boolean

Check if Time.zone is available and configured

Returns:

  • (Boolean)

    true if Time.zone is available and not nil



49
50
51
52
53
54
55
56
57
58
# File 'lib/omniauth_openid_federation/time_helpers.rb', line 49

def self.time_zone_available?
  return false unless defined?(ActiveSupport)
  return false unless Time.respond_to?(:zone)

  begin
    !Time.zone.nil?
  rescue
    false
  end
end