Module: Ace::B36ts::Atoms::FormatCodecs

Included in:
CompactIdEncoder
Defined in:
lib/ace/b36ts/atoms/format_codecs.rb

Overview

Format-specific encode/decode/increment methods for CompactIdEncoder.

Extracted to keep the main encoder focused on dispatch and validation, while this module holds the per-format codec logic.

Methods here rely on private helpers defined in CompactIdEncoder (encode_value, decode_value, validate_*, calculate_months_offset, etc.) and are mixed in via ‘include FormatCodecs` inside the class << self block.

Instance Method Summary collapse

Instance Method Details

#decode_2sec(compact_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ Time

Decode a 6-character compact ID to a Time object (~1.85s precision)

Parameters:

  • compact_id (String)

    The 6-character compact ID

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for decoding (default: 2000)

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet (default: 0-9a-z)

Returns:

  • (Time)

    The decoded time (UTC)

Raises:

  • (ArgumentError)

    If compact_id format is invalid or components out of range



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
93
94
95
96
97
98
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 67

def decode_2sec(compact_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  validate_length!(compact_id, 6)
  validate_alphabet!(compact_id, alphabet)

  id = compact_id.downcase

  # Decode components
  months_offset = decode_value(id[0..1], alphabet)
  day = decode_value(id[2], alphabet)
  block = decode_value(id[3], alphabet)
  precision = decode_value(id[4..5], alphabet)

  # Validate component ranges to prevent Time.utc ArgumentError
  validate_component_ranges!(months_offset, day, block, precision)

  # Calculate date/time components
  year = year_zero + (months_offset / 12)
  month = (months_offset % 12) + 1
  calendar_day = day + 1

  # Calculate time from block and precision
  minutes_of_day = block * CompactIdEncoder::BLOCK_MINUTES
  seconds_into_block = (precision * CompactIdEncoder::BLOCK_SECONDS) / CompactIdEncoder::PRECISION_DIVISOR

  hour = minutes_of_day / 60
  minute = (minutes_of_day % 60) + (seconds_into_block / 60)
  second = seconds_into_block % 60

  hour, minute = normalize_minute_overflow(hour, minute)

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

#decode_40min(encoded_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ Time

Decode 40min format (4 chars)

Parameters:

  • encoded_id (String)

    The 4-character 40min ID

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for decoding

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet

Returns:

  • (Time)

    The decoded time (UTC, at start of 40-min block)



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 293

def decode_40min(encoded_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  validate_length!(encoded_id, 4)
  validate_alphabet!(encoded_id, alphabet)

  id = encoded_id.downcase
  months_offset = decode_value(id[0..1], alphabet)
  day = decode_value(id[2], alphabet)
  block = decode_value(id[3], alphabet)

  if day > CompactIdEncoder::MAX_DAY
    raise ArgumentError, "Day value #{day} exceeds maximum (#{CompactIdEncoder::MAX_DAY} = 31 calendar days)"
  end

  if months_offset > CompactIdEncoder::MAX_MONTHS_OFFSET
    raise ArgumentError, "Month offset #{months_offset} exceeds maximum (#{CompactIdEncoder::MAX_MONTHS_OFFSET} = 108 years)"
  end

  if block > CompactIdEncoder::MAX_BLOCK
    raise ArgumentError, "Block value #{block} exceeds maximum (#{CompactIdEncoder::MAX_BLOCK} = 36 blocks per day)"
  end

  year = year_zero + (months_offset / 12)
  month = (months_offset % 12) + 1
  calendar_day = day + 1

  # Calculate time from block (40-minute block)
  minutes_of_day = block * CompactIdEncoder::BLOCK_MINUTES
  hour = minutes_of_day / 60
  minute = minutes_of_day % 60

  Time.utc(year, month, calendar_day, hour, minute, 0)
end

#decode_50ms(encoded_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ Time

Decode 50ms format (7 chars)

Parameters:

  • encoded_id (String)

    The 7-character 50ms ID

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for decoding

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet

Returns:

  • (Time)

    The decoded time (UTC, ~50ms precision)



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 366

def decode_50ms(encoded_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  validate_length!(encoded_id, 7)
  validate_alphabet!(encoded_id, alphabet)

  id = encoded_id.downcase
  months_offset = decode_value(id[0..1], alphabet)
  day = decode_value(id[2], alphabet)
  block = decode_value(id[3], alphabet)
  precision = decode_value(id[4..6], alphabet)

  validate_base_components!(months_offset, day, block)
  validate_precision_range!(precision, CompactIdEncoder::PRECISION_DIVISOR_3)

  year = year_zero + (months_offset / 12)
  month = (months_offset % 12) + 1
  calendar_day = day + 1

  # Calculate time with 50ms precision
  minutes_of_day = block * CompactIdEncoder::BLOCK_MINUTES
  total_usecs = (precision * CompactIdEncoder::BLOCK_SECONDS * 1_000_000) / CompactIdEncoder::PRECISION_DIVISOR_3

  hour = minutes_of_day / 60
  remaining_usecs = total_usecs % 60_000_000
  minute = (minutes_of_day % 60) + (total_usecs / 60_000_000)
  second = (remaining_usecs / 1_000_000).to_i
  usec = (remaining_usecs % 1_000_000).to_i

  hour, minute = normalize_minute_overflow(hour, minute)

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

#decode_day(encoded_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ Time

Decode day format (3 chars)

Parameters:

  • encoded_id (String)

    The 3-character day ID

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for decoding

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet

Returns:

  • (Time)

    The decoded time (UTC, at midnight)



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 236

def decode_day(encoded_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  validate_length!(encoded_id, 3)
  validate_alphabet!(encoded_id, alphabet)

  id = encoded_id.downcase
  months_offset = decode_value(id[0..1], alphabet)
  day = decode_value(id[2], alphabet)

  unless day <= FormatSpecs::DAY_FORMAT_MAX
    raise ArgumentError, "Day value #{day} exceeds day format maximum (#{FormatSpecs::DAY_FORMAT_MAX} = 31 calendar days)"
  end

  if months_offset > CompactIdEncoder::MAX_MONTHS_OFFSET
    raise ArgumentError, "Month offset #{months_offset} exceeds maximum (#{CompactIdEncoder::MAX_MONTHS_OFFSET} = 108 years)"
  end

  year = year_zero + (months_offset / 12)
  month = (months_offset % 12) + 1
  calendar_day = day + 1

  Time.utc(year, month, calendar_day, 0, 0, 0)
end

#decode_month(encoded_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ Time

Decode month format (2 chars)

Parameters:

  • encoded_id (String)

    The 2-character month ID

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for decoding

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet

Returns:

  • (Time)

    The decoded time (UTC, first day of month at midnight)



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 122

def decode_month(encoded_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  validate_length!(encoded_id, 2)
  validate_alphabet!(encoded_id, alphabet)

  months_offset = decode_value(encoded_id.downcase, alphabet)

  if months_offset > CompactIdEncoder::MAX_MONTHS_OFFSET
    raise ArgumentError, "Month offset #{months_offset} exceeds maximum (#{CompactIdEncoder::MAX_MONTHS_OFFSET} = 108 years)"
  end

  year = year_zero + (months_offset / 12)
  month = (months_offset % 12) + 1

  Time.utc(year, month, 1, 0, 0, 0)
end

#decode_ms(encoded_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ Time

Decode ms format (8 chars)

Parameters:

  • encoded_id (String)

    The 8-character ms ID

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for decoding

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet

Returns:

  • (Time)

    The decoded time (UTC, ~1.4ms precision)



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 438

def decode_ms(encoded_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  validate_length!(encoded_id, 8)
  validate_alphabet!(encoded_id, alphabet)

  id = encoded_id.downcase
  months_offset = decode_value(id[0..1], alphabet)
  day = decode_value(id[2], alphabet)
  block = decode_value(id[3], alphabet)
  precision = decode_value(id[4..7], alphabet)

  validate_base_components!(months_offset, day, block)
  validate_precision_range!(precision, CompactIdEncoder::PRECISION_DIVISOR_4)

  year = year_zero + (months_offset / 12)
  month = (months_offset % 12) + 1
  calendar_day = day + 1

  # Calculate time with ms precision
  minutes_of_day = block * CompactIdEncoder::BLOCK_MINUTES
  total_usecs = (precision * CompactIdEncoder::BLOCK_SECONDS * 1_000_000) / CompactIdEncoder::PRECISION_DIVISOR_4

  hour = minutes_of_day / 60
  remaining_usecs = total_usecs % 60_000_000
  minute = (minutes_of_day % 60) + (total_usecs / 60_000_000)
  second = (remaining_usecs / 1_000_000).to_i
  usec = (remaining_usecs % 1_000_000).to_i

  hour, minute = normalize_minute_overflow(hour, minute)

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

#decode_week(encoded_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ Time

Decode week format (3 chars)

Returns the Thursday of the Nth ISO week in the encoded month. For week 5 in months with fewer than 5 Thursdays, the result is clamped to the last Thursday of the month (lossy).

Parameters:

  • encoded_id (String)

    The 3-character week ID

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for decoding

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet

Returns:

  • (Time)

    The decoded time (UTC, Thursday of the week at midnight)



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 172

def decode_week(encoded_id, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  validate_length!(encoded_id, 3)
  validate_alphabet!(encoded_id, alphabet)

  id = encoded_id.downcase
  months_offset = decode_value(id[0..1], alphabet)
  week_value = decode_value(id[2], alphabet)

  unless week_value.between?(FormatSpecs::WEEK_FORMAT_MIN, FormatSpecs::WEEK_FORMAT_MAX)
    raise ArgumentError, "Week value #{week_value} must be between #{FormatSpecs::WEEK_FORMAT_MIN}-#{FormatSpecs::WEEK_FORMAT_MAX}"
  end

  if months_offset > CompactIdEncoder::MAX_MONTHS_OFFSET
    raise ArgumentError, "Month offset #{months_offset} exceeds maximum (#{CompactIdEncoder::MAX_MONTHS_OFFSET} = 108 years)"
  end

  year = year_zero + (months_offset / 12)
  month = (months_offset % 12) + 1
  week_in_month = week_value - 30  # Convert back to 1-5 range

  # Find the Nth Thursday in the month
  first_of_month = Date.new(year, month, 1)
  days_until_thu = (4 - first_of_month.wday) % 7
  first_thursday = first_of_month + days_until_thu
  target_thursday = first_thursday + ((week_in_month - 1) * 7)

  # Clamp if week 5 doesn't exist in this month
  if target_thursday.month != month
    last_day = Date.new(year, month, -1)
    target_thursday = last_day - ((last_day.wday - 4) % 7)
  end

  Time.utc(target_thursday.year, target_thursday.month, target_thursday.day, 0, 0, 0)
end

#encode_2sec(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ String

Encode a Time object to a 6-character compact ID (~1.85s precision)

Parameters:

  • time (Time)

    The time to encode (should already be UTC from encode_with_format)

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for encoding (default: 2000)

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet (default: 0-9a-z)

Returns:

  • (String)

    6-character compact ID

Raises:

  • (ArgumentError)

    If time is outside supported range



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 26

def encode_2sec(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  # Note: UTC conversion handled by encode_with_format caller
  months_offset = calculate_months_offset(time, year_zero)

  # Day of month (1-31 -> 0-30, fits in single base36 digit 0-35)
  day = time.day - 1

  # 40-minute block of day (0-35)
  minutes_of_day = (time.hour * 60) + time.min
  block = minutes_of_day / CompactIdEncoder::BLOCK_MINUTES

  # Precision within block
  # Clamp seconds to valid range (0-59) to handle edge cases:
  # - Invalid time objects with seconds > 59
  # - Rounding errors in floating point time operation
  # Note: Ruby's Time#sec returns 0-59 for normal times; leap seconds
  # (60) are not typically represented in Ruby Time objects.
  clamped_seconds = time.sec.clamp(0, 59)
  seconds_into_block = ((minutes_of_day % CompactIdEncoder::BLOCK_MINUTES) * 60) + clamped_seconds
  precision = (seconds_into_block * CompactIdEncoder::PRECISION_DIVISOR) / CompactIdEncoder::BLOCK_SECONDS
  # Clamp precision to valid 2-digit base36 range (0-1295)
  # This defensive clamping handles any arithmetic edge cases
  # that could produce values outside the valid range.
  precision = precision.clamp(0, CompactIdEncoder::PRECISION_DIVISOR - 1)

  # Build result string with pre-allocated capacity to reduce allocations
  result = String.new(capacity: 6)
  result << encode_value(months_offset, 2, alphabet)
  result << encode_value(day, 1, alphabet)
  result << encode_value(block, 1, alphabet)
  result << encode_value(precision, 2, alphabet)
  result.freeze
end

#encode_40min(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ String

Encode to 40min format (4 chars: months + day + 40-minute block)

Uses 40-minute blocks (0-35) for consistency with position 4 of compact format

Parameters:

  • time (Time)

    The time to encode (should already be UTC from encode_with_format)

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for encoding

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet

Returns:

  • (String)

    4-character 40min ID



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 271

def encode_40min(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  # Note: UTC conversion handled by encode_with_format caller
  months_offset = calculate_months_offset(time, year_zero)
  day = (time.day - 1).clamp(0, CompactIdEncoder::MAX_DAY)

  # Use 40-minute blocks like position 4 of compact format
  minutes_of_day = (time.hour * 60) + time.min
  block = minutes_of_day / CompactIdEncoder::BLOCK_MINUTES  # 0-35

  result = String.new(capacity: 4)
  result << encode_value(months_offset, 2, alphabet)
  result << encode_value(day, 1, alphabet)
  result << encode_value(block, 1, alphabet)
  result.freeze
end

#encode_50ms(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ String

Encode to 50ms format (7 chars: 2sec + 1 extra precision digit) ~50ms precision

Parameters:

  • time (Time)

    The time to encode (should already be UTC from encode_with_format)

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for encoding

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet

Returns:

  • (String)

    7-character 50ms ID



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 337

def encode_50ms(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  # Note: UTC conversion handled by encode_with_format caller
  months_offset = calculate_months_offset(time, year_zero)
  day = time.day - 1
  minutes_of_day = (time.hour * 60) + time.min
  block = minutes_of_day / CompactIdEncoder::BLOCK_MINUTES

  # 50ms: 3 digits of precision (36^3 = 46656 combinations for ~50ms)
  clamped_seconds = time.sec.clamp(0, 59)
  usec = time.respond_to?(:usec) ? time.usec : 0
  total_usecs = ((minutes_of_day % CompactIdEncoder::BLOCK_MINUTES) * 60 + clamped_seconds) * 1_000_000 + usec
  block_usecs = CompactIdEncoder::BLOCK_SECONDS * 1_000_000
  precision = (total_usecs * CompactIdEncoder::PRECISION_DIVISOR_3) / block_usecs
  precision = precision.clamp(0, CompactIdEncoder::PRECISION_DIVISOR_3 - 1)

  result = String.new(capacity: 7)
  result << encode_value(months_offset, 2, alphabet)
  result << encode_value(day, 1, alphabet)
  result << encode_value(block, 1, alphabet)
  result << encode_value(precision, 3, alphabet)
  result.freeze
end

#encode_day(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ String

Encode to day format (3 chars: months + day)

Day value is in range 0-30 to distinguish from week format

Parameters:

  • time (Time)

    The time to encode (should already be UTC from encode_with_format)

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for encoding

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet

Returns:

  • (String)

    3-character day ID



219
220
221
222
223
224
225
226
227
228
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 219

def encode_day(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  # Note: UTC conversion handled by encode_with_format caller
  months_offset = calculate_months_offset(time, year_zero)
  day = (time.day - 1).clamp(0, CompactIdEncoder::MAX_DAY)

  result = String.new(capacity: 3)
  result << encode_value(months_offset, 2, alphabet)
  result << encode_value(day, 1, alphabet)
  result.freeze
end

#encode_month(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ String

Encode to month format (2 chars: months offset only)

Parameters:

  • time (Time)

    The time to encode (should already be UTC from encode_with_format)

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for encoding

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet

Returns:

  • (String)

    2-character month ID



110
111
112
113
114
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 110

def encode_month(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  # Note: UTC conversion handled by encode_with_format caller
  months_offset = calculate_months_offset(time, year_zero)
  encode_value(months_offset, 2, alphabet)
end

#encode_ms(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ String

Encode to ms format (8 chars: 50ms + 1 extra precision digit) ~1.4ms precision

Parameters:

  • time (Time)

    The time to encode (should already be UTC from encode_with_format)

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for encoding

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet

Returns:

  • (String)

    8-character ms ID



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 409

def encode_ms(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  # Note: UTC conversion handled by encode_with_format caller
  months_offset = calculate_months_offset(time, year_zero)
  day = time.day - 1
  minutes_of_day = (time.hour * 60) + time.min
  block = minutes_of_day / CompactIdEncoder::BLOCK_MINUTES

  # ms: 4 digits of precision (36^4 = 1679616 combinations for ~1.4ms)
  clamped_seconds = time.sec.clamp(0, 59)
  usec = time.respond_to?(:usec) ? time.usec : 0
  total_usecs = ((minutes_of_day % CompactIdEncoder::BLOCK_MINUTES) * 60 + clamped_seconds) * 1_000_000 + usec
  block_usecs = CompactIdEncoder::BLOCK_SECONDS * 1_000_000
  precision = (total_usecs * CompactIdEncoder::PRECISION_DIVISOR_4) / block_usecs
  precision = precision.clamp(0, CompactIdEncoder::PRECISION_DIVISOR_4 - 1)

  result = String.new(capacity: 8)
  result << encode_value(months_offset, 2, alphabet)
  result << encode_value(day, 1, alphabet)
  result << encode_value(block, 1, alphabet)
  result << encode_value(precision, 4, alphabet)
  result.freeze
end

#encode_week(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET) ⇒ String

Encode to week format (3 chars: months + week)

Week value is in range 31-35 to distinguish from day format

Parameters:

  • time (Time)

    The time to encode (should already be UTC from encode_with_format)

  • year_zero (Integer) (defaults to: CompactIdEncoder::DEFAULT_YEAR_ZERO)

    Base year for encoding

  • alphabet (String) (defaults to: CompactIdEncoder::DEFAULT_ALPHABET)

    Base36 alphabet

Returns:

  • (String)

    3-character week ID



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 150

def encode_week(time, year_zero: CompactIdEncoder::DEFAULT_YEAR_ZERO, alphabet: CompactIdEncoder::DEFAULT_ALPHABET)
  # Note: UTC conversion handled by encode_with_format caller
  iso_year, iso_month, week_in_month = iso_week_month_and_number(time)
  months_offset = calculate_months_offset_ym(iso_year, iso_month, year_zero)
  week_value = week_in_month + 30  # Offset to 31-35 range

  result = String.new(capacity: 3)
  result << encode_value(months_offset, 2, alphabet)
  result << encode_value(week_value, 1, alphabet)
  result.freeze
end

#increment_2sec_id(id, alphabet, base) ⇒ Object

Increment 2sec format ID (6 chars) Overflow: precision -> block -> day -> month



558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 558

def increment_2sec_id(id, alphabet, base)
  month = decode_value(id[0..1], alphabet)
  day = decode_value(id[2], alphabet)
  block = decode_value(id[3], alphabet)
  precision = decode_value(id[4..5], alphabet)

  precision += 1
  if precision > CompactIdEncoder::MAX_PRECISION
    precision = 0
    block += 1
    if block > CompactIdEncoder::MAX_BLOCK
      block = 0
      day += 1
      if day > CompactIdEncoder::MAX_DAY
        day = 0
        month += 1
        if month > CompactIdEncoder::MAX_MONTHS_OFFSET
          raise ArgumentError, "Cannot increment: would exceed month range (max #{CompactIdEncoder::MAX_MONTHS_OFFSET})"
        end
      end
    end
  end

  result = String.new(capacity: 6)
  result << encode_value(month, 2, alphabet)
  result << encode_value(day, 1, alphabet)
  result << encode_value(block, 1, alphabet)
  result << encode_value(precision, 2, alphabet)
  result.freeze
end

#increment_40min_id(id, alphabet, base) ⇒ Object

Increment 40min format ID (4 chars) Overflow: block -> day -> month



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 531

def increment_40min_id(id, alphabet, base)
  month = decode_value(id[0..1], alphabet)
  day = decode_value(id[2], alphabet)
  block = decode_value(id[3], alphabet)

  block += 1
  if block > CompactIdEncoder::MAX_BLOCK
    block = 0
    day += 1
    if day > CompactIdEncoder::MAX_DAY
      day = 0
      month += 1
      if month > CompactIdEncoder::MAX_MONTHS_OFFSET
        raise ArgumentError, "Cannot increment: would exceed month range (max #{CompactIdEncoder::MAX_MONTHS_OFFSET})"
      end
    end
  end

  result = String.new(capacity: 4)
  result << encode_value(month, 2, alphabet)
  result << encode_value(day, 1, alphabet)
  result << encode_value(block, 1, alphabet)
  result.freeze
end

#increment_50ms_id(id, alphabet, base) ⇒ Object

Increment 50ms format ID (7 chars) Overflow: precision -> block -> day -> month



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 591

def increment_50ms_id(id, alphabet, base)
  month = decode_value(id[0..1], alphabet)
  day = decode_value(id[2], alphabet)
  block = decode_value(id[3], alphabet)
  precision = decode_value(id[4..6], alphabet)

  max_precision_50ms = CompactIdEncoder::PRECISION_DIVISOR_3 - 1  # 46655

  precision += 1
  if precision > max_precision_50ms
    precision = 0
    block += 1
    if block > CompactIdEncoder::MAX_BLOCK
      block = 0
      day += 1
      if day > CompactIdEncoder::MAX_DAY
        day = 0
        month += 1
        if month > CompactIdEncoder::MAX_MONTHS_OFFSET
          raise ArgumentError, "Cannot increment: would exceed month range (max #{CompactIdEncoder::MAX_MONTHS_OFFSET})"
        end
      end
    end
  end

  result = String.new(capacity: 7)
  result << encode_value(month, 2, alphabet)
  result << encode_value(day, 1, alphabet)
  result << encode_value(block, 1, alphabet)
  result << encode_value(precision, 3, alphabet)
  result.freeze
end

#increment_day_id(id, alphabet, base) ⇒ Object

Increment day format ID (3 chars) Overflow: day -> month



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 510

def increment_day_id(id, alphabet, base)
  month = decode_value(id[0..1], alphabet)
  day = decode_value(id[2], alphabet)

  day += 1
  if day > CompactIdEncoder::MAX_DAY
    day = 0
    month += 1
    if month > CompactIdEncoder::MAX_MONTHS_OFFSET
      raise ArgumentError, "Cannot increment: would exceed month range (max #{CompactIdEncoder::MAX_MONTHS_OFFSET})"
    end
  end

  result = String.new(capacity: 3)
  result << encode_value(month, 2, alphabet)
  result << encode_value(day, 1, alphabet)
  result.freeze
end

#increment_month_id(id, alphabet, base) ⇒ Object

Increment month format ID (2 chars) Overflow: month -> ERROR



476
477
478
479
480
481
482
483
484
485
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 476

def increment_month_id(id, alphabet, base)
  month = decode_value(id[0..1], alphabet)
  month += 1

  if month > CompactIdEncoder::MAX_MONTHS_OFFSET
    raise ArgumentError, "Cannot increment: would exceed month range (max #{CompactIdEncoder::MAX_MONTHS_OFFSET})"
  end

  encode_value(month, 2, alphabet)
end

#increment_ms_id(id, alphabet, base) ⇒ Object

Increment ms format ID (8 chars) Overflow: precision -> block -> day -> month



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 626

def increment_ms_id(id, alphabet, base)
  month = decode_value(id[0..1], alphabet)
  day = decode_value(id[2], alphabet)
  block = decode_value(id[3], alphabet)
  precision = decode_value(id[4..7], alphabet)

  max_precision_ms = CompactIdEncoder::PRECISION_DIVISOR_4 - 1  # 1679615

  precision += 1
  if precision > max_precision_ms
    precision = 0
    block += 1
    if block > CompactIdEncoder::MAX_BLOCK
      block = 0
      day += 1
      if day > CompactIdEncoder::MAX_DAY
        day = 0
        month += 1
        if month > CompactIdEncoder::MAX_MONTHS_OFFSET
          raise ArgumentError, "Cannot increment: would exceed month range (max #{CompactIdEncoder::MAX_MONTHS_OFFSET})"
        end
      end
    end
  end

  result = String.new(capacity: 8)
  result << encode_value(month, 2, alphabet)
  result << encode_value(day, 1, alphabet)
  result << encode_value(block, 1, alphabet)
  result << encode_value(precision, 4, alphabet)
  result.freeze
end

#increment_week_id(id, alphabet, base) ⇒ Object

Increment week format ID (3 chars) Overflow: week -> month



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/ace/b36ts/atoms/format_codecs.rb', line 489

def increment_week_id(id, alphabet, base)
  month = decode_value(id[0..1], alphabet)
  week = decode_value(id[2], alphabet)

  week += 1
  if week > FormatSpecs::WEEK_FORMAT_MAX
    week = FormatSpecs::WEEK_FORMAT_MIN
    month += 1
    if month > CompactIdEncoder::MAX_MONTHS_OFFSET
      raise ArgumentError, "Cannot increment: would exceed month range (max #{CompactIdEncoder::MAX_MONTHS_OFFSET})"
    end
  end

  result = String.new(capacity: 3)
  result << encode_value(month, 2, alphabet)
  result << encode_value(week, 1, alphabet)
  result.freeze
end