Module: Ace::B36ts::Atoms::Formats

Defined in:
lib/ace/b36ts/atoms/formats.rb

Overview

Detects and validates timestamp format types.

Supports multiple compact ID formats:

  • :“2sec” - 6-character Base36 compact ID (e.g., “i50jj3”)

  • :month - 2-character Base36 month ID (e.g., “i5”)

  • :week - 3-character Base36 week ID (e.g., “i5v”)

  • :day - 3-character Base36 day ID (e.g., “i50”)

  • :“40min” - 4-character Base36 40min block ID (e.g., “i50j”)

  • :“50ms” - 7-character Base36 high-precision ID (e.g., “i50jj3z”)

  • :ms - 8-character Base36 high-precision ID (e.g., “i50jj3zz”)

  • :timestamp - 14-character timestamp format (e.g., “20250101-120000”)

Examples:

Detect format

Formats.detect("i50jj3")         # => :"2sec"
Formats.detect("i5")             # => :month
Formats.detect("i5v")            # => :week (3rd char 31-35)
Formats.detect("i50")            # => :day (3rd char 0-30)
Formats.detect("i50j")           # => :"40min"
Formats.detect("i50jj3z")        # => :"50ms"
Formats.detect("i50jj3zz")       # => :ms
Formats.detect("20250101-120000") # => :timestamp
Formats.detect("invalid")         # => nil

Constant Summary collapse

MONTH_PATTERN =

Regex patterns for format detection

/\A[0-9a-z]{2}\z/i
DAY_WEEK_PATTERN =

Disambiguate by 3rd char value

/\A[0-9a-z]{3}\z/i
HOUR_PATTERN =
/\A[0-9a-z]{4}\z/i
COMPACT_PATTERN =
/\A[0-9a-z]{6}\z/i
HIGH_7_PATTERN =
/\A[0-9a-z]{7}\z/i
HIGH_8_PATTERN =
/\A[0-9a-z]{8}\z/i
TIMESTAMP_PATTERN =
/\A\d{8}-\d{6}\z/

Class Method Summary collapse

Class Method Details

.compact?(value) ⇒ Boolean

Check if value is a valid compact ID format

Parameters:

  • value (String)

    The string to check

Returns:

  • (Boolean)

    true if valid compact format



66
67
68
# File 'lib/ace/b36ts/atoms/formats.rb', line 66

def compact?(value)
  detect(value) == :"2sec"
end

.detect(value) ⇒ Symbol?

Detect the format type of a timestamp string

For 3-character IDs, uses the 3rd character value to distinguish day vs week:

  • Day format: 3rd char in 0-30 range

  • Week format: 3rd char in 31-35 range

Parameters:

  • value (String)

    The timestamp string to analyze

Returns:

  • (Symbol, nil)

    :“2sec”, :month, :week, :day, :“40min”, :“50ms”, :ms, :timestamp, or nil if unrecognized



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ace/b36ts/atoms/formats.rb', line 50

def detect(value)
  return nil unless value.is_a?(String)

  # For compact ID formats, delegate to FormatSpecs for proper detection
  # including day/week disambiguation
  if FormatSpecs::FORMATS.values.any? { |spec| spec.pattern.match?(value) }
    FormatSpecs.detect_from_id(value)
  elsif TIMESTAMP_PATTERN.match?(value)
    :timestamp
  end
end

.format_timestamp(time) ⇒ String

Format a Time object to timestamp format

Parameters:

  • time (Time)

    The time to format

Returns:

  • (String)

    Timestamp in “YYYYMMDD-HHMMSS” format



102
103
104
105
# File 'lib/ace/b36ts/atoms/formats.rb', line 102

def format_timestamp(time)
  time = time.utc if time.respond_to?(:utc)
  time.strftime("%Y%m%d-%H%M%S")
end

.parse_timestamp(value) ⇒ Time

Parse a timestamp format string to Time

Parameters:

  • value (String)

    Timestamp in “YYYYMMDD-HHMMSS” format

Returns:

  • (Time)

    Parsed time in UTC

Raises:

  • (ArgumentError)

    If format is invalid



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ace/b36ts/atoms/formats.rb', line 83

def parse_timestamp(value)
  unless timestamp?(value)
    raise ArgumentError, "Invalid timestamp format: #{value} (expected YYYYMMDD-HHMMSS)"
  end

  year = value[0..3].to_i
  month = value[4..5].to_i
  day = value[6..7].to_i
  hour = value[9..10].to_i
  minute = value[11..12].to_i
  second = value[13..14].to_i

  Time.utc(year, month, day, hour, minute, second)
end

.timestamp?(value) ⇒ Boolean

Check if value is a valid timestamp format

Parameters:

  • value (String)

    The string to check

Returns:

  • (Boolean)

    true if valid timestamp format



74
75
76
# File 'lib/ace/b36ts/atoms/formats.rb', line 74

def timestamp?(value)
  detect(value) == :timestamp
end