Class: Sumologic::Utils::TimeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/sumologic/utils/time_parser.rb

Overview

Parses various time formats into ISO 8601 strings for the Sumo Logic API Supports:

  • ‘now’ - current time

  • Relative times: ‘-30s’, ‘-5m’, ‘-2h’, ‘-7d’, ‘-1w’, ‘-1M’, ‘-1h30m’

  • Unix timestamps: ‘1700000000’ or 1700000000

  • ISO 8601: ‘2025-11-13T14:00:00’

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

UNITS =

Time unit multipliers in seconds

{
  's' => 1,           # seconds
  'm' => 60,          # minutes
  'h' => 3600,        # hours
  'd' => 86_400,       # days
  'w' => 604_800,      # weeks (7 days)
  'M' => 2_592_000 # months (30 days approximation)
}.freeze
RELATIVE_TIME_REGEX =

Matches single or compound relative times: -30m, -1h30m, -2d3h15m

/^([+-])(\d+[smhdwM])+$/.freeze
RELATIVE_COMPONENT_REGEX =
/(\d+)([smhdwM])/.freeze

Class Method Summary collapse

Class Method Details

.parse(time_str, _timezone: 'UTC') ⇒ String

Parse a time string into ISO 8601 format

Parameters:

  • Time string or Unix timestamp

  • (defaults to: 'UTC')

    IANA timezone name (default: ‘UTC’) - Reserved for future use

Returns:

  • ISO 8601 formatted time string



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sumologic/utils/time_parser.rb', line 34

def self.parse(time_str, _timezone: 'UTC')
  return parse_now if time_str.to_s.downcase == 'now'

  # Try relative time format (e.g., '-30m', '+1h', '-1h30m')
  return parse_relative_time(time_str) if time_str.is_a?(String) && time_str.match?(RELATIVE_TIME_REGEX)

  # Try Unix timestamp (integer or numeric string)
  return parse_unix_timestamp(time_str) if unix_timestamp?(time_str)

  # Try ISO 8601 format
  begin
    # Parse in UTC context to avoid local timezone conversion
    parsed = Time.parse(time_str.to_s)
    # If the input doesn't have timezone info, treat it as UTC
    parsed = parsed.getutc unless time_str.to_s.match?(/Z|[+-]\d{2}:?\d{2}$/)
    format_time(parsed)
  rescue ArgumentError
    raise ParseError,
          "Invalid time format: '#{time_str}'. " \
          "Supported formats: 'now', relative (e.g., '-30m'), Unix timestamp, or ISO 8601"
  end
end

.parse_timezone(timezone_str) ⇒ String

Parse timezone string to standard format Accepts IANA names, offset formats, or common abbreviations

Parameters:

  • Timezone string

Returns:

  • Standardized timezone string



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sumologic/utils/time_parser.rb', line 61

def self.parse_timezone(timezone_str)
  return 'UTC' if timezone_str.nil? || timezone_str.empty?

  # Handle offset formats like "+00:00", "-05:00", "+0000"
  if timezone_str.match?(/^[+-]\d{2}:?\d{2}$/)
    # Normalize to format with colon
    normalized = timezone_str.sub(/^([+-]\d{2})(\d{2})$/, '\1:\2')
    return normalized
  end

  # Map common abbreviations to IANA names
  timezone_map = {
    # US timezones
    'EST' => 'America/New_York',
    'EDT' => 'America/New_York',
    'CST' => 'America/Chicago',
    'CDT' => 'America/Chicago',
    'MST' => 'America/Denver',
    'MDT' => 'America/Denver',
    'PST' => 'America/Los_Angeles',
    'PDT' => 'America/Los_Angeles',
    # Australian timezones
    'AEST' => 'Australia/Sydney',      # Australian Eastern Standard Time
    'AEDT' => 'Australia/Sydney',      # Australian Eastern Daylight Time
    'ACST' => 'Australia/Adelaide',    # Australian Central Standard Time
    'ACDT' => 'Australia/Adelaide',    # Australian Central Daylight Time
    'AWST' => 'Australia/Perth',       # Australian Western Standard Time
    'AWDT' => 'Australia/Perth'        # Australian Western Daylight Time (rarely used)
  }

  timezone_map[timezone_str.upcase] || timezone_str
end