Skip to content
Kward Search API index

Class: Kward::Tools::DateTime

Inherits:
Base
  • Object
show all
Defined in:
lib/kward/tools/datetime.rb

Constant Summary collapse

OFFSET_PATTERN =
/\A(?:Z|[+-](?:0\d|1[0-4]):[0-5]\d)\z/.freeze
TIMESTAMP_OFFSET_PATTERN =
/(?:Z|[+-]\d{2}:?\d{2})\z/i.freeze

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#schema

Constructor Details

#initialize ⇒ DateTime

Returns a new instance of DateTime.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kward/tools/datetime.rb', line 12

def initialize
  super(
    "datetime",
    "Get the current date and time, or add signed whole seconds to an ISO 8601 timestamp. Current time uses the host's local timezone unless a UTC offset is supplied. Arithmetic preserves the timestamp's offset; offsets do not include daylight-saving rules.",
    properties: {
      operation: {
        type: "string",
        enum: %w[current_time add_seconds],
        description: "Operation to perform."
      },
      timezone_offset: {
        type: "string",
        description: "Optional UTC offset for current_time, formatted as +HH:MM, -HH:MM, or Z."
      },
      timestamp: {
        type: "string",
        description: "ISO 8601 timestamp with an explicit UTC offset, required for add_seconds."
      },
      seconds: {
        type: "integer",
        description: "Signed whole number of seconds to add, required for add_seconds."
      }
    },
    required: ["operation"]
  )
end

Instance Method Details

#call(args, _conversation, cancellation: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kward/tools/datetime.rb', line 39

def call(args, _conversation, cancellation: nil)
  cancellation&.raise_if_cancelled!

  case argument(args, :operation)
  when "current_time"
    current_time(args)
  when "add_seconds"
    add_seconds(args)
  else
    "Error: operation must be current_time or add_seconds"
  end
rescue ArgumentError, RangeError, TypeError => e
  "Error: #{e.message}"
end