buildStdTimeModule function

Module buildStdTimeModule()

Builds the std_time base module.

Implementation

Module buildStdTimeModule() {
  final module = Module()
    ..name = 'std_time'
    ..description =
        'Standard time module. Current time, formatting, parsing, durations.';

  // ============================================================
  // Types
  // ============================================================

  module.typeDefs.addAll(
    <google.DescriptorProto>[
      _type('FormatTimestampInput', [
        _intField('timestamp_ms', 1),
        _stringField('format', 2),
      ]),
      _type('ParseTimestampInput', [
        _stringField('value', 1),
        _stringField('format', 2),
      ]),
      _type('DurationInput', [_intField('left', 1), _intField('right', 2)]),
    ].map(
      (d) => TypeDefinition()
        ..name = d.name
        ..descriptor = d,
    ),
  );

  // ============================================================
  // Functions
  // ============================================================

  module.functions.addAll([
    // Current time
    _fn('now', '', 'int', 'Current time in milliseconds since epoch'),
    _fn('now_micros', '', 'int', 'Current time in microseconds since epoch'),

    // Formatting / parsing
    _fn(
      'format_timestamp',
      'FormatTimestampInput',
      'string',
      'Format ms-since-epoch to ISO 8601 or custom format string',
    ),
    _fn(
      'parse_timestamp',
      'ParseTimestampInput',
      'int',
      'Parse timestamp string to ms-since-epoch',
    ),

    // Duration arithmetic
    _fn('duration_add', 'DurationInput', 'int', 'Add two durations (ms + ms)'),
    _fn(
      'duration_subtract',
      'DurationInput',
      'int',
      'Subtract two durations (ms - ms)',
    ),

    // Components
    _fn('year', '', 'int', 'Year component of current UTC time'),
    _fn('month', '', 'int', 'Month component of current UTC time (1-12)'),
    _fn('day', '', 'int', 'Day component of current UTC time (1-31)'),
    _fn('hour', '', 'int', 'Hour component of current UTC time (0-23)'),
    _fn('minute', '', 'int', 'Minute component of current UTC time (0-59)'),
    _fn('second', '', 'int', 'Second component of current UTC time (0-59)'),
  ]);

  return module;
}