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”)
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
-
.compact?(value) ⇒ Boolean
Check if value is a valid compact ID format.
-
.detect(value) ⇒ Symbol?
Detect the format type of a timestamp string.
-
.format_timestamp(time) ⇒ String
Format a Time object to timestamp format.
-
.parse_timestamp(value) ⇒ Time
Parse a timestamp format string to Time.
-
.timestamp?(value) ⇒ Boolean
Check if value is a valid timestamp format.
Class Method Details
.compact?(value) ⇒ Boolean
Check if value is a valid compact ID 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
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
102 103 104 105 |
# File 'lib/ace/b36ts/atoms/formats.rb', line 102 def (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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ace/b36ts/atoms/formats.rb', line 83 def (value) unless (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
74 75 76 |
# File 'lib/ace/b36ts/atoms/formats.rb', line 74 def (value) detect(value) == :timestamp end |